update
This commit is contained in:
@@ -11,22 +11,6 @@ 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) {
|
||||
@@ -59,21 +43,30 @@ class DeadlineCalculator
|
||||
{
|
||||
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),
|
||||
TaskSchemaType::Custom => $this->matchesDays($schema, $date),
|
||||
default => false,
|
||||
};
|
||||
}
|
||||
|
||||
private function matchesYearDays(TaskSchema $schema, \DateTimeImmutable $date): bool
|
||||
private function matchesDays(TaskSchema $schema, \DateTimeImmutable $date): bool
|
||||
{
|
||||
$month = (int) $date->format('n');
|
||||
$day = (int) $date->format('j');
|
||||
$days = $schema->getDays() ?? [];
|
||||
|
||||
foreach ($schema->getYearDays() ?? [] as $yd) {
|
||||
if (($yd['month'] ?? 0) === $month && ($yd['day'] ?? 0) === $day) {
|
||||
return true;
|
||||
if (!empty($days['week']) && in_array((int) $date->format('N'), $days['week'], true)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!empty($days['month']) && in_array((int) $date->format('j'), $days['month'], true)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!empty($days['year'])) {
|
||||
$month = (int) $date->format('n');
|
||||
$day = (int) $date->format('j');
|
||||
foreach ($days['year'] as $yd) {
|
||||
if (($yd['month'] ?? 0) === $month && ($yd['day'] ?? 0) === $day) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
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;
|
||||
@@ -34,6 +32,7 @@ class TaskGenerator
|
||||
$task = new Task();
|
||||
$task->setSchema($schema);
|
||||
$task->setDate(new \DateTime($deadline->format('Y-m-d')));
|
||||
$task->setName($schema->getName());
|
||||
$task->setCategory($schema->getCategory());
|
||||
|
||||
$this->em->persist($task);
|
||||
@@ -47,31 +46,4 @@ class TaskGenerator
|
||||
$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) {
|
||||
$task = new Task();
|
||||
$task->setSchema($schema);
|
||||
$task->setDate(null);
|
||||
$task->setCategory($schema->getCategory());
|
||||
$this->em->persist($task);
|
||||
$hasNew = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ($hasNew) {
|
||||
$this->em->flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,26 +2,40 @@
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\DTO\Request\ToggleRequest;
|
||||
use App\DTO\Request\CreateTaskRequest;
|
||||
use App\DTO\Request\UpdateTaskRequest;
|
||||
use App\DTO\Response\ToggleResponse;
|
||||
use App\Entity\Task;
|
||||
use App\Entity\TaskSchema;
|
||||
use App\Enum\TaskSchemaStatus;
|
||||
use App\Enum\TaskStatus;
|
||||
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,
|
||||
) {}
|
||||
|
||||
public function createTask(CreateTaskRequest $request): Task
|
||||
{
|
||||
$task = new Task();
|
||||
$task->setName($request->name);
|
||||
|
||||
if ($request->categoryId !== null) {
|
||||
$category = $this->categoryRepository->find($request->categoryId);
|
||||
$task->setCategory($category);
|
||||
}
|
||||
|
||||
if ($request->date !== null) {
|
||||
$task->setDate(new \DateTime($request->date));
|
||||
}
|
||||
|
||||
$this->em->persist($task);
|
||||
$this->em->flush();
|
||||
|
||||
return $task;
|
||||
}
|
||||
|
||||
public function updateTask(Task $task, UpdateTaskRequest $request): Task
|
||||
{
|
||||
$task->setName($request->name);
|
||||
@@ -43,27 +57,15 @@ class TaskManager
|
||||
return $task;
|
||||
}
|
||||
|
||||
public function toggleTaskStatus(TaskSchema $schema, ToggleRequest $request): ToggleResponse
|
||||
public function toggleTask(Task $task): Task
|
||||
{
|
||||
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::Done
|
||||
: TaskStatus::Active;
|
||||
$task->setStatus($newStatus);
|
||||
$this->em->flush();
|
||||
|
||||
return new ToggleResponse(completed: $newStatus === TaskStatus::Completed);
|
||||
return $task;
|
||||
}
|
||||
|
||||
public function deleteTask(Task $task): void
|
||||
|
||||
@@ -4,10 +4,12 @@ namespace App\Service;
|
||||
|
||||
use App\DTO\Request\CreateSchemaRequest;
|
||||
use App\DTO\Request\UpdateSchemaRequest;
|
||||
use App\Entity\Task;
|
||||
use App\Entity\TaskSchema;
|
||||
use App\Enum\TaskSchemaStatus;
|
||||
use App\Enum\TaskSchemaType;
|
||||
use App\Repository\CategoryRepository;
|
||||
use App\Repository\TaskRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
class TaskSchemaManager
|
||||
@@ -15,10 +17,11 @@ class TaskSchemaManager
|
||||
public function __construct(
|
||||
private EntityManagerInterface $em,
|
||||
private CategoryRepository $categoryRepository,
|
||||
private TaskRepository $taskRepository,
|
||||
private TaskSynchronizer $taskSynchronizer,
|
||||
) {}
|
||||
|
||||
public function createSchema(CreateSchemaRequest $request): TaskSchema
|
||||
public function createSchema(CreateSchemaRequest $request): TaskSchema|array
|
||||
{
|
||||
$schema = new TaskSchema();
|
||||
$schema->setName($request->name ?? '');
|
||||
@@ -30,6 +33,14 @@ class TaskSchemaManager
|
||||
$this->em->persist($schema);
|
||||
$this->em->flush();
|
||||
|
||||
if ($schema->getTaskType() === TaskSchemaType::Single) {
|
||||
$tasks = $this->createSingleTasks($schema);
|
||||
$this->em->remove($schema);
|
||||
$this->em->flush();
|
||||
|
||||
return $tasks;
|
||||
}
|
||||
|
||||
return $schema;
|
||||
}
|
||||
|
||||
@@ -50,6 +61,19 @@ class TaskSchemaManager
|
||||
return $schema;
|
||||
}
|
||||
|
||||
public function deleteSchema(TaskSchema $schema, bool $deleteTasks = false): void
|
||||
{
|
||||
if ($deleteTasks) {
|
||||
$tasks = $this->taskRepository->findBy(['schema' => $schema]);
|
||||
foreach ($tasks as $task) {
|
||||
$this->em->remove($task);
|
||||
}
|
||||
}
|
||||
|
||||
$this->em->remove($schema);
|
||||
$this->em->flush();
|
||||
}
|
||||
|
||||
private function applyFields(TaskSchema $schema, CreateSchemaRequest|UpdateSchemaRequest $request): void
|
||||
{
|
||||
if ($request->status !== null) {
|
||||
@@ -59,19 +83,16 @@ class TaskSchemaManager
|
||||
}
|
||||
}
|
||||
|
||||
if ($request->taskType !== null) {
|
||||
$taskType = TaskSchemaType::tryFrom($request->taskType);
|
||||
if ($request->type !== null) {
|
||||
$taskType = TaskSchemaType::tryFrom($request->type);
|
||||
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);
|
||||
$schema->setDays($request->days);
|
||||
}
|
||||
|
||||
private function resolveCategory(TaskSchema $schema, UpdateSchemaRequest|CreateSchemaRequest $request): void
|
||||
@@ -93,16 +114,37 @@ class TaskSchemaManager
|
||||
}
|
||||
}
|
||||
|
||||
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'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Task[]
|
||||
*/
|
||||
private function createSingleTasks(TaskSchema $schema): array
|
||||
{
|
||||
$days = $schema->getDays()['year'] ?? [];
|
||||
$tasks = [];
|
||||
|
||||
foreach ($days as $yd) {
|
||||
$month = $yd['month'] ?? 1;
|
||||
$day = $yd['day'] ?? 1;
|
||||
$date = new \DateTime(sprintf('%d-%02d-%02d', (int) date('Y'), $month, $day));
|
||||
|
||||
$task = new Task();
|
||||
$task->setName($schema->getName());
|
||||
$task->setCategory($schema->getCategory());
|
||||
$task->setDate($date);
|
||||
|
||||
$this->em->persist($task);
|
||||
$tasks[] = $task;
|
||||
}
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
return $tasks;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ namespace App\Service;
|
||||
|
||||
use App\Entity\Task;
|
||||
use App\Entity\TaskSchema;
|
||||
use App\Enum\TaskSchemaType;
|
||||
use App\Repository\TaskRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
@@ -30,8 +29,7 @@ class TaskSynchronizer
|
||||
$existingByDate = $this->loadExistingByDate($schema, $today);
|
||||
|
||||
$this->removeObsoleteTasks($existingByDate, $shouldExist);
|
||||
$this->handleNullDateTasks($schema);
|
||||
$this->resetFutureOverrides($existingByDate);
|
||||
$this->resetFutureOverrides($schema, $existingByDate);
|
||||
$this->createMissingTasks($schema, $deadlines, $existingByDate);
|
||||
|
||||
$this->em->flush();
|
||||
@@ -75,30 +73,14 @@ class TaskSynchronizer
|
||||
}
|
||||
}
|
||||
|
||||
private function handleNullDateTasks(TaskSchema $schema): void
|
||||
{
|
||||
$nullDateTasks = $this->taskRepository->findBy(['schema' => $schema, 'date' => null]);
|
||||
|
||||
if ($schema->getTaskType() === TaskSchemaType::Single && $schema->getDeadline() === null) {
|
||||
foreach ($nullDateTasks as $task) {
|
||||
$task->setName(null);
|
||||
$task->setCategory($schema->getCategory());
|
||||
}
|
||||
} else {
|
||||
foreach ($nullDateTasks as $task) {
|
||||
$this->em->remove($task);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, Task> $existingByDate
|
||||
*/
|
||||
private function resetFutureOverrides(array $existingByDate): void
|
||||
private function resetFutureOverrides(TaskSchema $schema, array $existingByDate): void
|
||||
{
|
||||
foreach ($existingByDate as $task) {
|
||||
$task->setName(null);
|
||||
$task->setCategory($task->getSchema()->getCategory());
|
||||
$task->setName($schema->getName());
|
||||
$task->setCategory($schema->getCategory());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,6 +96,7 @@ class TaskSynchronizer
|
||||
$task = new Task();
|
||||
$task->setSchema($schema);
|
||||
$task->setDate(new \DateTime($dateKey));
|
||||
$task->setName($schema->getName());
|
||||
$task->setCategory($schema->getCategory());
|
||||
|
||||
$this->em->persist($task);
|
||||
|
||||
@@ -1,78 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\DTO\Response\DayResponse;
|
||||
use App\DTO\Response\WeekViewResponse;
|
||||
use App\Entity\Task;
|
||||
use App\Repository\TaskRepository;
|
||||
|
||||
class TaskViewBuilder
|
||||
{
|
||||
public function __construct(
|
||||
private TaskGenerator $taskGenerator,
|
||||
private TaskRepository $taskRepository,
|
||||
) {}
|
||||
|
||||
public function buildWeekView(?\DateTimeImmutable $start = null): WeekViewResponse
|
||||
{
|
||||
$start = $start ?? new \DateTimeImmutable('today');
|
||||
$end = $start->modify('+6 days');
|
||||
|
||||
$this->taskGenerator->generateForRange($start, $end);
|
||||
$this->taskGenerator->generateForTasksWithoutDate();
|
||||
|
||||
$tasks = $this->taskRepository->findInRange($start, $end);
|
||||
|
||||
$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][] = $task;
|
||||
}
|
||||
}
|
||||
|
||||
$tasksWithoutDeadline = $this->taskRepository->findWithoutDate();
|
||||
|
||||
$dayResponses = [];
|
||||
foreach ($days as $date => $dayTasks) {
|
||||
usort($dayTasks, fn(Task $a, Task $b) => strcmp($a->getEffectiveName(), $b->getEffectiveName()));
|
||||
$dayResponses[] = new DayResponse(
|
||||
date: $date,
|
||||
tasks: $dayTasks,
|
||||
);
|
||||
}
|
||||
|
||||
return new WeekViewResponse(
|
||||
tasksWithoutDeadline: $tasksWithoutDeadline,
|
||||
days: $dayResponses,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Task[]
|
||||
*/
|
||||
public function buildAllTasksView(): array
|
||||
{
|
||||
$this->taskGenerator->generateForTasksWithoutDate();
|
||||
$result = [];
|
||||
|
||||
$noDate = $this->taskRepository->findWithoutDate();
|
||||
foreach ($noDate as $task) {
|
||||
$result[] = $task;
|
||||
}
|
||||
|
||||
$tasks = $this->taskRepository->findAllSorted();
|
||||
foreach ($tasks as $task) {
|
||||
$result[] = $task;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user