145 lines
5.5 KiB
PHP
145 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace TorstenHettstedt\TimekeepingApi\Controller;
|
|
|
|
use DateInterval;
|
|
use DateTime;
|
|
use Exception;
|
|
use Fig\Http\Message\StatusCodeInterface;
|
|
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\RepositoryBadWhereDataException;
|
|
use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryRecordAlreadyExistException;
|
|
use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryRecordNotFoundException;
|
|
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursRepository;
|
|
|
|
class WorkingHoursController extends AbstractController
|
|
{
|
|
|
|
/**
|
|
* @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 HttpInternalServerErrorException
|
|
* @throws HttpBadRequestException
|
|
* @noinspection PhpUnusedParameterInspection
|
|
*/
|
|
public function browse(Request $request, Response $response, array $args): Response
|
|
{
|
|
$repository = new WorkingHoursRepository($this->databases);
|
|
$queryParams = $request->getQueryParams();
|
|
try {
|
|
return $this->printResponse($response, $repository->findFiltered(
|
|
$queryParams['start-date'] ?? null,
|
|
$queryParams['end-date'] ?? null
|
|
), StatusCodeInterface::STATUS_OK);
|
|
} catch (RepositoryBadWhereDataException $exception) {
|
|
throw new HttpBadRequestException($request, 'Ein Wert für das Datum im Query ist ungültig', $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 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 (RepositoryRecordAlreadyExistException $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 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);
|
|
}
|
|
|
|
} |