update
This commit is contained in:
@@ -2,8 +2,11 @@
|
||||
|
||||
namespace App\Controller\Api;
|
||||
|
||||
use App\DTO\Request\CreateTaskRequest;
|
||||
use App\DTO\Request\UpdateTaskRequest;
|
||||
use App\Entity\Task;
|
||||
use App\Repository\TaskRepository;
|
||||
use App\Service\TaskGenerator;
|
||||
use App\Service\TaskManager;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
@@ -11,20 +14,42 @@ use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
#[Route('/api/tasks', name: 'tasks.')]
|
||||
#[Route('/api/tasks')]
|
||||
class TaskController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private TaskManager $taskManager,
|
||||
private TaskGenerator $taskGenerator,
|
||||
private TaskRepository $taskRepository,
|
||||
) {}
|
||||
|
||||
#[Route('/{id}', name: 'show', methods: ['GET'])]
|
||||
#[Route('', name: 'tasks.index', methods: ['GET'])]
|
||||
public function index(): JsonResponse
|
||||
{
|
||||
$today = new \DateTimeImmutable('today');
|
||||
$end = $today->modify('+6 days');
|
||||
$this->taskGenerator->generateForRange($today, $end);
|
||||
|
||||
$tasks = $this->taskRepository->findAllWithRelations();
|
||||
|
||||
return $this->json($tasks, context: ['groups' => ['task:read', 'category:read']]);
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: 'tasks.show', methods: ['GET'])]
|
||||
public function show(Task $task): JsonResponse
|
||||
{
|
||||
return $this->json($task, context: ['groups' => ['task:read', 'category:read']]);
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: 'update', methods: ['PUT'])]
|
||||
#[Route('', name: 'tasks.create', methods: ['POST'])]
|
||||
public function create(#[MapRequestPayload] CreateTaskRequest $dto): JsonResponse
|
||||
{
|
||||
$task = $this->taskManager->createTask($dto);
|
||||
|
||||
return $this->json($task, Response::HTTP_CREATED, context: ['groups' => ['task:read', 'category:read']]);
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: 'tasks.update', methods: ['PUT'])]
|
||||
public function update(#[MapRequestPayload] UpdateTaskRequest $dto, Task $task): JsonResponse
|
||||
{
|
||||
$task = $this->taskManager->updateTask($task, $dto);
|
||||
@@ -32,11 +57,19 @@ class TaskController extends AbstractController
|
||||
return $this->json($task, context: ['groups' => ['task:read', 'category:read']]);
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: 'delete', methods: ['DELETE'])]
|
||||
#[Route('/{id}', name: 'tasks.delete', methods: ['DELETE'])]
|
||||
public function delete(Task $task): JsonResponse
|
||||
{
|
||||
$this->taskManager->deleteTask($task);
|
||||
|
||||
return $this->json(null, Response::HTTP_NO_CONTENT);
|
||||
}
|
||||
|
||||
#[Route('/{id}/toggle', name: 'tasks.toggle', methods: ['PATCH'])]
|
||||
public function toggle(Task $task): JsonResponse
|
||||
{
|
||||
$task = $this->taskManager->toggleTask($task);
|
||||
|
||||
return $this->json($task, context: ['groups' => ['task:read', 'category:read']]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,13 +3,10 @@
|
||||
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\TaskManager;
|
||||
use App\Service\TaskSchemaManager;
|
||||
use App\Service\TaskViewBuilder;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
@@ -23,41 +20,16 @@ class TaskSchemaController extends AbstractController
|
||||
public function __construct(
|
||||
private TaskSchemaRepository $schemaRepository,
|
||||
private TaskSchemaManager $schemaManager,
|
||||
private TaskManager $taskManager,
|
||||
private TaskViewBuilder $taskViewBuilder,
|
||||
) {}
|
||||
|
||||
#[Route('', name: '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: '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), context: ['groups' => ['task:read', 'category:read']]);
|
||||
}
|
||||
|
||||
#[Route('/all', name: '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: 'schemas.allTasks', methods: ['GET'])]
|
||||
public function allTasks(): JsonResponse
|
||||
{
|
||||
return $this->json($this->taskViewBuilder->buildAllTasksView(), context: ['groups' => ['task:read', 'category:read']]);
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: 'schemas.show', methods: ['GET'])]
|
||||
public function show(TaskSchema $schema): JsonResponse
|
||||
{
|
||||
@@ -67,9 +39,13 @@ class TaskSchemaController extends AbstractController
|
||||
#[Route('', name: 'schemas.create', methods: ['POST'])]
|
||||
public function create(#[MapRequestPayload] CreateSchemaRequest $dto): JsonResponse
|
||||
{
|
||||
$schema = $this->schemaManager->createSchema($dto);
|
||||
$result = $this->schemaManager->createSchema($dto);
|
||||
|
||||
return $this->json($schema, Response::HTTP_CREATED, context: ['groups' => ['schema:read', 'category:read']]);
|
||||
if (is_array($result)) {
|
||||
return $this->json($result, Response::HTTP_CREATED, context: ['groups' => ['task:read', 'category:read']]);
|
||||
}
|
||||
|
||||
return $this->json($result, Response::HTTP_CREATED, context: ['groups' => ['schema:read', 'category:read']]);
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: 'schemas.update', methods: ['PUT'])]
|
||||
@@ -81,18 +57,11 @@ class TaskSchemaController extends AbstractController
|
||||
}
|
||||
|
||||
#[Route('/{id}', name: 'schemas.delete', methods: ['DELETE'])]
|
||||
public function delete(TaskSchema $schema): JsonResponse
|
||||
public function delete(TaskSchema $schema, Request $request): JsonResponse
|
||||
{
|
||||
$this->schemaManager->deleteSchema($schema);
|
||||
$deleteTasks = (bool) $request->query->get('deleteTasks', false);
|
||||
$this->schemaManager->deleteSchema($schema, $deleteTasks);
|
||||
|
||||
return $this->json(null, Response::HTTP_NO_CONTENT);
|
||||
}
|
||||
|
||||
#[Route('/{id}/toggle', name: 'schemas.toggle', methods: ['PATCH'])]
|
||||
public function toggle(TaskSchema $schema, #[MapRequestPayload] ToggleRequest $dto): JsonResponse
|
||||
{
|
||||
$result = $this->taskManager->toggleTaskStatus($schema, $dto);
|
||||
|
||||
return $this->json($result);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user