current init

This commit is contained in:
Marek
2026-03-30 15:42:44 +02:00
parent c5229e48ed
commit 2f96caaa23
366 changed files with 6093 additions and 11029 deletions

View 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();
}
}

View 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;
}
}

View 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();
}
}
}

View 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();
}
}

View 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'));
}
}
}

View 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(),
);
}
}

View 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();
}
}

View 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;
}
}