From 54c2c8e897aaf1c85ea1a64f947b4fad45847e99 Mon Sep 17 00:00:00 2001 From: Miguel Nogueira Date: Sun, 13 Apr 2025 22:18:14 +0100 Subject: [PATCH] feat: update error handling logic --- index.php | 2 ++ src/Controllers/TaskController.php | 17 +++++++++++++++-- src/Interfaces/TaskDao.php | 8 +++++++- src/Repositories/TaskRepository.php | 7 +++++-- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/index.php b/index.php index ef01f71..6651683 100644 --- a/index.php +++ b/index.php @@ -13,6 +13,8 @@ $app->addBodyParsingMiddleware(); $app->get('/', [HomeFrontController::class, 'home']); +$app->get('/tasks', [TaskController::class, 'getTasks']); $app->post('/tasks', [TaskController::class, 'addTask']); + $app->run(); \ No newline at end of file diff --git a/src/Controllers/TaskController.php b/src/Controllers/TaskController.php index 524b669..98a66d6 100644 --- a/src/Controllers/TaskController.php +++ b/src/Controllers/TaskController.php @@ -102,7 +102,9 @@ class TaskController ->setStartDt($params['tasks']['start']) ->setEndDt($params['tasks']['end']); - if (!$this->repository->create($task)) + + $createdTask = $this->repository->create($task); + if ($createdTask == 0) { $this->builder->setError() ->setErrorMessage('An unexpected error has occurred whilst trying to perform this operation.') @@ -116,9 +118,20 @@ class TaskController $createdResponse = $response->withStatus(201)->withHeader('Content-Type', 'application/json'); $this->builder->setOptionalMessage('Task created.') - ->setPayload($task->persist()); + ->setPayload($task->setId($createdTask)->persist()); $createdResponse->getBody()->write($this->builder->build()); return $createdResponse; } + + public function getTasks(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface + { + $tasks = $this->repository->readAll(); + $payload['tasks'] = $tasks; + + $this->builder->setPayload($payload)->setOptionalMessage('Listing all tasks: '); + $response->getBody()->write($this->builder->build()); + + return $response; + } } \ No newline at end of file diff --git a/src/Interfaces/TaskDao.php b/src/Interfaces/TaskDao.php index 4a16b50..f48cea5 100644 --- a/src/Interfaces/TaskDao.php +++ b/src/Interfaces/TaskDao.php @@ -6,7 +6,13 @@ use Models\Task; interface TaskDao { - public function create(Task $task): bool; + /** + * Inserts the specified task. + * + * @param Task $task The task to persist. + * @return int The new task's ID, or 0 if the operation was unsucessful + */ + public function create(Task $task): int; /** * Returns an array of Tasks diff --git a/src/Repositories/TaskRepository.php b/src/Repositories/TaskRepository.php index 09839b6..024e9b6 100644 --- a/src/Repositories/TaskRepository.php +++ b/src/Repositories/TaskRepository.php @@ -19,7 +19,7 @@ class TaskRepository implements TaskDao $this->conn = $connection->getConnection(); } - public function create(Task $task): bool + public function create(Task $task): int { $data = $task->persist(); $stmt = $this->conn->prepare( @@ -35,8 +35,11 @@ class TaskRepository implements TaskDao $stmt->bindParam(7, $data['end']); $stmt->bindParam(8, $data['status_id']); + if ($stmt->execute()) { + return (int) $this->conn->lastInsertId(); + } - return $stmt->execute(); + return 0; } public function readAll(): array