api-erstellen #1
+19
-1
@@ -17,7 +17,25 @@ $container->set('databases', function () {
|
||||
AppFactory::setContainer($container);
|
||||
$app = AppFactory::create();
|
||||
|
||||
$app->get('/', 'TorstenHettstedt\TimekeepingApi\Controller\HelloWorldController:read');
|
||||
$app->get(
|
||||
'/',
|
||||
'TorstenHettstedt\TimekeepingApi\Controller\HelloWorldController:read'
|
||||
);
|
||||
|
||||
$app->get(
|
||||
'/views/working-hours/weekly',
|
||||
'TorstenHettstedt\TimekeepingApi\Controller\WorkingHoursViewController:browseWeekly'
|
||||
);
|
||||
|
||||
$app->get(
|
||||
'/views/working-hours/monthly',
|
||||
'TorstenHettstedt\TimekeepingApi\Controller\WorkingHoursViewController:browseMonthly'
|
||||
);
|
||||
|
||||
$app->get(
|
||||
'/views/working-hours/yearly',
|
||||
'TorstenHettstedt\TimekeepingApi\Controller\WorkingHoursViewController:browseYearly'
|
||||
);
|
||||
|
||||
$app->addErrorMiddleware(true, true, true);
|
||||
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace TorstenHettstedt\TimekeepingApi\Controller;
|
||||
|
||||
use Exception;
|
||||
|
||||
class NotDatabasesException extends Exception
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace TorstenHettstedt\TimekeepingApi\Controller;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Slim\Psr7\Request;
|
||||
use Slim\Psr7\Response;
|
||||
use TorstenHettstedt\TimekeepingApi\Models\WorkingHoursView;
|
||||
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursMonthlyViewRepository;
|
||||
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursWeeklyViewRepository;
|
||||
use TorstenHettstedt\TimekeepingApi\Repositories\WorkingHoursYearlyViewRepository;
|
||||
|
||||
class WorkingHoursViewController
|
||||
{
|
||||
/**
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
protected ContainerInterface $container;
|
||||
|
||||
public function __construct(ContainerInterface $container) {
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @param mixed[] $args
|
||||
*
|
||||
* @return Response
|
||||
*
|
||||
* @noinspection PhpUnusedParameterInspection
|
||||
* @throws NotDatabasesException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function browseWeekly(Request $request, Response $response, array $args): Response
|
||||
{
|
||||
if ($this->container->has('databases') === false) {
|
||||
throw new NotDatabasesException('Datenbank ist nicht Vorhanden');
|
||||
}
|
||||
$repository = new WorkingHoursWeeklyViewRepository($this->container->get('databases'));
|
||||
return $this->printResponse($response, $repository->findAll());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @param mixed[] $args
|
||||
*
|
||||
* @return Response
|
||||
*
|
||||
* @noinspection PhpUnusedParameterInspection
|
||||
* @throws Exception
|
||||
*/
|
||||
public function browseMonthly(Request $request, Response $response, array $args): Response
|
||||
{
|
||||
if ($this->container->has('databases') === false) {
|
||||
throw new NotDatabasesException('Datenbank ist nicht Vorhanden');
|
||||
}
|
||||
$repository = new WorkingHoursMonthlyViewRepository($this->container->get('databases'));
|
||||
return $this->printResponse($response, $repository->findAll());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @param mixed[] $args
|
||||
*
|
||||
* @return Response
|
||||
*
|
||||
* @noinspection PhpUnusedParameterInspection
|
||||
* @throws Exception
|
||||
*/
|
||||
public function browseYearly(Request $request, Response $response, array $args): Response
|
||||
{
|
||||
if ($this->container->has('databases') === false) {
|
||||
throw new NotDatabasesException('Datenbank ist nicht Vorhanden');
|
||||
}
|
||||
$repository = new WorkingHoursYearlyViewRepository($this->container->get('databases'));
|
||||
return $this->printResponse($response, $repository->findAll());
|
||||
}
|
||||
|
||||
/**
|
||||
* @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