140 lines
5.3 KiB
PHP
140 lines
5.3 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
|
|
{
|
|
|
|
/**
|
|
* @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'], $body['isHomeOffice'] ?? false);
|
|
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);
|
|
}
|
|
|
|
/**
|
|
* @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
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @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
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @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'],
|
|
$body['isHomeOffice'] ?? false
|
|
);
|
|
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);
|
|
}
|
|
|
|
/**
|
|
* @throws HttpBadRequestException
|
|
*/
|
|
protected function buildModel(Request $request, string $date, string $time, bool $isHomeOffice): 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)
|
|
->setIsHomeOffice($isHomeOffice);
|
|
}
|
|
|
|
} |