diff --git a/api/html/index.php b/api/html/index.php
index 3330c7b..300a3bd 100644
--- a/api/html/index.php
+++ b/api/html/index.php
@@ -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();
\ No newline at end of file
diff --git a/api/src/Controller/HttpConflictRequestException.php b/api/src/Controller/HttpConflictRequestException.php
new file mode 100644
index 0000000..f37fa77
--- /dev/null
+++ b/api/src/Controller/HttpConflictRequestException.php
@@ -0,0 +1,21 @@
+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);
+ }
+
+}
\ No newline at end of file
diff --git a/api/src/Middleware/JsonBodyParserMiddleware.php b/api/src/Middleware/JsonBodyParserMiddleware.php
new file mode 100644
index 0000000..1c020e6
--- /dev/null
+++ b/api/src/Middleware/JsonBodyParserMiddleware.php
@@ -0,0 +1,25 @@
+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);
+ }
+}