api-erstellen #1
@@ -6,6 +6,9 @@ $dotenv->load();
|
||||
|
||||
use DI\Container;
|
||||
use Slim\Factory\AppFactory;
|
||||
use Slim\Routing\RouteCollectorProxy;
|
||||
use TorstenHettstedt\TimekeepingApi\Controller\WorkingHoursController;
|
||||
use TorstenHettstedt\TimekeepingApi\Middleware\JsonBodyParserMiddleware;
|
||||
|
||||
$container = new Container();
|
||||
|
||||
@@ -16,6 +19,7 @@ $container->set('databases', function () {
|
||||
|
||||
AppFactory::setContainer($container);
|
||||
$app = AppFactory::create();
|
||||
$app->add(new JsonBodyParserMiddleware());
|
||||
|
||||
$app->get(
|
||||
'/',
|
||||
@@ -37,6 +41,15 @@ $app->get(
|
||||
'TorstenHettstedt\TimekeepingApi\Controller\WorkingHoursViewController:browseYearly'
|
||||
);
|
||||
|
||||
$app->group('/working-hours', function (RouteCollectorProxy $group) {
|
||||
$group->get('', WorkingHoursController::class . ':browse');
|
||||
$group->post('', WorkingHoursController::class . ':creat');
|
||||
$group->group('/{id:\d\d\d\d-\d\d-\d\d}', function (RouteCollectorProxy $group) {
|
||||
$group->get('', WorkingHoursController::class . ':read');
|
||||
$group->put('', WorkingHoursController::class . ':update');
|
||||
});
|
||||
});
|
||||
|
||||
$app->addErrorMiddleware(true, true, true);
|
||||
|
||||
$app->run();
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace TorstenHettstedt\TimekeepingApi\Controller;
|
||||
|
||||
use Slim\Exception\HttpSpecializedException;
|
||||
|
||||
class HttpConflictRequestException extends HttpSpecializedException
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $code = 409;
|
||||
|
||||
/**
|
||||
|
TorstenHettstedt marked this conversation as resolved
|
||||
* @var string
|
||||
*/
|
||||
protected $message = 'Conflict.';
|
||||
|
||||
protected $title = '409 Conflict';
|
||||
|
TorstenHettstedt marked this conversation as resolved
TorstenHettstedt
commented
Typen erweitern. Typen erweitern.
|
||||
protected $description = 'The 409 (Conflict) status code indicates that the request could not be completed due to a conflict with the current state of the target resource.';
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
<?php
|
||||
|
||||
namespace TorstenHettstedt\TimekeepingApi\Controller;
|
||||
|
||||
|
||||
use DateInterval;
|
||||
use DateTime;
|
||||
use Exception;
|
||||
use Fig\Http\Message\StatusCodeInterface;
|
||||
use PDO;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Slim\Exception\HttpBadRequestException;
|
||||
use Slim\Exception\HttpInternalServerErrorException;
|
||||
use Slim\Exception\HttpNotFoundException;
|
||||
use Slim\Psr7\Request;
|
||||
use Slim\Psr7\Response;
|
||||
use TorstenHettstedt\TimekeepingApi\Models\WorkingHours;
|
||||
use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryModelAlreadyExists;
|
||||
use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryRecordNotFoundException;
|
||||
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursRepository;
|
||||
|
||||
class WorkingHoursController
|
||||
{
|
||||
|
||||
protected PDO $databases;
|
||||
protected ContainerInterface $container;
|
||||
|
TorstenHettstedt marked this conversation as resolved
TorstenHettstedt
commented
Der Container wird nur bei der Initialisierung benötigt. Der Container wird nur bei der Initialisierung benötigt.
|
||||
|
||||
/**
|
||||
* WorkingHoursController constructor.
|
||||
*
|
||||
* @param ContainerInterface $container
|
||||
*
|
||||
* @throws NotDatabasesException
|
||||
*/
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
if ($this->container->has('databases') === false) {
|
||||
throw new NotDatabasesException('Datenbank ist nicht Vorhanden');
|
||||
}
|
||||
$this->databases = $this->container->get('databases');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @param mixed[] $args
|
||||
*
|
||||
* @return Response
|
||||
*
|
||||
* @throws HttpBadRequestException
|
||||
* @throws HttpNotFoundException
|
||||
*/
|
||||
public function update(Request $request, Response $response, array $args): Response
|
||||
{
|
||||
$body = $request->getParsedBody();
|
||||
$repository = new WorkingHoursRepository($this->databases);
|
||||
$model = $this->buildModel($request, $args['id'], $body['workingTime']);
|
||||
try {
|
||||
$repository->update($model);
|
||||
} catch (RepositoryRecordNotFoundException $exception) {
|
||||
throw new HttpNotFoundException($request, 'Der Eintrag ist nicht vorhanden.', $exception);
|
||||
}
|
||||
return $this->printResponse($response, $model, StatusCodeInterface::STATUS_OK);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @param mixed[] $args
|
||||
*
|
||||
* @return Response
|
||||
*
|
||||
* @throws Exception
|
||||
* @noinspection PhpUnusedParameterInspection
|
||||
*/
|
||||
public function browse(Request $request, Response $response, array $args): Response
|
||||
{
|
||||
$repository = new WorkingHoursRepository($this->databases);
|
||||
return $this->printResponse($response, $repository->findAll(), StatusCodeInterface::STATUS_OK);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @param mixed[] $args
|
||||
*
|
||||
* @return Response
|
||||
*
|
||||
* @throws HttpNotFoundException
|
||||
* @throws HttpInternalServerErrorException
|
||||
*/
|
||||
public function read(Request $request, Response $response, array $args): Response
|
||||
{
|
||||
$repository = new WorkingHoursRepository($this->databases);
|
||||
try {
|
||||
return $this->printResponse($response, $repository->findByKey($args['id']), StatusCodeInterface::STATUS_OK);
|
||||
} catch (RepositoryRecordNotFoundException $exception) {
|
||||
throw new HttpNotFoundException($request, 'Der Eintrag ist nicht vorhanden.', $exception);
|
||||
} catch (Exception $exception) {
|
||||
throw new HttpInternalServerErrorException($request, 'Der Wert für das Datum oder die Zeit ist falsch in der Datenbank', $exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @param mixed[] $args
|
||||
*
|
||||
* @return Response
|
||||
*
|
||||
* @throws HttpBadRequestException
|
||||
* @throws HttpConflictRequestException
|
||||
* @throws HttpInternalServerErrorException
|
||||
* @noinspection PhpUnusedParameterInspection
|
||||
*/
|
||||
public function creat(Request $request, Response $response, array $args): Response
|
||||
{
|
||||
$body = $request->getParsedBody();
|
||||
$repository = new WorkingHoursRepository($this->databases);
|
||||
$model = $this->buildModel($request, $body['workingDay'], $body['workingTime']);
|
||||
try {
|
||||
$repository->insert($model);
|
||||
} catch (RepositoryModelAlreadyExists $exception) {
|
||||
throw new HttpConflictRequestException($request, 'Der Eintrag ist schon vorhanden.', $exception);
|
||||
} catch (Exception $exception) {
|
||||
throw new HttpInternalServerErrorException($request, 'Der Wert für das Datum oder die Zeit ist falsch in der Datenbank', $exception);
|
||||
}
|
||||
return $this->printResponse($response, $model, StatusCodeInterface::STATUS_CREATED);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Response $response
|
||||
* @param mixed $data
|
||||
* @param int $status_code
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
protected function printResponse(Response $response, mixed $data, int $status_code): Response
|
||||
{
|
||||
$payload = json_encode($data);
|
||||
|
||||
$response->getBody()->write($payload);
|
||||
|
||||
return $response
|
||||
->withHeader('Content-Type', 'application/json')
|
||||
->withStatus($status_code);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param string $date
|
||||
* @param string $time
|
||||
*
|
||||
* @return WorkingHours
|
||||
* @throws HttpBadRequestException
|
||||
*/
|
||||
protected function buildModel(Request $request, string $date, string $time): WorkingHours
|
||||
{
|
||||
try {
|
||||
$workingDay = new DateTime($date);
|
||||
} catch (Exception $exception) {
|
||||
throw new HttpBadRequestException($request, 'Der Wert für das Datum ist falsch', $exception);
|
||||
}
|
||||
try {
|
||||
$workingTime = new DateInterval('P0000-00-00T' . $time);
|
||||
} catch (Exception $exception) {
|
||||
throw new HttpBadRequestException($request, 'Der Wert für die Zeit ist falsch', $exception);
|
||||
}
|
||||
return (new WorkingHours())->setWorkingDay($workingDay)->setWorkingTime($workingTime);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace TorstenHettstedt\TimekeepingApi\Middleware;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
|
||||
|
||||
class JsonBodyParserMiddleware implements MiddlewareInterface
|
||||
{
|
||||
public function process(Request $request, RequestHandler $handler): Response
|
||||
{
|
||||
$contentType = $request->getHeaderLine('Content-Type');
|
||||
|
||||
if (strstr($contentType, 'application/json')) {
|
||||
$contents = json_decode(file_get_contents('php://input'), true);
|
||||
if (json_last_error() === JSON_ERROR_NONE) {
|
||||
$request = $request->withParsedBody($contents);
|
||||
}
|
||||
}
|
||||
|
||||
return $handler->handle($request);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user
Kann auf eine Zeile.