current init
This commit is contained in:
61
backend/src/Controller/Api/CategoryController.php
Normal file
61
backend/src/Controller/Api/CategoryController.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller\Api;
|
||||
|
||||
use App\DTO\Request\CreateCategoryRequest;
|
||||
use App\DTO\Request\UpdateCategoryRequest;
|
||||
use App\Entity\Category;
|
||||
use App\Repository\CategoryRepository;
|
||||
use App\Service\CategoryManager;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
#[Route('/api/categories', name: 'categories.')]
|
||||
class CategoryController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private CategoryRepository $categoryRepository,
|
||||
private CategoryManager $categoryManager,
|
||||
) {}
|
||||
|
||||
#[Route('', name: 'index', methods: ['GET'])]
|
||||
public function index(): JsonResponse
|
||||
{
|
||||
$categories = $this->categoryRepository->findAll();
|
||||
|
||||
return $this->json($categories, context: ['groups' => ['category:read']]);
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: 'show', methods: ['GET'])]
|
||||
public function show(Category $category): JsonResponse
|
||||
{
|
||||
return $this->json($category, context: ['groups' => ['category:read']]);
|
||||
}
|
||||
|
||||
#[Route('', name: 'create', methods: ['POST'])]
|
||||
public function create(#[MapRequestPayload] CreateCategoryRequest $dto): JsonResponse
|
||||
{
|
||||
$category = $this->categoryManager->createCategory($dto);
|
||||
|
||||
return $this->json($category, Response::HTTP_CREATED, context: ['groups' => ['category:read']]);
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: 'update', methods: ['PUT'])]
|
||||
public function update(#[MapRequestPayload] UpdateCategoryRequest $dto, Category $category): JsonResponse
|
||||
{
|
||||
$category = $this->categoryManager->updateCategory($category, $dto);
|
||||
|
||||
return $this->json($category, context: ['groups' => ['category:read']]);
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: 'delete', methods: ['DELETE'])]
|
||||
public function delete(Category $category): Response
|
||||
{
|
||||
$this->categoryManager->deleteCategory($category);
|
||||
|
||||
return new Response(status: Response::HTTP_NO_CONTENT);
|
||||
}
|
||||
}
|
||||
46
backend/src/Controller/Api/TaskController.php
Normal file
46
backend/src/Controller/Api/TaskController.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller\Api;
|
||||
|
||||
use App\DTO\Request\UpdateTaskRequest;
|
||||
use App\Entity\Task;
|
||||
use App\Service\TaskManager;
|
||||
use App\Service\TaskSerializer;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
#[Route('/api/tasks', name: 'tasks.')]
|
||||
class TaskController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private TaskManager $taskManager,
|
||||
private TaskSerializer $taskSerializer,
|
||||
) {}
|
||||
|
||||
#[Route('/{id}', name: 'show', methods: ['GET'])]
|
||||
public function show(Task $task): JsonResponse
|
||||
{
|
||||
$response = $this->taskSerializer->serializeTask($task);
|
||||
|
||||
return $this->json($response);
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: 'update', methods: ['PUT'])]
|
||||
public function update(#[MapRequestPayload] UpdateTaskRequest $dto, Task $task): JsonResponse
|
||||
{
|
||||
$result = $this->taskManager->updateTask($task, $dto);
|
||||
|
||||
return $this->json($result);
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: 'delete', methods: ['DELETE'])]
|
||||
public function delete(Task $task): Response
|
||||
{
|
||||
$this->taskManager->deleteTask($task);
|
||||
|
||||
return new Response(status: Response::HTTP_NO_CONTENT);
|
||||
}
|
||||
}
|
||||
96
backend/src/Controller/Api/TaskSchemaController.php
Normal file
96
backend/src/Controller/Api/TaskSchemaController.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller\Api;
|
||||
|
||||
use App\DTO\Request\CreateSchemaRequest;
|
||||
use App\DTO\Request\ToggleRequest;
|
||||
use App\DTO\Request\UpdateSchemaRequest;
|
||||
use App\Entity\TaskSchema;
|
||||
use App\Repository\TaskSchemaRepository;
|
||||
use App\Service\TaskSchemaManager;
|
||||
use App\Service\TaskViewBuilder;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
#[Route('/api/schemas')]
|
||||
class TaskSchemaController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private TaskSchemaRepository $schemaRepository,
|
||||
private TaskSchemaManager $schemaManager,
|
||||
private TaskViewBuilder $taskViewBuilder,
|
||||
) {}
|
||||
|
||||
#[Route('', name: 'api_schemas_index', methods: ['GET'])]
|
||||
public function index(): JsonResponse
|
||||
{
|
||||
$schemas = $this->schemaRepository->findAll();
|
||||
|
||||
return $this->json($schemas, context: ['groups' => ['schema:read', 'category:read']]);
|
||||
}
|
||||
|
||||
#[Route('/week', name: 'api_schemas_week', methods: ['GET'])]
|
||||
public function week(Request $request): JsonResponse
|
||||
{
|
||||
$startParam = $request->query->get('start');
|
||||
$start = $startParam ? new \DateTimeImmutable($startParam) : new \DateTimeImmutable('today');
|
||||
|
||||
return $this->json($this->taskViewBuilder->buildWeekView($start));
|
||||
}
|
||||
|
||||
#[Route('/all', name: 'api_schemas_all', methods: ['GET'])]
|
||||
public function allSchemas(): JsonResponse
|
||||
{
|
||||
$schemas = $this->schemaRepository->findBy([], ['createdAt' => 'DESC']);
|
||||
|
||||
return $this->json($schemas, context: ['groups' => ['schema:read', 'category:read']]);
|
||||
}
|
||||
|
||||
#[Route('/all-tasks', name: 'api_schemas_all_tasks', methods: ['GET'])]
|
||||
public function allTasks(): JsonResponse
|
||||
{
|
||||
return $this->json($this->taskViewBuilder->buildAllTasksView());
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: 'api_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'])]
|
||||
public function create(#[MapRequestPayload] CreateSchemaRequest $dto): JsonResponse
|
||||
{
|
||||
$schema = $this->schemaManager->createSchema($dto);
|
||||
|
||||
return $this->json($schema, Response::HTTP_CREATED, context: ['groups' => ['schema:read', 'category:read']]);
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: 'api_schemas_update', methods: ['PUT'])]
|
||||
public function update(#[MapRequestPayload] UpdateSchemaRequest $dto, TaskSchema $schema): JsonResponse
|
||||
{
|
||||
$schema = $this->schemaManager->updateSchema($schema, $dto);
|
||||
|
||||
return $this->json($schema, context: ['groups' => ['schema:read', 'category:read']]);
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: 'api_schemas_delete', methods: ['DELETE'])]
|
||||
public function delete(TaskSchema $schema): JsonResponse
|
||||
{
|
||||
$this->schemaManager->deleteSchema($schema);
|
||||
|
||||
return $this->json(null, Response::HTTP_NO_CONTENT);
|
||||
}
|
||||
|
||||
#[Route('/{id}/toggle', name: 'api_schemas_toggle', methods: ['PATCH'])]
|
||||
public function toggle(TaskSchema $schema, #[MapRequestPayload] ToggleRequest $dto): JsonResponse
|
||||
{
|
||||
$result = $this->schemaManager->toggleTaskStatus($schema, $dto);
|
||||
|
||||
return $this->json($result);
|
||||
}
|
||||
}
|
||||
16
backend/src/DTO/Request/CreateCategoryRequest.php
Normal file
16
backend/src/DTO/Request/CreateCategoryRequest.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\DTO\Request;
|
||||
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
class CreateCategoryRequest
|
||||
{
|
||||
#[Assert\NotBlank]
|
||||
#[Assert\Length(max: 255)]
|
||||
public string $name;
|
||||
|
||||
#[Assert\NotBlank]
|
||||
#[Assert\CssColor(formats: Assert\CssColor::HEX_LONG)]
|
||||
public string $color;
|
||||
}
|
||||
59
backend/src/DTO/Request/CreateSchemaRequest.php
Normal file
59
backend/src/DTO/Request/CreateSchemaRequest.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\DTO\Request;
|
||||
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
|
||||
class CreateSchemaRequest
|
||||
{
|
||||
#[Assert\NotBlank]
|
||||
#[Assert\Length(max: 255)]
|
||||
public ?string $name = null;
|
||||
|
||||
public ?int $categoryId = null;
|
||||
public ?string $status = null;
|
||||
public ?string $taskType = null;
|
||||
public ?string $deadline = null;
|
||||
public ?string $startDate = null;
|
||||
public ?string $endDate = null;
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
8
backend/src/DTO/Request/ToggleRequest.php
Normal file
8
backend/src/DTO/Request/ToggleRequest.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\DTO\Request;
|
||||
|
||||
class ToggleRequest
|
||||
{
|
||||
public ?string $date = null;
|
||||
}
|
||||
14
backend/src/DTO/Request/UpdateCategoryRequest.php
Normal file
14
backend/src/DTO/Request/UpdateCategoryRequest.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace App\DTO\Request;
|
||||
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
class UpdateCategoryRequest
|
||||
{
|
||||
#[Assert\Length(min: 1, max: 255)]
|
||||
public ?string $name = null;
|
||||
|
||||
#[Assert\CssColor(formats: Assert\CssColor::HEX_LONG)]
|
||||
public ?string $color = null;
|
||||
}
|
||||
60
backend/src/DTO/Request/UpdateSchemaRequest.php
Normal file
60
backend/src/DTO/Request/UpdateSchemaRequest.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\DTO\Request;
|
||||
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
|
||||
class UpdateSchemaRequest
|
||||
{
|
||||
#[Assert\NotBlank]
|
||||
#[Assert\Length(max: 255)]
|
||||
public ?string $name = null;
|
||||
|
||||
public ?int $categoryId = null;
|
||||
public bool $hasCategoryId = false;
|
||||
public ?string $status = null;
|
||||
public ?string $taskType = null;
|
||||
public ?string $deadline = null;
|
||||
public ?string $startDate = null;
|
||||
public ?string $endDate = null;
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
backend/src/DTO/Request/UpdateTaskRequest.php
Normal file
11
backend/src/DTO/Request/UpdateTaskRequest.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\DTO\Request;
|
||||
|
||||
class UpdateTaskRequest
|
||||
{
|
||||
public ?string $name = null;
|
||||
public ?int $categoryId = null;
|
||||
public ?string $status = null;
|
||||
public ?string $date = null;
|
||||
}
|
||||
12
backend/src/DTO/Response/CategoryResponse.php
Normal file
12
backend/src/DTO/Response/CategoryResponse.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace App\DTO\Response;
|
||||
|
||||
class CategoryResponse
|
||||
{
|
||||
public function __construct(
|
||||
public readonly int $id,
|
||||
public readonly string $name,
|
||||
public readonly string $color,
|
||||
) {}
|
||||
}
|
||||
12
backend/src/DTO/Response/DayResponse.php
Normal file
12
backend/src/DTO/Response/DayResponse.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace App\DTO\Response;
|
||||
|
||||
class DayResponse
|
||||
{
|
||||
public function __construct(
|
||||
public readonly string $date,
|
||||
/** @var TaskResponse[] */
|
||||
public readonly array $tasks,
|
||||
) {}
|
||||
}
|
||||
18
backend/src/DTO/Response/TaskResponse.php
Normal file
18
backend/src/DTO/Response/TaskResponse.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\DTO\Response;
|
||||
|
||||
class TaskResponse
|
||||
{
|
||||
public function __construct(
|
||||
public readonly int $schemaId,
|
||||
public readonly int $taskId,
|
||||
public readonly string $name,
|
||||
public readonly string $status,
|
||||
public readonly string $taskType,
|
||||
public readonly ?string $date,
|
||||
public readonly ?string $deadline,
|
||||
public readonly bool $isPast,
|
||||
public readonly ?CategoryResponse $category,
|
||||
) {}
|
||||
}
|
||||
10
backend/src/DTO/Response/ToggleResponse.php
Normal file
10
backend/src/DTO/Response/ToggleResponse.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\DTO\Response;
|
||||
|
||||
class ToggleResponse
|
||||
{
|
||||
public function __construct(
|
||||
public readonly bool $completed,
|
||||
) {}
|
||||
}
|
||||
13
backend/src/DTO/Response/WeekViewResponse.php
Normal file
13
backend/src/DTO/Response/WeekViewResponse.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\DTO\Response;
|
||||
|
||||
class WeekViewResponse
|
||||
{
|
||||
public function __construct(
|
||||
/** @var TaskResponse[] */
|
||||
public readonly array $tasksWithoutDeadline,
|
||||
/** @var DayResponse[] */
|
||||
public readonly array $days,
|
||||
) {}
|
||||
}
|
||||
52
backend/src/Entity/Category.php
Normal file
52
backend/src/Entity/Category.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use App\Repository\CategoryRepository;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Serializer\Attribute\Groups;
|
||||
|
||||
#[ORM\Entity(repositoryClass: CategoryRepository::class)]
|
||||
class Category
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column]
|
||||
#[Groups(['category:read', 'task:read'])]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\Column(length: 255)]
|
||||
#[Groups(['category:read', 'category:write', 'task:read'])]
|
||||
private ?string $name = null;
|
||||
|
||||
#[ORM\Column(length: 7)]
|
||||
#[Groups(['category:read', 'category:write', 'task:read'])]
|
||||
private ?string $color = null;
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getName(): ?string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function setName(string $name): static
|
||||
{
|
||||
$this->name = $name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getColor(): ?string
|
||||
{
|
||||
return $this->color;
|
||||
}
|
||||
|
||||
public function setColor(string $color): static
|
||||
{
|
||||
$this->color = $color;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
138
backend/src/Entity/Task.php
Normal file
138
backend/src/Entity/Task.php
Normal file
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use App\Enum\TaskStatus;
|
||||
use App\Repository\TaskRepository;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
#[ORM\Entity(repositoryClass: TaskRepository::class)]
|
||||
#[ORM\UniqueConstraint(columns: ['task_id', 'date'])]
|
||||
class Task
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->createdAt = new \DateTime();
|
||||
}
|
||||
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\ManyToOne]
|
||||
#[ORM\JoinColumn(name: 'task_id', nullable: false, onDelete: 'CASCADE')]
|
||||
private TaskSchema $schema;
|
||||
|
||||
#[ORM\Column(length: 255, nullable: true)]
|
||||
private ?string $name = null;
|
||||
|
||||
#[ORM\ManyToOne]
|
||||
#[ORM\JoinColumn(nullable: true, onDelete: 'SET NULL')]
|
||||
private ?Category $category = null;
|
||||
|
||||
#[ORM\Column]
|
||||
private bool $categoryOverridden = false;
|
||||
|
||||
#[ORM\Column(type: Types::DATE_MUTABLE, nullable: true)]
|
||||
private ?\DateTimeInterface $date = null;
|
||||
|
||||
#[ORM\Column(length: 20, enumType: TaskStatus::class)]
|
||||
private TaskStatus $status = TaskStatus::Active;
|
||||
|
||||
#[ORM\Column(type: Types::DATETIME_MUTABLE)]
|
||||
private \DateTimeInterface $createdAt;
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getSchema(): TaskSchema
|
||||
{
|
||||
return $this->schema;
|
||||
}
|
||||
|
||||
public function setSchema(TaskSchema $schema): static
|
||||
{
|
||||
$this->schema = $schema;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getName(): ?string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function setName(?string $name): static
|
||||
{
|
||||
$this->name = $name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCategory(): ?Category
|
||||
{
|
||||
return $this->category;
|
||||
}
|
||||
|
||||
public function setCategory(?Category $category): static
|
||||
{
|
||||
$this->category = $category;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function isCategoryOverridden(): bool
|
||||
{
|
||||
return $this->categoryOverridden;
|
||||
}
|
||||
|
||||
public function setCategoryOverridden(bool $categoryOverridden): static
|
||||
{
|
||||
$this->categoryOverridden = $categoryOverridden;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getEffectiveName(): string
|
||||
{
|
||||
return $this->name ?? $this->schema->getName();
|
||||
}
|
||||
|
||||
public function getEffectiveCategory(): ?Category
|
||||
{
|
||||
return $this->categoryOverridden ? $this->category : $this->schema->getCategory();
|
||||
}
|
||||
|
||||
public function getDate(): ?\DateTimeInterface
|
||||
{
|
||||
return $this->date;
|
||||
}
|
||||
|
||||
public function setDate(?\DateTimeInterface $date): static
|
||||
{
|
||||
$this->date = $date;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getStatus(): TaskStatus
|
||||
{
|
||||
return $this->status;
|
||||
}
|
||||
|
||||
public function setStatus(TaskStatus $status): static
|
||||
{
|
||||
$this->status = $status;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCreatedAt(): \DateTimeInterface
|
||||
{
|
||||
return $this->createdAt;
|
||||
}
|
||||
|
||||
public function setCreatedAt(\DateTimeInterface $createdAt): static
|
||||
{
|
||||
$this->createdAt = $createdAt;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
190
backend/src/Entity/TaskSchema.php
Normal file
190
backend/src/Entity/TaskSchema.php
Normal file
@@ -0,0 +1,190 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use App\Enum\TaskSchemaStatus;
|
||||
use App\Enum\TaskSchemaType;
|
||||
use App\Repository\TaskSchemaRepository;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Serializer\Attribute\Groups;
|
||||
|
||||
#[ORM\Entity(repositoryClass: TaskSchemaRepository::class)]
|
||||
class TaskSchema
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column]
|
||||
#[Groups(['schema:read'])]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\Column(length: 255)]
|
||||
#[Groups(['schema:read', 'schema:write'])]
|
||||
private ?string $name = null;
|
||||
|
||||
#[ORM\Column(length: 20, enumType: TaskSchemaStatus::class)]
|
||||
#[Groups(['schema:read', 'schema:write'])]
|
||||
private TaskSchemaStatus $status = TaskSchemaStatus::Active;
|
||||
|
||||
#[ORM\Column(length: 20, enumType: TaskSchemaType::class)]
|
||||
#[Groups(['schema:read', 'schema:write'])]
|
||||
private TaskSchemaType $taskType = TaskSchemaType::Single;
|
||||
|
||||
#[ORM\ManyToOne]
|
||||
#[ORM\JoinColumn(onDelete: 'SET NULL')]
|
||||
#[Groups(['schema:read'])]
|
||||
private ?Category $category = null;
|
||||
|
||||
#[ORM\Column(type: Types::DATE_MUTABLE, nullable: true)]
|
||||
#[Groups(['schema:read', 'schema:write'])]
|
||||
private ?\DateTimeInterface $deadline = null;
|
||||
|
||||
#[ORM\Column(type: Types::DATE_MUTABLE, nullable: true)]
|
||||
#[Groups(['schema:read', 'schema:write'])]
|
||||
private ?\DateTimeInterface $startDate = null;
|
||||
|
||||
#[ORM\Column(type: Types::DATE_MUTABLE, nullable: true)]
|
||||
#[Groups(['schema:read', 'schema:write'])]
|
||||
private ?\DateTimeInterface $endDate = null;
|
||||
|
||||
#[ORM\Column(type: Types::JSON, nullable: true)]
|
||||
#[Groups(['schema:read', 'schema:write'])]
|
||||
private ?array $weekdays = null;
|
||||
|
||||
#[ORM\Column(type: Types::JSON, nullable: true)]
|
||||
#[Groups(['schema:read', 'schema:write'])]
|
||||
private ?array $monthDays = null;
|
||||
|
||||
#[ORM\Column(type: Types::JSON, nullable: true)]
|
||||
#[Groups(['schema:read', 'schema:write'])]
|
||||
private ?array $yearDays = null;
|
||||
|
||||
#[ORM\Column(type: Types::DATETIME_MUTABLE)]
|
||||
#[Groups(['schema:read'])]
|
||||
private \DateTimeInterface $createdAt;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->createdAt = new \DateTime();
|
||||
}
|
||||
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getName(): ?string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function setName(string $name): static
|
||||
{
|
||||
$this->name = $name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getStatus(): TaskSchemaStatus
|
||||
{
|
||||
return $this->status;
|
||||
}
|
||||
|
||||
public function setStatus(TaskSchemaStatus $status): static
|
||||
{
|
||||
$this->status = $status;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getTaskType(): TaskSchemaType
|
||||
{
|
||||
return $this->taskType;
|
||||
}
|
||||
|
||||
public function setTaskType(TaskSchemaType $taskType): static
|
||||
{
|
||||
$this->taskType = $taskType;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCategory(): ?Category
|
||||
{
|
||||
return $this->category;
|
||||
}
|
||||
|
||||
public function setCategory(?Category $category): static
|
||||
{
|
||||
$this->category = $category;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDeadline(): ?\DateTimeInterface
|
||||
{
|
||||
return $this->deadline;
|
||||
}
|
||||
|
||||
public function setDeadline(?\DateTimeInterface $deadline): static
|
||||
{
|
||||
$this->deadline = $deadline;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getStartDate(): ?\DateTimeInterface
|
||||
{
|
||||
return $this->startDate;
|
||||
}
|
||||
|
||||
public function setStartDate(?\DateTimeInterface $startDate): static
|
||||
{
|
||||
$this->startDate = $startDate;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getEndDate(): ?\DateTimeInterface
|
||||
{
|
||||
return $this->endDate;
|
||||
}
|
||||
|
||||
public function setEndDate(?\DateTimeInterface $endDate): static
|
||||
{
|
||||
$this->endDate = $endDate;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getWeekdays(): ?array
|
||||
{
|
||||
return $this->weekdays;
|
||||
}
|
||||
|
||||
public function setWeekdays(?array $weekdays): static
|
||||
{
|
||||
$this->weekdays = $weekdays;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getMonthDays(): ?array
|
||||
{
|
||||
return $this->monthDays;
|
||||
}
|
||||
|
||||
public function setMonthDays(?array $monthDays): static
|
||||
{
|
||||
$this->monthDays = $monthDays;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getYearDays(): ?array
|
||||
{
|
||||
return $this->yearDays;
|
||||
}
|
||||
|
||||
public function setYearDays(?array $yearDays): static
|
||||
{
|
||||
$this->yearDays = $yearDays;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCreatedAt(): \DateTimeInterface
|
||||
{
|
||||
return $this->createdAt;
|
||||
}
|
||||
}
|
||||
10
backend/src/Enum/TaskSchemaStatus.php
Normal file
10
backend/src/Enum/TaskSchemaStatus.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Enum;
|
||||
|
||||
enum TaskSchemaStatus: string
|
||||
{
|
||||
case Active = 'aktiv';
|
||||
case Completed = 'erledigt';
|
||||
case Inactive = 'inaktiv';
|
||||
}
|
||||
13
backend/src/Enum/TaskSchemaType.php
Normal file
13
backend/src/Enum/TaskSchemaType.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Enum;
|
||||
|
||||
enum TaskSchemaType: string
|
||||
{
|
||||
case Single = 'einzel';
|
||||
case Daily = 'taeglich';
|
||||
case Multi = 'multi';
|
||||
case Weekly = 'woechentlich';
|
||||
case Monthly = 'monatlich';
|
||||
case Yearly = 'jaehrlich';
|
||||
}
|
||||
9
backend/src/Enum/TaskStatus.php
Normal file
9
backend/src/Enum/TaskStatus.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace App\Enum;
|
||||
|
||||
enum TaskStatus: string
|
||||
{
|
||||
case Active = 'aktiv';
|
||||
case Completed = 'erledigt';
|
||||
}
|
||||
18
backend/src/Repository/CategoryRepository.php
Normal file
18
backend/src/Repository/CategoryRepository.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repository;
|
||||
|
||||
use App\Entity\Category;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @extends ServiceEntityRepository<Category>
|
||||
*/
|
||||
class CategoryRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, Category::class);
|
||||
}
|
||||
}
|
||||
149
backend/src/Repository/TaskRepository.php
Normal file
149
backend/src/Repository/TaskRepository.php
Normal file
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repository;
|
||||
|
||||
use App\Entity\Task;
|
||||
use App\Entity\TaskSchema;
|
||||
use App\Enum\TaskStatus;
|
||||
use App\Enum\TaskSchemaStatus;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @extends ServiceEntityRepository<Task>
|
||||
*/
|
||||
class TaskRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, Task::class);
|
||||
}
|
||||
|
||||
public function findByTaskAndDate(TaskSchema $task, \DateTimeInterface $date): ?Task
|
||||
{
|
||||
return $this->createQueryBuilder('o')
|
||||
->where('o.schema = :task')
|
||||
->andWhere('o.date = :date')
|
||||
->setParameter('task', $task)
|
||||
->setParameter('date', $date)
|
||||
->getQuery()
|
||||
->getOneOrNullResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Task[]
|
||||
*/
|
||||
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')
|
||||
->setParameter('from', $from)
|
||||
->setParameter('to', $to)
|
||||
->setParameter('excluded', TaskSchemaStatus::Inactive)
|
||||
->orderBy('o.date', 'ASC')
|
||||
->getQuery()
|
||||
->getResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, true> Set of "taskId-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')
|
||||
->setParameter('from', $from)
|
||||
->setParameter('to', $to)
|
||||
->getQuery()
|
||||
->getArrayResult();
|
||||
|
||||
$set = [];
|
||||
foreach ($rows as $row) {
|
||||
$date = $row['date'] instanceof \DateTimeInterface
|
||||
? $row['date']->format('Y-m-d')
|
||||
: $row['date'];
|
||||
$set[$row['schemaId'] . '-' . $date] = true;
|
||||
}
|
||||
|
||||
return $set;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Task[]
|
||||
*/
|
||||
public function findByTaskFromDate(TaskSchema $task, \DateTimeInterface $fromDate): array
|
||||
{
|
||||
return $this->createQueryBuilder('o')
|
||||
->where('o.schema = :task')
|
||||
->andWhere('o.date >= :from')
|
||||
->setParameter('task', $task)
|
||||
->setParameter('from', $fromDate)
|
||||
->getQuery()
|
||||
->getResult();
|
||||
}
|
||||
|
||||
public function deleteFutureByTask(TaskSchema $task, \DateTimeInterface $fromDate): void
|
||||
{
|
||||
$this->createQueryBuilder('o')
|
||||
->delete()
|
||||
->where('o.schema = :task')
|
||||
->andWhere('o.date >= :from')
|
||||
->setParameter('task', $task)
|
||||
->setParameter('from', $fromDate)
|
||||
->getQuery()
|
||||
->execute();
|
||||
}
|
||||
|
||||
public function deleteFutureActive(TaskSchema $task, \DateTimeInterface $fromDate): void
|
||||
{
|
||||
$this->createQueryBuilder('o')
|
||||
->delete()
|
||||
->where('o.schema = :task')
|
||||
->andWhere('o.date >= :from')
|
||||
->andWhere('o.status = :status')
|
||||
->setParameter('task', $task)
|
||||
->setParameter('from', $fromDate)
|
||||
->setParameter('status', TaskStatus::Active)
|
||||
->getQuery()
|
||||
->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Task[]
|
||||
*/
|
||||
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')
|
||||
->getQuery()
|
||||
->getResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Task[]
|
||||
*/
|
||||
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')
|
||||
->setParameter('excluded', TaskSchemaStatus::Inactive)
|
||||
->orderBy('o.createdAt', 'DESC')
|
||||
->getQuery()
|
||||
->getResult();
|
||||
}
|
||||
}
|
||||
39
backend/src/Repository/TaskSchemaRepository.php
Normal file
39
backend/src/Repository/TaskSchemaRepository.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repository;
|
||||
|
||||
use App\Entity\TaskSchema;
|
||||
use App\Enum\TaskSchemaStatus;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @extends ServiceEntityRepository<TaskSchema>
|
||||
*/
|
||||
class TaskSchemaRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, TaskSchema::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return TaskSchema[]
|
||||
*/
|
||||
public function findActiveTasksInRange(\DateTimeInterface $from, \DateTimeInterface $to): array
|
||||
{
|
||||
return $this->createQueryBuilder('t')
|
||||
->where('t.status != :excluded')
|
||||
->andWhere(
|
||||
'(t.taskType = :einzel AND (t.deadline IS NULL OR (t.deadline >= :from AND t.deadline <= :to)))'
|
||||
. ' OR '
|
||||
. '(t.taskType != :einzel AND t.startDate <= :to AND (t.endDate IS NULL OR t.endDate >= :from))'
|
||||
)
|
||||
->setParameter('excluded', TaskSchemaStatus::Inactive)
|
||||
->setParameter('einzel', 'einzel')
|
||||
->setParameter('from', $from)
|
||||
->setParameter('to', $to)
|
||||
->getQuery()
|
||||
->getResult();
|
||||
}
|
||||
}
|
||||
43
backend/src/Service/CategoryManager.php
Normal file
43
backend/src/Service/CategoryManager.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\DTO\Request\CreateCategoryRequest;
|
||||
use App\DTO\Request\UpdateCategoryRequest;
|
||||
use App\Entity\Category;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
class CategoryManager
|
||||
{
|
||||
public function __construct(
|
||||
private EntityManagerInterface $em,
|
||||
) {}
|
||||
|
||||
public function createCategory(CreateCategoryRequest $request): Category
|
||||
{
|
||||
$category = new Category();
|
||||
$category->setName($request->name);
|
||||
$category->setColor($request->color);
|
||||
|
||||
$this->em->persist($category);
|
||||
$this->em->flush();
|
||||
|
||||
return $category;
|
||||
}
|
||||
|
||||
public function updateCategory(Category $category, UpdateCategoryRequest $request): Category
|
||||
{
|
||||
$category->setName($request->name);
|
||||
$category->setColor($request->color);
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
return $category;
|
||||
}
|
||||
|
||||
public function deleteCategory(Category $category): void
|
||||
{
|
||||
$this->em->remove($category);
|
||||
$this->em->flush();
|
||||
}
|
||||
}
|
||||
82
backend/src/Service/DeadlineCalculator.php
Normal file
82
backend/src/Service/DeadlineCalculator.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Entity\TaskSchema;
|
||||
use App\Enum\TaskSchemaType;
|
||||
|
||||
class DeadlineCalculator
|
||||
{
|
||||
/**
|
||||
* @return \DateTimeInterface[]
|
||||
*/
|
||||
public function getDeadlinesForRange(TaskSchema $schema, \DateTimeInterface $from, \DateTimeInterface $to): array
|
||||
{
|
||||
if ($schema->getTaskType() === TaskSchemaType::Single) {
|
||||
$deadline = $schema->getDeadline();
|
||||
if ($deadline === null) {
|
||||
return [];
|
||||
}
|
||||
return ($deadline >= $from && $deadline <= $to) ? [$deadline] : [];
|
||||
}
|
||||
|
||||
return $this->getRecurringDeadlines($schema, $from, $to);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \DateTimeInterface[]
|
||||
*/
|
||||
private function getRecurringDeadlines(TaskSchema $schema, \DateTimeInterface $from, \DateTimeInterface $to): array
|
||||
{
|
||||
$startDate = $schema->getStartDate();
|
||||
if ($startDate === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$effectiveFrom = $from > $startDate ? $from : $startDate;
|
||||
$endDate = $schema->getEndDate();
|
||||
$effectiveTo = $endDate !== null && $to > $endDate ? $endDate : $to;
|
||||
|
||||
if ($effectiveFrom > $effectiveTo) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$deadlines = [];
|
||||
$current = new \DateTimeImmutable($effectiveFrom->format('Y-m-d'));
|
||||
$end = new \DateTimeImmutable($effectiveTo->format('Y-m-d'));
|
||||
|
||||
while ($current <= $end) {
|
||||
if ($this->matchesType($schema, $current)) {
|
||||
$deadlines[] = $current;
|
||||
}
|
||||
$current = $current->modify('+1 day');
|
||||
}
|
||||
|
||||
return $deadlines;
|
||||
}
|
||||
|
||||
private function matchesType(TaskSchema $schema, \DateTimeImmutable $date): bool
|
||||
{
|
||||
return match ($schema->getTaskType()) {
|
||||
TaskSchemaType::Daily => true,
|
||||
TaskSchemaType::Weekly => in_array((int) $date->format('N'), $schema->getWeekdays() ?? [], true),
|
||||
TaskSchemaType::Monthly => in_array((int) $date->format('j'), $schema->getMonthDays() ?? [], true),
|
||||
TaskSchemaType::Multi, TaskSchemaType::Yearly => $this->matchesYearDays($schema, $date),
|
||||
default => false,
|
||||
};
|
||||
}
|
||||
|
||||
private function matchesYearDays(TaskSchema $schema, \DateTimeImmutable $date): bool
|
||||
{
|
||||
$month = (int) $date->format('n');
|
||||
$day = (int) $date->format('j');
|
||||
|
||||
foreach ($schema->getYearDays() ?? [] as $yd) {
|
||||
if (($yd['month'] ?? 0) === $month && ($yd['day'] ?? 0) === $day) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
75
backend/src/Service/TaskGenerator.php
Normal file
75
backend/src/Service/TaskGenerator.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Entity\Task;
|
||||
use App\Enum\TaskSchemaType;
|
||||
use App\Enum\TaskSchemaStatus;
|
||||
use App\Repository\TaskRepository;
|
||||
use App\Repository\TaskSchemaRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
class TaskGenerator
|
||||
{
|
||||
public function __construct(
|
||||
private EntityManagerInterface $em,
|
||||
private TaskSchemaRepository $schemaRepository,
|
||||
private TaskRepository $taskRepository,
|
||||
private DeadlineCalculator $deadlineCalculator,
|
||||
) {}
|
||||
|
||||
public function generateForRange(\DateTimeInterface $from, \DateTimeInterface $to): void
|
||||
{
|
||||
$schemas = $this->schemaRepository->findActiveTasksInRange($from, $to);
|
||||
$existingKeys = $this->taskRepository->getExistingKeys($from, $to);
|
||||
|
||||
$hasNew = false;
|
||||
|
||||
foreach ($schemas as $schema) {
|
||||
$deadlines = $this->deadlineCalculator->getDeadlinesForRange($schema, $from, $to);
|
||||
|
||||
foreach ($deadlines as $deadline) {
|
||||
$key = $schema->getId() . '-' . $deadline->format('Y-m-d');
|
||||
if (!isset($existingKeys[$key])) {
|
||||
$task = new Task();
|
||||
$task->setSchema($schema);
|
||||
$task->setDate(new \DateTime($deadline->format('Y-m-d')));
|
||||
|
||||
$this->em->persist($task);
|
||||
$existingKeys[$key] = true;
|
||||
$hasNew = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($hasNew) {
|
||||
$this->em->flush();
|
||||
}
|
||||
}
|
||||
|
||||
public function generateForTasksWithoutDate(): void
|
||||
{
|
||||
$schemas = $this->schemaRepository->findBy([
|
||||
'taskType' => TaskSchemaType::Single,
|
||||
'deadline' => null,
|
||||
'status' => TaskSchemaStatus::Active,
|
||||
]);
|
||||
|
||||
$hasNew = false;
|
||||
|
||||
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);
|
||||
$hasNew = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ($hasNew) {
|
||||
$this->em->flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
47
backend/src/Service/TaskManager.php
Normal file
47
backend/src/Service/TaskManager.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\DTO\Request\UpdateTaskRequest;
|
||||
use App\DTO\Response\TaskResponse;
|
||||
use App\Entity\Task;
|
||||
use App\Enum\TaskStatus;
|
||||
use App\Repository\CategoryRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
class TaskManager
|
||||
{
|
||||
public function __construct(
|
||||
private EntityManagerInterface $em,
|
||||
private CategoryRepository $categoryRepository,
|
||||
private TaskSerializer $taskSerializer,
|
||||
) {}
|
||||
|
||||
public function updateTask(Task $task, UpdateTaskRequest $request): TaskResponse
|
||||
{
|
||||
$task->setName($request->name);
|
||||
|
||||
$category = $request->categoryId !== null
|
||||
? $this->categoryRepository->find($request->categoryId)
|
||||
: null;
|
||||
$task->setCategory($category);
|
||||
$task->setCategoryOverridden(true);
|
||||
|
||||
$status = TaskStatus::tryFrom($request->status);
|
||||
if ($status !== null) {
|
||||
$task->setStatus($status);
|
||||
}
|
||||
|
||||
$task->setDate($request->date ? new \DateTime($request->date) : null);
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
return $this->taskSerializer->serializeTask($task, new \DateTimeImmutable('today'));
|
||||
}
|
||||
|
||||
public function deleteTask(Task $task): void
|
||||
{
|
||||
$this->em->remove($task);
|
||||
$this->em->flush();
|
||||
}
|
||||
}
|
||||
155
backend/src/Service/TaskSchemaManager.php
Normal file
155
backend/src/Service/TaskSchemaManager.php
Normal file
@@ -0,0 +1,155 @@
|
||||
<?php
|
||||
|
||||
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->resolveCategory($schema, $request);
|
||||
$this->applyDefaults($schema);
|
||||
|
||||
$this->em->persist($schema);
|
||||
$this->em->flush();
|
||||
|
||||
return $schema;
|
||||
}
|
||||
|
||||
public function updateSchema(TaskSchema $schema, UpdateSchemaRequest $request): TaskSchema
|
||||
{
|
||||
if ($request->name !== null) {
|
||||
$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->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
|
||||
{
|
||||
if ($request instanceof UpdateSchemaRequest) {
|
||||
if ($request->hasCategoryId) {
|
||||
if ($request->categoryId !== null) {
|
||||
$category = $this->categoryRepository->find($request->categoryId);
|
||||
$schema->setCategory($category);
|
||||
} else {
|
||||
$schema->setCategory(null);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if ($request->categoryId !== null) {
|
||||
$category = $this->categoryRepository->find($request->categoryId);
|
||||
$schema->setCategory($category);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function deleteSchema(TaskSchema $schema): void
|
||||
{
|
||||
$this->em->remove($schema);
|
||||
$this->em->flush();
|
||||
}
|
||||
|
||||
private function applyDefaults(TaskSchema $schema): void
|
||||
{
|
||||
if ($schema->getTaskType() !== TaskSchemaType::Single && $schema->getStartDate() === null) {
|
||||
$schema->setStartDate(new \DateTime('today'));
|
||||
}
|
||||
}
|
||||
}
|
||||
49
backend/src/Service/TaskSerializer.php
Normal file
49
backend/src/Service/TaskSerializer.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\DTO\Response\CategoryResponse;
|
||||
use App\DTO\Response\TaskResponse;
|
||||
use App\Entity\Category;
|
||||
use App\Entity\Task;
|
||||
|
||||
class TaskSerializer
|
||||
{
|
||||
public function serializeTask(Task $task, ?\DateTimeImmutable $today = null): TaskResponse
|
||||
{
|
||||
$today ??= new \DateTimeImmutable('today');
|
||||
$schema = $task->getSchema();
|
||||
$category = $task->getEffectiveCategory();
|
||||
$date = $task->getDate();
|
||||
|
||||
return new TaskResponse(
|
||||
schemaId: $schema->getId(),
|
||||
taskId: $task->getId(),
|
||||
name: $task->getEffectiveName(),
|
||||
status: $task->getStatus()->value,
|
||||
taskType: $schema->getTaskType()->value,
|
||||
date: $date?->format('Y-m-d'),
|
||||
deadline: $date?->format('Y-m-d'),
|
||||
isPast: $date !== null && $date < $today,
|
||||
category: $category !== null ? $this->serializeCategory($category) : null,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Task[] $tasks
|
||||
* @return TaskResponse[]
|
||||
*/
|
||||
public function serializeTasks(array $tasks, \DateTimeImmutable $today): array
|
||||
{
|
||||
return array_map(fn(Task $task) => $this->serializeTask($task, $today), $tasks);
|
||||
}
|
||||
|
||||
public function serializeCategory(Category $category): CategoryResponse
|
||||
{
|
||||
return new CategoryResponse(
|
||||
id: $category->getId(),
|
||||
name: $category->getName(),
|
||||
color: $category->getColor(),
|
||||
);
|
||||
}
|
||||
}
|
||||
92
backend/src/Service/TaskSynchronizer.php
Normal file
92
backend/src/Service/TaskSynchronizer.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Entity\Task;
|
||||
use App\Entity\TaskSchema;
|
||||
use App\Enum\TaskSchemaType;
|
||||
use App\Repository\TaskRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
class TaskSynchronizer
|
||||
{
|
||||
public function __construct(
|
||||
private EntityManagerInterface $em,
|
||||
private TaskRepository $taskRepository,
|
||||
private DeadlineCalculator $deadlineCalculator,
|
||||
) {}
|
||||
|
||||
public function syncForSchema(TaskSchema $schema): void
|
||||
{
|
||||
$today = new \DateTimeImmutable('today');
|
||||
|
||||
// Range: bis endDate oder mindestens +6 Tage
|
||||
$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;
|
||||
}
|
||||
|
||||
// Alle zukünftigen Tasks laden (mit Datum)
|
||||
$futureTasks = $this->taskRepository->findByTaskFromDate($schema, $today);
|
||||
$existingByDate = [];
|
||||
foreach ($futureTasks as $occ) {
|
||||
$existingByDate[$occ->getDate()->format('Y-m-d')] = $occ;
|
||||
}
|
||||
|
||||
// Null-Datum Tasks laden
|
||||
$nullDateTasks = $this->taskRepository->findBy(['schema' => $schema, 'date' => null]);
|
||||
|
||||
// Nicht mehr im Schema -> entfernen
|
||||
foreach ($existingByDate as $dateKey => $occ) {
|
||||
if (!isset($shouldExist[$dateKey])) {
|
||||
$this->em->remove($occ);
|
||||
unset($existingByDate[$dateKey]);
|
||||
}
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
} else {
|
||||
// Sonst null-date Tasks entfernen
|
||||
foreach ($nullDateTasks as $occ) {
|
||||
$this->em->remove($occ);
|
||||
}
|
||||
}
|
||||
|
||||
// Bestehende zukünftige Overrides zurücksetzen
|
||||
foreach ($existingByDate as $occ) {
|
||||
$occ->setName(null);
|
||||
$occ->setCategory(null);
|
||||
$occ->setCategoryOverridden(false);
|
||||
}
|
||||
|
||||
// Fehlende Tasks erstellen
|
||||
foreach ($deadlines as $dl) {
|
||||
$dateKey = $dl->format('Y-m-d');
|
||||
if (!isset($existingByDate[$dateKey])) {
|
||||
$occ = new Task();
|
||||
$occ->setSchema($schema);
|
||||
$occ->setDate(new \DateTime($dateKey));
|
||||
|
||||
$this->em->persist($occ);
|
||||
}
|
||||
}
|
||||
|
||||
$this->em->flush();
|
||||
}
|
||||
}
|
||||
91
backend/src/Service/TaskViewBuilder.php
Normal file
91
backend/src/Service/TaskViewBuilder.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\DTO\Response\DayResponse;
|
||||
use App\DTO\Response\TaskResponse;
|
||||
use App\DTO\Response\WeekViewResponse;
|
||||
use App\Repository\TaskRepository;
|
||||
|
||||
class TaskViewBuilder
|
||||
{
|
||||
public function __construct(
|
||||
private TaskGenerator $taskGenerator,
|
||||
private TaskRepository $taskRepository,
|
||||
private TaskSerializer $taskSerializer,
|
||||
) {}
|
||||
|
||||
public function buildWeekView(?\DateTimeImmutable $start = null): WeekViewResponse
|
||||
{
|
||||
$start = $start ?? new \DateTimeImmutable('today');
|
||||
$end = $start->modify('+6 days');
|
||||
$today = new \DateTimeImmutable('today');
|
||||
|
||||
// Generate missing tasks
|
||||
$this->taskGenerator->generateForRange($start, $end);
|
||||
$this->taskGenerator->generateForTasksWithoutDate();
|
||||
|
||||
// Load tasks with dates in range
|
||||
$tasks = $this->taskRepository->findInRange($start, $end);
|
||||
|
||||
// Build days structure
|
||||
$days = [];
|
||||
$current = $start;
|
||||
while ($current <= $end) {
|
||||
$days[$current->format('Y-m-d')] = [];
|
||||
$current = $current->modify('+1 day');
|
||||
}
|
||||
|
||||
foreach ($tasks as $task) {
|
||||
$dateKey = $task->getDate()->format('Y-m-d');
|
||||
if (isset($days[$dateKey])) {
|
||||
$days[$dateKey][] = $this->taskSerializer->serializeTask($task, $today);
|
||||
}
|
||||
}
|
||||
|
||||
// Tasks without date (einzel without deadline)
|
||||
$tasksWithoutDeadline = [];
|
||||
$noDateTasks = $this->taskRepository->findWithoutDate();
|
||||
foreach ($noDateTasks as $task) {
|
||||
$tasksWithoutDeadline[] = $this->taskSerializer->serializeTask($task, $today);
|
||||
}
|
||||
|
||||
$dayResponses = [];
|
||||
foreach ($days as $date => $dayTasks) {
|
||||
usort($dayTasks, fn(TaskResponse $a, TaskResponse $b) => strcmp($a->name, $b->name));
|
||||
$dayResponses[] = new DayResponse(
|
||||
date: $date,
|
||||
tasks: $dayTasks,
|
||||
);
|
||||
}
|
||||
|
||||
return new WeekViewResponse(
|
||||
tasksWithoutDeadline: $tasksWithoutDeadline,
|
||||
days: $dayResponses,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return TaskResponse[]
|
||||
*/
|
||||
public function buildAllTasksView(): array
|
||||
{
|
||||
$this->taskGenerator->generateForTasksWithoutDate();
|
||||
$today = new \DateTimeImmutable('today');
|
||||
$result = [];
|
||||
|
||||
// Tasks without date first
|
||||
$noDate = $this->taskRepository->findWithoutDate();
|
||||
foreach ($noDate as $task) {
|
||||
$result[] = $this->taskSerializer->serializeTask($task, $today);
|
||||
}
|
||||
|
||||
// Then all tasks with date
|
||||
$tasks = $this->taskRepository->findAllSorted();
|
||||
foreach ($tasks as $task) {
|
||||
$result[] = $this->taskSerializer->serializeTask($task, $today);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user