update
This commit is contained in:
@@ -4,12 +4,11 @@ namespace App\Service;
|
||||
|
||||
use App\DTO\Request\ToggleRequest;
|
||||
use App\DTO\Request\UpdateTaskRequest;
|
||||
use App\DTO\Response\TaskResponse;
|
||||
use App\DTO\Response\ToggleResponse;
|
||||
use App\Entity\Task;
|
||||
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;
|
||||
@@ -21,10 +20,9 @@ class TaskManager
|
||||
private EntityManagerInterface $em,
|
||||
private CategoryRepository $categoryRepository,
|
||||
private TaskRepository $taskRepository,
|
||||
private TaskSerializer $taskSerializer,
|
||||
) {}
|
||||
|
||||
public function updateTask(Task $task, UpdateTaskRequest $request): TaskResponse
|
||||
public function updateTask(Task $task, UpdateTaskRequest $request): Task
|
||||
{
|
||||
$task->setName($request->name);
|
||||
|
||||
@@ -43,7 +41,7 @@ class TaskManager
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
return $this->taskSerializer->serializeTask($task, new \DateTimeImmutable('today'));
|
||||
return $task;
|
||||
}
|
||||
|
||||
public function toggleTaskStatus(TaskSchema $schema, ToggleRequest $request): ToggleResponse
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
<?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(),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -3,8 +3,8 @@
|
||||
namespace App\Service;
|
||||
|
||||
use App\DTO\Response\DayResponse;
|
||||
use App\DTO\Response\TaskResponse;
|
||||
use App\DTO\Response\WeekViewResponse;
|
||||
use App\Entity\Task;
|
||||
use App\Repository\TaskRepository;
|
||||
|
||||
class TaskViewBuilder
|
||||
@@ -12,23 +12,18 @@ 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) {
|
||||
@@ -39,20 +34,15 @@ class TaskViewBuilder
|
||||
foreach ($tasks as $task) {
|
||||
$dateKey = $task->getDate()->format('Y-m-d');
|
||||
if (isset($days[$dateKey])) {
|
||||
$days[$dateKey][] = $this->taskSerializer->serializeTask($task, $today);
|
||||
$days[$dateKey][] = $task;
|
||||
}
|
||||
}
|
||||
|
||||
// Tasks without date (einzel without deadline)
|
||||
$tasksWithoutDeadline = [];
|
||||
$noDateTasks = $this->taskRepository->findWithoutDate();
|
||||
foreach ($noDateTasks as $task) {
|
||||
$tasksWithoutDeadline[] = $this->taskSerializer->serializeTask($task, $today);
|
||||
}
|
||||
$tasksWithoutDeadline = $this->taskRepository->findWithoutDate();
|
||||
|
||||
$dayResponses = [];
|
||||
foreach ($days as $date => $dayTasks) {
|
||||
usort($dayTasks, fn(TaskResponse $a, TaskResponse $b) => strcmp($a->name, $b->name));
|
||||
usort($dayTasks, fn(Task $a, Task $b) => strcmp($a->getEffectiveName(), $b->getEffectiveName()));
|
||||
$dayResponses[] = new DayResponse(
|
||||
date: $date,
|
||||
tasks: $dayTasks,
|
||||
@@ -66,24 +56,21 @@ class TaskViewBuilder
|
||||
}
|
||||
|
||||
/**
|
||||
* @return TaskResponse[]
|
||||
* @return Task[]
|
||||
*/
|
||||
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);
|
||||
$result[] = $task;
|
||||
}
|
||||
|
||||
// Then all tasks with date
|
||||
$tasks = $this->taskRepository->findAllSorted();
|
||||
foreach ($tasks as $task) {
|
||||
$result[] = $this->taskSerializer->serializeTask($task, $today);
|
||||
$result[] = $task;
|
||||
}
|
||||
|
||||
return $result;
|
||||
|
||||
Reference in New Issue
Block a user