api-erstellen #1
@@ -0,0 +1,47 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace TorstenHettstedt\TimekeepingApi\Controller;
|
||||||
|
|
||||||
|
use JsonSerializable;
|
||||||
|
use PDO;
|
||||||
|
use Psr\Container\ContainerInterface;
|
||||||
|
use Slim\Psr7\Response;
|
||||||
|
|
||||||
|
abstract class AbstractController
|
||||||
|
{
|
||||||
|
|
||||||
|
protected PDO $databases;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* WorkingHoursController constructor.
|
||||||
|
*
|
||||||
|
* @param ContainerInterface $container
|
||||||
|
*
|
||||||
|
* @throws NotDatabasesException
|
||||||
|
*/
|
||||||
|
public function __construct(ContainerInterface $container)
|
||||||
|
{
|
||||||
|
if ($container->has('databases') === false) {
|
||||||
|
throw new NotDatabasesException('Datenbank ist nicht Vorhanden');
|
||||||
|
}
|
||||||
|
$this->databases = $container->get('databases');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Response $response
|
||||||
|
* @param JsonSerializable|JsonSerializable[] $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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,13 +2,10 @@
|
|||||||
|
|
||||||
namespace TorstenHettstedt\TimekeepingApi\Controller;
|
namespace TorstenHettstedt\TimekeepingApi\Controller;
|
||||||
|
|
||||||
|
|
||||||
use DateInterval;
|
use DateInterval;
|
||||||
use DateTime;
|
use DateTime;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Fig\Http\Message\StatusCodeInterface;
|
use Fig\Http\Message\StatusCodeInterface;
|
||||||
use PDO;
|
|
||||||
use Psr\Container\ContainerInterface;
|
|
||||||
use Slim\Exception\HttpBadRequestException;
|
use Slim\Exception\HttpBadRequestException;
|
||||||
use Slim\Exception\HttpInternalServerErrorException;
|
use Slim\Exception\HttpInternalServerErrorException;
|
||||||
use Slim\Exception\HttpNotFoundException;
|
use Slim\Exception\HttpNotFoundException;
|
||||||
@@ -19,26 +16,9 @@ use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryRecordAlreadyExistExc
|
|||||||
use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryRecordNotFoundException;
|
use TorstenHettstedt\TimekeepingApi\Repositories\RepositoryRecordNotFoundException;
|
||||||
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursRepository;
|
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursRepository;
|
||||||
|
|
||||||
class WorkingHoursController
|
class WorkingHoursController extends AbstractController
|
||||||
{
|
{
|
||||||
|
|
||||||
protected PDO $databases;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* WorkingHoursController constructor.
|
|
||||||
*
|
|
||||||
* @param ContainerInterface $container
|
|
||||||
*
|
|
||||||
* @throws NotDatabasesException
|
|
||||||
*/
|
|
||||||
public function __construct(ContainerInterface $container)
|
|
||||||
{
|
|
||||||
if ($container->has('databases') === false) {
|
|
||||||
throw new NotDatabasesException('Datenbank ist nicht Vorhanden');
|
|
||||||
}
|
|
||||||
$this->databases = $container->get('databases');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @param Response $response
|
* @param Response $response
|
||||||
@@ -131,24 +111,6 @@ class WorkingHoursController
|
|||||||
return $this->printResponse($response, $model, StatusCodeInterface::STATUS_CREATED);
|
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 Request $request
|
||||||
* @param string $date
|
* @param string $date
|
||||||
|
|||||||
@@ -4,41 +4,16 @@ namespace TorstenHettstedt\TimekeepingApi\Controller;
|
|||||||
|
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use PDO;
|
use Fig\Http\Message\StatusCodeInterface;
|
||||||
use Psr\Container\ContainerInterface;
|
|
||||||
use Slim\Exception\HttpInternalServerErrorException;
|
use Slim\Exception\HttpInternalServerErrorException;
|
||||||
use Slim\Psr7\Request;
|
use Slim\Psr7\Request;
|
||||||
use Slim\Psr7\Response;
|
use Slim\Psr7\Response;
|
||||||
use TorstenHettstedt\TimekeepingApi\Models\WorkingHoursView;
|
|
||||||
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursMonthlyViewRepository;
|
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursMonthlyViewRepository;
|
||||||
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursWeeklyViewRepository;
|
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursWeeklyViewRepository;
|
||||||
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursYearlyViewRepository;
|
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursYearlyViewRepository;
|
||||||
|
|
||||||
class WorkingHoursViewController
|
class WorkingHoursViewController extends AbstractController
|
||||||
{
|
{
|
||||||
protected PDO $databases;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var ContainerInterface
|
|
||||||
*/
|
|
||||||
protected ContainerInterface $container;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* WorkingHoursViewController constructor.
|
|
||||||
*
|
|
||||||
* @param ContainerInterface $container
|
|
||||||
*
|
|
||||||
* @throws NotDatabasesException
|
|
||||||
*/
|
|
||||||
public function __construct(ContainerInterface $container)
|
|
||||||
{
|
|
||||||
$this->container = $container;
|
|
||||||
if ($container->has('databases') === false) {
|
|
||||||
throw new NotDatabasesException('Datenbank ist nicht Vorhanden');
|
|
||||||
}
|
|
||||||
$this->databases = $container->get('databases');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @param Response $response
|
* @param Response $response
|
||||||
@@ -51,9 +26,9 @@ class WorkingHoursViewController
|
|||||||
*/
|
*/
|
||||||
public function browseWeekly(Request $request, Response $response, array $args): Response
|
public function browseWeekly(Request $request, Response $response, array $args): Response
|
||||||
{
|
{
|
||||||
$repository = new WorkingHoursWeeklyViewRepository($this->container->get('databases'));
|
$repository = new WorkingHoursWeeklyViewRepository($this->databases);
|
||||||
try {
|
try {
|
||||||
return $this->printResponse($response, $repository->findAll());
|
return $this->printResponse($response, $repository->findAll(), StatusCodeInterface::STATUS_OK);
|
||||||
} catch (Exception $exception) {
|
} catch (Exception $exception) {
|
||||||
throw new HttpInternalServerErrorException($request,
|
throw new HttpInternalServerErrorException($request,
|
||||||
'Der Wert für das Datum oder die Zeit ist falsch in der Datenbank', $exception);
|
'Der Wert für das Datum oder die Zeit ist falsch in der Datenbank', $exception);
|
||||||
@@ -72,9 +47,9 @@ class WorkingHoursViewController
|
|||||||
*/
|
*/
|
||||||
public function browseMonthly(Request $request, Response $response, array $args): Response
|
public function browseMonthly(Request $request, Response $response, array $args): Response
|
||||||
{
|
{
|
||||||
$repository = new WorkingHoursMonthlyViewRepository($this->container->get('databases'));
|
$repository = new WorkingHoursMonthlyViewRepository($this->databases);
|
||||||
try {
|
try {
|
||||||
return $this->printResponse($response, $repository->findAll());
|
return $this->printResponse($response, $repository->findAll(), StatusCodeInterface::STATUS_OK);
|
||||||
} catch (Exception $exception) {
|
} catch (Exception $exception) {
|
||||||
throw new HttpInternalServerErrorException($request,
|
throw new HttpInternalServerErrorException($request,
|
||||||
'Der Wert für das Datum oder die Zeit ist falsch in der Datenbank', $exception);
|
'Der Wert für das Datum oder die Zeit ist falsch in der Datenbank', $exception);
|
||||||
@@ -93,30 +68,13 @@ class WorkingHoursViewController
|
|||||||
*/
|
*/
|
||||||
public function browseYearly(Request $request, Response $response, array $args): Response
|
public function browseYearly(Request $request, Response $response, array $args): Response
|
||||||
{
|
{
|
||||||
$repository = new WorkingHoursYearlyViewRepository($this->container->get('databases'));
|
$repository = new WorkingHoursYearlyViewRepository($this->databases);
|
||||||
try {
|
try {
|
||||||
return $this->printResponse($response, $repository->findAll());
|
return $this->printResponse($response, $repository->findAll(), StatusCodeInterface::STATUS_OK);
|
||||||
} catch (Exception $exception) {
|
} catch (Exception $exception) {
|
||||||
throw new HttpInternalServerErrorException($request,
|
throw new HttpInternalServerErrorException($request,
|
||||||
'Der Wert für das Datum oder die Zeit ist falsch in der Datenbank', $exception);
|
'Der Wert für das Datum oder die Zeit ist falsch in der Datenbank', $exception);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Response $response
|
|
||||||
* @param WorkingHoursView[] $data
|
|
||||||
*
|
|
||||||
* @return Response
|
|
||||||
*/
|
|
||||||
protected function printResponse(Response $response, array $data): Response
|
|
||||||
{
|
|
||||||
$payload = json_encode($data);
|
|
||||||
|
|
||||||
$response->getBody()->write($payload);
|
|
||||||
|
|
||||||
return $response
|
|
||||||
->withHeader('Content-Type', 'application/json')
|
|
||||||
->withStatus(200);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user