feat: add getTask for fetching single task

This commit is contained in:
2025-04-14 00:05:48 +01:00
parent 0ff8ffdb05
commit 66ba2473af
2 changed files with 29 additions and 1 deletions

View File

@@ -5,6 +5,7 @@ namespace Controllers;
use Carbon\Carbon;
use Carbon\Exceptions\InvalidFormatException;
use Exceptions\InvalidTaskDateException;
use Exceptions\TaskNotFoundException;
use Models\Task;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
@@ -69,7 +70,7 @@ class TaskController
$end = Carbon::parse($params['tasks']['end']);
if ($start->gt($end) || $end->lt($start)) {
throw new \DomainException('Invalid task date. Start date must not be after end date.');
throw new InvalidTaskDateException('Invalid task date. Start date must not be after end date.');
}
} catch (InvalidFormatException | \DomainException $exception)
@@ -134,4 +135,24 @@ class TaskController
return $response;
}
public function getTask(ServerRequestInterface $request, ResponseInterface $response, string $id): ResponseInterface
{
try {
$payload['task'] = $this->repository->readById((int)$id)->persist();
$this->builder->setPayload($payload)->setOptionalMessage("Task retrieved.");
$response->getBody()->write($this->builder->build());
return $response;
} catch (TaskNotFoundException $e) {
$this->builder->setError()->setErrorMessage($e->getMessage())->setErrorCode(404);
$errorResponse = $response->withStatus(404, $e->getMessage());
$response->getBody()->write($this->builder->build());
return $errorResponse;
}
}
}