update
This commit is contained in:
@@ -52,10 +52,10 @@ class CategoryController extends AbstractController
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: 'delete', methods: ['DELETE'])]
|
||||
public function delete(Category $category): Response
|
||||
public function delete(Category $category): JsonResponse
|
||||
{
|
||||
$this->categoryManager->deleteCategory($category);
|
||||
|
||||
return new Response(status: Response::HTTP_NO_CONTENT);
|
||||
return $this->json(null, Response::HTTP_NO_CONTENT);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,10 +37,10 @@ class TaskController extends AbstractController
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: 'delete', methods: ['DELETE'])]
|
||||
public function delete(Task $task): Response
|
||||
public function delete(Task $task): JsonResponse
|
||||
{
|
||||
$this->taskManager->deleteTask($task);
|
||||
|
||||
return new Response(status: Response::HTTP_NO_CONTENT);
|
||||
return $this->json(null, Response::HTTP_NO_CONTENT);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ use App\DTO\Request\ToggleRequest;
|
||||
use App\DTO\Request\UpdateSchemaRequest;
|
||||
use App\Entity\TaskSchema;
|
||||
use App\Repository\TaskSchemaRepository;
|
||||
use App\Service\TaskManager;
|
||||
use App\Service\TaskSchemaManager;
|
||||
use App\Service\TaskViewBuilder;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
@@ -22,10 +23,11 @@ class TaskSchemaController extends AbstractController
|
||||
public function __construct(
|
||||
private TaskSchemaRepository $schemaRepository,
|
||||
private TaskSchemaManager $schemaManager,
|
||||
private TaskManager $taskManager,
|
||||
private TaskViewBuilder $taskViewBuilder,
|
||||
) {}
|
||||
|
||||
#[Route('', name: 'api_schemas_index', methods: ['GET'])]
|
||||
#[Route('', name: 'schemas.index', methods: ['GET'])]
|
||||
public function index(): JsonResponse
|
||||
{
|
||||
$schemas = $this->schemaRepository->findAll();
|
||||
@@ -33,7 +35,7 @@ class TaskSchemaController extends AbstractController
|
||||
return $this->json($schemas, context: ['groups' => ['schema:read', 'category:read']]);
|
||||
}
|
||||
|
||||
#[Route('/week', name: 'api_schemas_week', methods: ['GET'])]
|
||||
#[Route('/week', name: 'schemas.week', methods: ['GET'])]
|
||||
public function week(Request $request): JsonResponse
|
||||
{
|
||||
$startParam = $request->query->get('start');
|
||||
@@ -42,7 +44,7 @@ class TaskSchemaController extends AbstractController
|
||||
return $this->json($this->taskViewBuilder->buildWeekView($start));
|
||||
}
|
||||
|
||||
#[Route('/all', name: 'api_schemas_all', methods: ['GET'])]
|
||||
#[Route('/all', name: 'schemas.all', methods: ['GET'])]
|
||||
public function allSchemas(): JsonResponse
|
||||
{
|
||||
$schemas = $this->schemaRepository->findBy([], ['createdAt' => 'DESC']);
|
||||
@@ -50,19 +52,19 @@ class TaskSchemaController extends AbstractController
|
||||
return $this->json($schemas, context: ['groups' => ['schema:read', 'category:read']]);
|
||||
}
|
||||
|
||||
#[Route('/all-tasks', name: 'api_schemas_all_tasks', methods: ['GET'])]
|
||||
#[Route('/all-tasks', name: 'schemas.allTasks', methods: ['GET'])]
|
||||
public function allTasks(): JsonResponse
|
||||
{
|
||||
return $this->json($this->taskViewBuilder->buildAllTasksView());
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: 'api_schemas_show', methods: ['GET'])]
|
||||
#[Route('/{id}', name: 'schemas.show', methods: ['GET'])]
|
||||
public function show(TaskSchema $schema): JsonResponse
|
||||
{
|
||||
return $this->json($schema, context: ['groups' => ['schema:read', 'category:read']]);
|
||||
}
|
||||
|
||||
#[Route('', name: 'api_schemas_create', methods: ['POST'])]
|
||||
#[Route('', name: 'schemas.create', methods: ['POST'])]
|
||||
public function create(#[MapRequestPayload] CreateSchemaRequest $dto): JsonResponse
|
||||
{
|
||||
$schema = $this->schemaManager->createSchema($dto);
|
||||
@@ -70,7 +72,7 @@ class TaskSchemaController extends AbstractController
|
||||
return $this->json($schema, Response::HTTP_CREATED, context: ['groups' => ['schema:read', 'category:read']]);
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: 'api_schemas_update', methods: ['PUT'])]
|
||||
#[Route('/{id}', name: 'schemas.update', methods: ['PUT'])]
|
||||
public function update(#[MapRequestPayload] UpdateSchemaRequest $dto, TaskSchema $schema): JsonResponse
|
||||
{
|
||||
$schema = $this->schemaManager->updateSchema($schema, $dto);
|
||||
@@ -78,7 +80,7 @@ class TaskSchemaController extends AbstractController
|
||||
return $this->json($schema, context: ['groups' => ['schema:read', 'category:read']]);
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: 'api_schemas_delete', methods: ['DELETE'])]
|
||||
#[Route('/{id}', name: 'schemas.delete', methods: ['DELETE'])]
|
||||
public function delete(TaskSchema $schema): JsonResponse
|
||||
{
|
||||
$this->schemaManager->deleteSchema($schema);
|
||||
@@ -86,10 +88,10 @@ class TaskSchemaController extends AbstractController
|
||||
return $this->json(null, Response::HTTP_NO_CONTENT);
|
||||
}
|
||||
|
||||
#[Route('/{id}/toggle', name: 'api_schemas_toggle', methods: ['PATCH'])]
|
||||
#[Route('/{id}/toggle', name: 'schemas.toggle', methods: ['PATCH'])]
|
||||
public function toggle(TaskSchema $schema, #[MapRequestPayload] ToggleRequest $dto): JsonResponse
|
||||
{
|
||||
$result = $this->schemaManager->toggleTaskStatus($schema, $dto);
|
||||
$result = $this->taskManager->toggleTaskStatus($schema, $dto);
|
||||
|
||||
return $this->json($result);
|
||||
}
|
||||
|
||||
@@ -3,10 +3,11 @@
|
||||
namespace App\DTO\Request;
|
||||
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
|
||||
class CreateSchemaRequest
|
||||
{
|
||||
use SchemaValidationTrait;
|
||||
|
||||
#[Assert\NotBlank]
|
||||
#[Assert\Length(max: 255)]
|
||||
public ?string $name = null;
|
||||
@@ -20,40 +21,4 @@ class CreateSchemaRequest
|
||||
public ?array $weekdays = null;
|
||||
public ?array $monthDays = null;
|
||||
public ?array $yearDays = null;
|
||||
|
||||
#[Assert\Callback]
|
||||
public function validate(ExecutionContextInterface $context): void
|
||||
{
|
||||
if ($this->taskType !== null && $this->taskType !== 'einzel') {
|
||||
if ($this->startDate !== null && $this->endDate !== null && $this->endDate < $this->startDate) {
|
||||
$context->buildViolation('endDate muss >= startDate sein.')
|
||||
->atPath('endDate')
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->taskType === 'woechentlich') {
|
||||
if (empty($this->weekdays)) {
|
||||
$context->buildViolation('weekdays darf nicht leer sein.')
|
||||
->atPath('weekdays')
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->taskType === 'monatlich') {
|
||||
if (empty($this->monthDays)) {
|
||||
$context->buildViolation('monthDays darf nicht leer sein.')
|
||||
->atPath('monthDays')
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->taskType === 'multi' || $this->taskType === 'jaehrlich') {
|
||||
if (empty($this->yearDays)) {
|
||||
$context->buildViolation('yearDays darf nicht leer sein.')
|
||||
->atPath('yearDays')
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
43
backend/src/DTO/Request/SchemaValidationTrait.php
Normal file
43
backend/src/DTO/Request/SchemaValidationTrait.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\DTO\Request;
|
||||
|
||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
|
||||
trait SchemaValidationTrait
|
||||
{
|
||||
public function validate(ExecutionContextInterface $context): void
|
||||
{
|
||||
if ($this->taskType !== null && $this->taskType !== 'einzel') {
|
||||
if ($this->startDate !== null && $this->endDate !== null && $this->endDate < $this->startDate) {
|
||||
$context->buildViolation('endDate muss >= startDate sein.')
|
||||
->atPath('endDate')
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->taskType === 'woechentlich') {
|
||||
if (empty($this->weekdays)) {
|
||||
$context->buildViolation('weekdays darf nicht leer sein.')
|
||||
->atPath('weekdays')
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->taskType === 'monatlich') {
|
||||
if (empty($this->monthDays)) {
|
||||
$context->buildViolation('monthDays darf nicht leer sein.')
|
||||
->atPath('monthDays')
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->taskType === 'multi' || $this->taskType === 'jaehrlich') {
|
||||
if (empty($this->yearDays)) {
|
||||
$context->buildViolation('yearDays darf nicht leer sein.')
|
||||
->atPath('yearDays')
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,10 +3,11 @@
|
||||
namespace App\DTO\Request;
|
||||
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
|
||||
class UpdateSchemaRequest
|
||||
{
|
||||
use SchemaValidationTrait;
|
||||
|
||||
#[Assert\NotBlank]
|
||||
#[Assert\Length(max: 255)]
|
||||
public ?string $name = null;
|
||||
@@ -21,40 +22,4 @@ class UpdateSchemaRequest
|
||||
public ?array $weekdays = null;
|
||||
public ?array $monthDays = null;
|
||||
public ?array $yearDays = null;
|
||||
|
||||
#[Assert\Callback]
|
||||
public function validate(ExecutionContextInterface $context): void
|
||||
{
|
||||
if ($this->taskType !== null && $this->taskType !== 'einzel') {
|
||||
if ($this->startDate !== null && $this->endDate !== null && $this->endDate < $this->startDate) {
|
||||
$context->buildViolation('endDate muss >= startDate sein.')
|
||||
->atPath('endDate')
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->taskType === 'woechentlich') {
|
||||
if (empty($this->weekdays)) {
|
||||
$context->buildViolation('weekdays darf nicht leer sein.')
|
||||
->atPath('weekdays')
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->taskType === 'monatlich') {
|
||||
if (empty($this->monthDays)) {
|
||||
$context->buildViolation('monthDays darf nicht leer sein.')
|
||||
->atPath('monthDays')
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->taskType === 'multi' || $this->taskType === 'jaehrlich') {
|
||||
if (empty($this->yearDays)) {
|
||||
$context->buildViolation('yearDays darf nicht leer sein.')
|
||||
->atPath('yearDays')
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,12 +19,12 @@ class TaskRepository extends ServiceEntityRepository
|
||||
parent::__construct($registry, Task::class);
|
||||
}
|
||||
|
||||
public function findByTaskAndDate(TaskSchema $task, \DateTimeInterface $date): ?Task
|
||||
public function findBySchemaAndDate(TaskSchema $schema, \DateTimeInterface $date): ?Task
|
||||
{
|
||||
return $this->createQueryBuilder('o')
|
||||
->where('o.schema = :task')
|
||||
->andWhere('o.date = :date')
|
||||
->setParameter('task', $task)
|
||||
return $this->createQueryBuilder('task')
|
||||
->where('task.schema = :schema')
|
||||
->andWhere('task.date = :date')
|
||||
->setParameter('schema', $schema)
|
||||
->setParameter('date', $date)
|
||||
->getQuery()
|
||||
->getOneOrNullResult();
|
||||
@@ -35,30 +35,30 @@ class TaskRepository extends ServiceEntityRepository
|
||||
*/
|
||||
public function findInRange(\DateTimeInterface $from, \DateTimeInterface $to): array
|
||||
{
|
||||
return $this->createQueryBuilder('o')
|
||||
->join('o.schema', 't')
|
||||
->leftJoin('t.category', 'c')
|
||||
->addSelect('t', 'c')
|
||||
->where('o.date >= :from')
|
||||
->andWhere('o.date <= :to')
|
||||
->andWhere('t.status != :excluded')
|
||||
return $this->createQueryBuilder('task')
|
||||
->join('task.schema', 'schema')
|
||||
->leftJoin('schema.category', 'category')
|
||||
->addSelect('schema', 'category')
|
||||
->where('task.date >= :from')
|
||||
->andWhere('task.date <= :to')
|
||||
->andWhere('schema.status != :excluded')
|
||||
->setParameter('from', $from)
|
||||
->setParameter('to', $to)
|
||||
->setParameter('excluded', TaskSchemaStatus::Inactive)
|
||||
->orderBy('o.date', 'ASC')
|
||||
->orderBy('task.date', 'ASC')
|
||||
->getQuery()
|
||||
->getResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, true> Set of "taskId-YYYY-MM-DD" keys
|
||||
* @return array<string, true> Set of "schemaId-YYYY-MM-DD" keys
|
||||
*/
|
||||
public function getExistingKeys(\DateTimeInterface $from, \DateTimeInterface $to): array
|
||||
{
|
||||
$rows = $this->createQueryBuilder('o')
|
||||
->select('IDENTITY(o.schema) AS schemaId', 'o.date')
|
||||
->where('o.date >= :from')
|
||||
->andWhere('o.date <= :to')
|
||||
$rows = $this->createQueryBuilder('task')
|
||||
->select('IDENTITY(task.schema) AS schemaId', 'task.date')
|
||||
->where('task.date >= :from')
|
||||
->andWhere('task.date <= :to')
|
||||
->setParameter('from', $from)
|
||||
->setParameter('to', $to)
|
||||
->getQuery()
|
||||
@@ -78,37 +78,37 @@ class TaskRepository extends ServiceEntityRepository
|
||||
/**
|
||||
* @return Task[]
|
||||
*/
|
||||
public function findByTaskFromDate(TaskSchema $task, \DateTimeInterface $fromDate): array
|
||||
public function findBySchemaFromDate(TaskSchema $schema, \DateTimeInterface $fromDate): array
|
||||
{
|
||||
return $this->createQueryBuilder('o')
|
||||
->where('o.schema = :task')
|
||||
->andWhere('o.date >= :from')
|
||||
->setParameter('task', $task)
|
||||
return $this->createQueryBuilder('task')
|
||||
->where('task.schema = :schema')
|
||||
->andWhere('task.date >= :from')
|
||||
->setParameter('schema', $schema)
|
||||
->setParameter('from', $fromDate)
|
||||
->getQuery()
|
||||
->getResult();
|
||||
}
|
||||
|
||||
public function deleteFutureByTask(TaskSchema $task, \DateTimeInterface $fromDate): void
|
||||
public function deleteFutureBySchema(TaskSchema $schema, \DateTimeInterface $fromDate): void
|
||||
{
|
||||
$this->createQueryBuilder('o')
|
||||
$this->createQueryBuilder('task')
|
||||
->delete()
|
||||
->where('o.schema = :task')
|
||||
->andWhere('o.date >= :from')
|
||||
->setParameter('task', $task)
|
||||
->where('task.schema = :schema')
|
||||
->andWhere('task.date >= :from')
|
||||
->setParameter('schema', $schema)
|
||||
->setParameter('from', $fromDate)
|
||||
->getQuery()
|
||||
->execute();
|
||||
}
|
||||
|
||||
public function deleteFutureActive(TaskSchema $task, \DateTimeInterface $fromDate): void
|
||||
public function deleteFutureActiveBySchema(TaskSchema $schema, \DateTimeInterface $fromDate): void
|
||||
{
|
||||
$this->createQueryBuilder('o')
|
||||
$this->createQueryBuilder('task')
|
||||
->delete()
|
||||
->where('o.schema = :task')
|
||||
->andWhere('o.date >= :from')
|
||||
->andWhere('o.status = :status')
|
||||
->setParameter('task', $task)
|
||||
->where('task.schema = :schema')
|
||||
->andWhere('task.date >= :from')
|
||||
->andWhere('task.status = :status')
|
||||
->setParameter('schema', $schema)
|
||||
->setParameter('from', $fromDate)
|
||||
->setParameter('status', TaskStatus::Active)
|
||||
->getQuery()
|
||||
@@ -120,12 +120,12 @@ class TaskRepository extends ServiceEntityRepository
|
||||
*/
|
||||
public function findAllSorted(): array
|
||||
{
|
||||
return $this->createQueryBuilder('o')
|
||||
->join('o.schema', 't')
|
||||
->leftJoin('t.category', 'c')
|
||||
->addSelect('t', 'c')
|
||||
->where('o.date IS NOT NULL')
|
||||
->orderBy('o.date', 'DESC')
|
||||
return $this->createQueryBuilder('task')
|
||||
->join('task.schema', 'schema')
|
||||
->leftJoin('schema.category', 'category')
|
||||
->addSelect('schema', 'category')
|
||||
->where('task.date IS NOT NULL')
|
||||
->orderBy('task.date', 'DESC')
|
||||
->getQuery()
|
||||
->getResult();
|
||||
}
|
||||
@@ -135,14 +135,14 @@ class TaskRepository extends ServiceEntityRepository
|
||||
*/
|
||||
public function findWithoutDate(): array
|
||||
{
|
||||
return $this->createQueryBuilder('o')
|
||||
->join('o.schema', 't')
|
||||
->leftJoin('t.category', 'c')
|
||||
->addSelect('t', 'c')
|
||||
->where('o.date IS NULL')
|
||||
->andWhere('t.status != :excluded')
|
||||
return $this->createQueryBuilder('task')
|
||||
->join('task.schema', 'schema')
|
||||
->leftJoin('schema.category', 'category')
|
||||
->addSelect('schema', 'category')
|
||||
->where('task.date IS NULL')
|
||||
->andWhere('schema.status != :excluded')
|
||||
->setParameter('excluded', TaskSchemaStatus::Inactive)
|
||||
->orderBy('o.createdAt', 'DESC')
|
||||
->orderBy('task.createdAt', 'DESC')
|
||||
->getQuery()
|
||||
->getResult();
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Repository;
|
||||
|
||||
use App\Entity\TaskSchema;
|
||||
use App\Enum\TaskSchemaStatus;
|
||||
use App\Enum\TaskSchemaType;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
@@ -20,7 +21,7 @@ class TaskSchemaRepository extends ServiceEntityRepository
|
||||
/**
|
||||
* @return TaskSchema[]
|
||||
*/
|
||||
public function findActiveTasksInRange(\DateTimeInterface $from, \DateTimeInterface $to): array
|
||||
public function findActiveSchemasInRange(\DateTimeInterface $from, \DateTimeInterface $to): array
|
||||
{
|
||||
return $this->createQueryBuilder('t')
|
||||
->where('t.status != :excluded')
|
||||
@@ -30,7 +31,7 @@ class TaskSchemaRepository extends ServiceEntityRepository
|
||||
. '(t.taskType != :einzel AND t.startDate <= :to AND (t.endDate IS NULL OR t.endDate >= :from))'
|
||||
)
|
||||
->setParameter('excluded', TaskSchemaStatus::Inactive)
|
||||
->setParameter('einzel', 'einzel')
|
||||
->setParameter('einzel', TaskSchemaType::Single->value)
|
||||
->setParameter('from', $from)
|
||||
->setParameter('to', $to)
|
||||
->getQuery()
|
||||
|
||||
@@ -20,7 +20,7 @@ class TaskGenerator
|
||||
|
||||
public function generateForRange(\DateTimeInterface $from, \DateTimeInterface $to): void
|
||||
{
|
||||
$schemas = $this->schemaRepository->findActiveTasksInRange($from, $to);
|
||||
$schemas = $this->schemaRepository->findActiveSchemasInRange($from, $to);
|
||||
$existingKeys = $this->taskRepository->getExistingKeys($from, $to);
|
||||
|
||||
$hasNew = false;
|
||||
@@ -60,10 +60,10 @@ class TaskGenerator
|
||||
foreach ($schemas as $schema) {
|
||||
$existing = $this->taskRepository->findOneBy(['schema' => $schema]);
|
||||
if (!$existing) {
|
||||
$occ = new Task();
|
||||
$occ->setSchema($schema);
|
||||
$occ->setDate(null);
|
||||
$this->em->persist($occ);
|
||||
$task = new Task();
|
||||
$task->setSchema($schema);
|
||||
$task->setDate(null);
|
||||
$this->em->persist($task);
|
||||
$hasNew = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,18 +2,25 @@
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\DTO\Request\ToggleRequest;
|
||||
use App\DTO\Request\UpdateTaskRequest;
|
||||
use App\DTO\Response\TaskResponse;
|
||||
use App\Entity\Task;
|
||||
use App\DTO\Response\ToggleResponse;
|
||||
use App\Entity\TaskSchema;
|
||||
use App\Enum\TaskSchemaStatus;
|
||||
use App\Enum\TaskStatus;
|
||||
use App\Entity\Task;
|
||||
use App\Repository\CategoryRepository;
|
||||
use App\Repository\TaskRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
|
||||
class TaskManager
|
||||
{
|
||||
public function __construct(
|
||||
private EntityManagerInterface $em,
|
||||
private CategoryRepository $categoryRepository,
|
||||
private TaskRepository $taskRepository,
|
||||
private TaskSerializer $taskSerializer,
|
||||
) {}
|
||||
|
||||
@@ -39,6 +46,29 @@ class TaskManager
|
||||
return $this->taskSerializer->serializeTask($task, new \DateTimeImmutable('today'));
|
||||
}
|
||||
|
||||
public function toggleTaskStatus(TaskSchema $schema, ToggleRequest $request): ToggleResponse
|
||||
{
|
||||
if ($schema->getStatus() === TaskSchemaStatus::Inactive) {
|
||||
throw new HttpException(422, 'Inaktive Aufgaben können nicht umgeschaltet werden.');
|
||||
}
|
||||
|
||||
$task = $request->date !== null
|
||||
? $this->taskRepository->findBySchemaAndDate($schema, new \DateTime($request->date))
|
||||
: $this->taskRepository->findOneBy(['schema' => $schema, 'date' => null]);
|
||||
|
||||
if (!$task) {
|
||||
throw new HttpException(404, 'Task nicht gefunden.');
|
||||
}
|
||||
|
||||
$newStatus = $task->getStatus() === TaskStatus::Active
|
||||
? TaskStatus::Completed
|
||||
: TaskStatus::Active;
|
||||
$task->setStatus($newStatus);
|
||||
$this->em->flush();
|
||||
|
||||
return new ToggleResponse(completed: $newStatus === TaskStatus::Completed);
|
||||
}
|
||||
|
||||
public function deleteTask(Task $task): void
|
||||
{
|
||||
$this->em->remove($task);
|
||||
|
||||
@@ -3,53 +3,27 @@
|
||||
namespace App\Service;
|
||||
|
||||
use App\DTO\Request\CreateSchemaRequest;
|
||||
use App\DTO\Request\ToggleRequest;
|
||||
use App\DTO\Request\UpdateSchemaRequest;
|
||||
use App\DTO\Response\ToggleResponse;
|
||||
use App\Entity\TaskSchema;
|
||||
use App\Enum\TaskSchemaStatus;
|
||||
use App\Enum\TaskSchemaType;
|
||||
use App\Enum\TaskStatus;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
use App\Repository\CategoryRepository;
|
||||
use App\Repository\TaskRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
class TaskSchemaManager
|
||||
{
|
||||
public function __construct(
|
||||
private EntityManagerInterface $em,
|
||||
private CategoryRepository $categoryRepository,
|
||||
private TaskRepository $taskRepository,
|
||||
private TaskSynchronizer $taskSynchronizer,
|
||||
) {}
|
||||
|
||||
public function createSchema(CreateSchemaRequest $request): TaskSchema
|
||||
{
|
||||
$schema = new TaskSchema();
|
||||
|
||||
$schema->setName($request->name ?? '');
|
||||
|
||||
if ($request->status !== null) {
|
||||
$status = TaskSchemaStatus::tryFrom($request->status);
|
||||
if ($status !== null) {
|
||||
$schema->setStatus($status);
|
||||
}
|
||||
}
|
||||
|
||||
if ($request->taskType !== null) {
|
||||
$taskType = TaskSchemaType::tryFrom($request->taskType);
|
||||
if ($taskType !== null) {
|
||||
$schema->setTaskType($taskType);
|
||||
}
|
||||
}
|
||||
|
||||
$schema->setDeadline($request->deadline !== null ? new \DateTime($request->deadline) : null);
|
||||
$schema->setStartDate($request->startDate !== null ? new \DateTime($request->startDate) : null);
|
||||
$schema->setEndDate($request->endDate !== null ? new \DateTime($request->endDate) : null);
|
||||
$schema->setWeekdays($request->weekdays);
|
||||
$schema->setMonthDays($request->monthDays);
|
||||
$schema->setYearDays($request->yearDays);
|
||||
|
||||
$this->applyFields($schema, $request);
|
||||
$this->resolveCategory($schema, $request);
|
||||
$this->applyDefaults($schema);
|
||||
|
||||
@@ -65,6 +39,19 @@ class TaskSchemaManager
|
||||
$schema->setName($request->name);
|
||||
}
|
||||
|
||||
$this->applyFields($schema, $request);
|
||||
$this->resolveCategory($schema, $request);
|
||||
$this->applyDefaults($schema);
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
$this->taskSynchronizer->syncForSchema($schema);
|
||||
|
||||
return $schema;
|
||||
}
|
||||
|
||||
private function applyFields(TaskSchema $schema, CreateSchemaRequest|UpdateSchemaRequest $request): void
|
||||
{
|
||||
if ($request->status !== null) {
|
||||
$status = TaskSchemaStatus::tryFrom($request->status);
|
||||
if ($status !== null) {
|
||||
@@ -85,40 +72,6 @@ class TaskSchemaManager
|
||||
$schema->setWeekdays($request->weekdays);
|
||||
$schema->setMonthDays($request->monthDays);
|
||||
$schema->setYearDays($request->yearDays);
|
||||
|
||||
$this->resolveCategory($schema, $request);
|
||||
$this->applyDefaults($schema);
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
// Sync: delete what no longer fits, create what's missing
|
||||
$this->taskSynchronizer->syncForSchema($schema);
|
||||
|
||||
return $schema;
|
||||
}
|
||||
|
||||
public function toggleTaskStatus(TaskSchema $schema, ToggleRequest $request): ToggleResponse
|
||||
{
|
||||
if ($schema->getStatus() === TaskSchemaStatus::Inactive) {
|
||||
throw new HttpException(422, 'Inaktive Aufgaben können nicht umgeschaltet werden.');
|
||||
}
|
||||
|
||||
// Find the task
|
||||
$task = $request->date !== null
|
||||
? $this->taskRepository->findByTaskAndDate($schema, new \DateTime($request->date))
|
||||
: $this->taskRepository->findOneBy(['schema' => $schema, 'date' => null]);
|
||||
|
||||
if (!$task) {
|
||||
throw new HttpException(404, 'Task nicht gefunden.');
|
||||
}
|
||||
|
||||
$newStatus = $task->getStatus() === TaskStatus::Active
|
||||
? TaskStatus::Completed
|
||||
: TaskStatus::Active;
|
||||
$task->setStatus($newStatus);
|
||||
$this->em->flush();
|
||||
|
||||
return new ToggleResponse(completed: $newStatus === TaskStatus::Completed);
|
||||
}
|
||||
|
||||
private function resolveCategory(TaskSchema $schema, UpdateSchemaRequest|CreateSchemaRequest $request): void
|
||||
|
||||
@@ -19,74 +19,106 @@ class TaskSynchronizer
|
||||
public function syncForSchema(TaskSchema $schema): void
|
||||
{
|
||||
$today = new \DateTimeImmutable('today');
|
||||
$end = $this->calculateSyncEnd($schema, $today);
|
||||
|
||||
// Range: bis endDate oder mindestens +6 Tage
|
||||
$deadlines = $this->deadlineCalculator->getDeadlinesForRange($schema, $today, $end);
|
||||
$shouldExist = [];
|
||||
foreach ($deadlines as $deadline) {
|
||||
$shouldExist[$deadline->format('Y-m-d')] = true;
|
||||
}
|
||||
|
||||
$existingByDate = $this->loadExistingByDate($schema, $today);
|
||||
|
||||
$this->removeObsoleteTasks($existingByDate, $shouldExist);
|
||||
$this->handleNullDateTasks($schema);
|
||||
$this->resetFutureOverrides($existingByDate);
|
||||
$this->createMissingTasks($schema, $deadlines, $existingByDate);
|
||||
|
||||
$this->em->flush();
|
||||
}
|
||||
|
||||
private function calculateSyncEnd(TaskSchema $schema, \DateTimeImmutable $today): \DateTimeImmutable
|
||||
{
|
||||
$minEnd = $today->modify('+6 days');
|
||||
$end = $schema->getEndDate()
|
||||
? new \DateTimeImmutable($schema->getEndDate()->format('Y-m-d'))
|
||||
: $minEnd;
|
||||
if ($end < $minEnd) {
|
||||
$end = $minEnd;
|
||||
}
|
||||
|
||||
// Soll-Termine berechnen
|
||||
$deadlines = $this->deadlineCalculator->getDeadlinesForRange($schema, $today, $end);
|
||||
$shouldExist = [];
|
||||
foreach ($deadlines as $dl) {
|
||||
$shouldExist[$dl->format('Y-m-d')] = true;
|
||||
}
|
||||
return $end < $minEnd ? $minEnd : $end;
|
||||
}
|
||||
|
||||
// Alle zukünftigen Tasks laden (mit Datum)
|
||||
$futureTasks = $this->taskRepository->findByTaskFromDate($schema, $today);
|
||||
/**
|
||||
* @return array<string, Task>
|
||||
*/
|
||||
private function loadExistingByDate(TaskSchema $schema, \DateTimeImmutable $today): array
|
||||
{
|
||||
$futureTasks = $this->taskRepository->findBySchemaFromDate($schema, $today);
|
||||
$existingByDate = [];
|
||||
foreach ($futureTasks as $occ) {
|
||||
$existingByDate[$occ->getDate()->format('Y-m-d')] = $occ;
|
||||
foreach ($futureTasks as $task) {
|
||||
$existingByDate[$task->getDate()->format('Y-m-d')] = $task;
|
||||
}
|
||||
|
||||
// Null-Datum Tasks laden
|
||||
$nullDateTasks = $this->taskRepository->findBy(['schema' => $schema, 'date' => null]);
|
||||
return $existingByDate;
|
||||
}
|
||||
|
||||
// Nicht mehr im Schema -> entfernen
|
||||
foreach ($existingByDate as $dateKey => $occ) {
|
||||
/**
|
||||
* @param array<string, Task> $existingByDate
|
||||
* @param array<string, true> $shouldExist
|
||||
*/
|
||||
private function removeObsoleteTasks(array &$existingByDate, array $shouldExist): void
|
||||
{
|
||||
foreach ($existingByDate as $dateKey => $task) {
|
||||
if (!isset($shouldExist[$dateKey])) {
|
||||
$this->em->remove($occ);
|
||||
$this->em->remove($task);
|
||||
unset($existingByDate[$dateKey]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function handleNullDateTasks(TaskSchema $schema): void
|
||||
{
|
||||
$nullDateTasks = $this->taskRepository->findBy(['schema' => $schema, 'date' => null]);
|
||||
|
||||
// Einzel ohne Deadline: null-date Task behalten
|
||||
if ($schema->getTaskType() === TaskSchemaType::Single && $schema->getDeadline() === null) {
|
||||
foreach ($nullDateTasks as $occ) {
|
||||
$occ->setName(null);
|
||||
$occ->setCategory(null);
|
||||
$occ->setCategoryOverridden(false);
|
||||
foreach ($nullDateTasks as $task) {
|
||||
$task->setName(null);
|
||||
$task->setCategory(null);
|
||||
$task->setCategoryOverridden(false);
|
||||
}
|
||||
} else {
|
||||
// Sonst null-date Tasks entfernen
|
||||
foreach ($nullDateTasks as $occ) {
|
||||
$this->em->remove($occ);
|
||||
foreach ($nullDateTasks as $task) {
|
||||
$this->em->remove($task);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Bestehende zukünftige Overrides zurücksetzen
|
||||
foreach ($existingByDate as $occ) {
|
||||
$occ->setName(null);
|
||||
$occ->setCategory(null);
|
||||
$occ->setCategoryOverridden(false);
|
||||
/**
|
||||
* @param array<string, Task> $existingByDate
|
||||
*/
|
||||
private function resetFutureOverrides(array $existingByDate): void
|
||||
{
|
||||
foreach ($existingByDate as $task) {
|
||||
$task->setName(null);
|
||||
$task->setCategory(null);
|
||||
$task->setCategoryOverridden(false);
|
||||
}
|
||||
}
|
||||
|
||||
// Fehlende Tasks erstellen
|
||||
foreach ($deadlines as $dl) {
|
||||
$dateKey = $dl->format('Y-m-d');
|
||||
/**
|
||||
* @param \DateTimeInterface[] $deadlines
|
||||
* @param array<string, Task> $existingByDate
|
||||
*/
|
||||
private function createMissingTasks(TaskSchema $schema, array $deadlines, array $existingByDate): void
|
||||
{
|
||||
foreach ($deadlines as $deadline) {
|
||||
$dateKey = $deadline->format('Y-m-d');
|
||||
if (!isset($existingByDate[$dateKey])) {
|
||||
$occ = new Task();
|
||||
$occ->setSchema($schema);
|
||||
$occ->setDate(new \DateTime($dateKey));
|
||||
$task = new Task();
|
||||
$task->setSchema($schema);
|
||||
$task->setDate(new \DateTime($dateKey));
|
||||
|
||||
$this->em->persist($occ);
|
||||
$this->em->persist($task);
|
||||
}
|
||||
}
|
||||
|
||||
$this->em->flush();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user