feat: add getTask for fetching single task
This commit is contained in:
parent
0ff8ffdb05
commit
66ba2473af
@ -4,6 +4,7 @@ use Controllers\TaskController;
|
|||||||
use DI\Bridge\Slim\Bridge;
|
use DI\Bridge\Slim\Bridge;
|
||||||
use DI\Container;
|
use DI\Container;
|
||||||
use Controllers\HomeFrontController;
|
use Controllers\HomeFrontController;
|
||||||
|
use Slim\Handlers\Strategies\RequestResponseArgs;
|
||||||
|
|
||||||
require_once __DIR__ . '/vendor/autoload.php';
|
require_once __DIR__ . '/vendor/autoload.php';
|
||||||
|
|
||||||
@ -11,9 +12,15 @@ $container = new Container();
|
|||||||
$app = Bridge::create($container);
|
$app = Bridge::create($container);
|
||||||
$app->addBodyParsingMiddleware();
|
$app->addBodyParsingMiddleware();
|
||||||
|
|
||||||
|
// this strategy is preferable because we aren't using a lot of named placeholders
|
||||||
|
$routeCollector = $app->getRouteCollector();
|
||||||
|
$routeCollector->setDefaultInvocationStrategy(new RequestResponseArgs());
|
||||||
|
|
||||||
|
|
||||||
$app->get('/', [HomeFrontController::class, 'home']);
|
$app->get('/', [HomeFrontController::class, 'home']);
|
||||||
|
|
||||||
$app->get('/tasks', [TaskController::class, 'getTasks']);
|
$app->get('/tasks', [TaskController::class, 'getTasks']);
|
||||||
|
$app->get('/tasks/{id}', [TaskController::class, 'getTask']);
|
||||||
$app->post('/tasks', [TaskController::class, 'addTask']);
|
$app->post('/tasks', [TaskController::class, 'addTask']);
|
||||||
|
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ namespace Controllers;
|
|||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Carbon\Exceptions\InvalidFormatException;
|
use Carbon\Exceptions\InvalidFormatException;
|
||||||
use Exceptions\InvalidTaskDateException;
|
use Exceptions\InvalidTaskDateException;
|
||||||
|
use Exceptions\TaskNotFoundException;
|
||||||
use Models\Task;
|
use Models\Task;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
@ -69,7 +70,7 @@ class TaskController
|
|||||||
$end = Carbon::parse($params['tasks']['end']);
|
$end = Carbon::parse($params['tasks']['end']);
|
||||||
|
|
||||||
if ($start->gt($end) || $end->lt($start)) {
|
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)
|
} catch (InvalidFormatException | \DomainException $exception)
|
||||||
@ -134,4 +135,24 @@ class TaskController
|
|||||||
|
|
||||||
return $response;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user