Permalink
Browse files

Try to remember how to write PHP again, also test build

  • Loading branch information...
jacobemerick committed Jan 26, 2019
1 parent 14e33e4 commit e951c4e997bc19da4c08e75edfb04241646c7d15
Showing with 89 additions and 2 deletions.
  1. +6 −0 bootstrap.php
  2. +34 −2 src/Controller/Event.php
  3. +23 −0 src/Model/Event.php
  4. +26 −0 src/Serializer/Event.php
@@ -43,9 +43,11 @@
));
$di->types['Aura\Sql\ExtendedPdo'] = $di->lazyGet('dbal');
$di->set('eventModel', $di->lazyNew('Jacobemerick\LifestreamService\Model\Event'));
$di->set('typeModel', $di->lazyNew('Jacobemerick\LifestreamService\Model\Type'));
// set up serializers
$di->set('eventSerializer', $di->lazyNew('Jacobemerick\LifestreamService\Serializer\Event'));
$di->set('typeSerializer', $di->lazyNew('Jacobemerick\LifestreamService\Serializer\Type'));
// set up logger
@@ -85,6 +87,10 @@
// controllers
use Jacobemerick\LifestreamService\Controller;
$talus->addController('getEvent', function ($req, $res) use ($di) {
return (new Controller\Event($di))->getEvent($req, $res);
});
$talus->addController('getTypes', function ($req, $res) use ($di) {
return (new Controller\Type($di))->getTypes($req, $res);
});
@@ -2,12 +2,44 @@
namespace Jacobemerick\LifestreamService\Controller;
use Interop\Container\ContainerInterface as Container;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
class Event
{
public function __construct() {}
/** @var Container */
protected $container;
/**
* @param Container $container
*/
public function __construct(Container $container)
{
$this->container = $container;
}
/**
* @param Request $request
* @param Response $response
* @return Response
*/
public function getEvent(Request $request, Response $response)
{
$eventId = $request->getAttribute('swagger')->getParams()['event_id']['value'];
$event = $this->container->get('eventModel')->findById($eventId);
if (!$event) {
throw new NotFound('No event found');
}
$event = $this->container->get('eventSerializer')->__invoke($event);
$event = json_encode($event);
$response->getBody()->write($event);
public function getEvent() {}
return $response;
}
public function getEvents() {}
}
@@ -70,6 +70,29 @@ public function getEvents(
return $this->extendedPdo->fetchAll($query, $bindings);
}
/**
* @param integer $id
* @return array
*/
public function findById($id) {
$query = "
SELECT `event`.`id`, `description`, `description_html`, `datetime`, `metadata`,
`user`.`id` AS `user_id`, `user`.`name` AS `user_name`,
`type`.`id` AS `type_id`, `type`.`name` AS `type_name`
FROM `event`
INNER JOIN `user` ON `user`.`id` = `event`.`user_id`
INNER JOIN `type` ON `type`.`id` = `event`.`type_id`
WHERE `event`.`id` = :id
LIMIT 1";
$bindings = [
'id' => $id
];
return $this->extendedPdo->fetchOne($query, $bindings);
}
/**
* @param string $type
* @param integer $typeId
@@ -0,0 +1,26 @@
<?php
namespace Jacobemerick\LifestreamService\Serializer;
use DateTime;
class Event
{
/**
* @param array $event
* @return array
*/
public function __invoke(array $event)
{
return [
'id' => $event['id'],
'description' => $event['description'],
'description_html' => $event['description_html'],
'metadata' => json_decode($event['metadata'], true),
'date' => (new DateTime($event['date']))->format('c'),
'user' => $event['user_name'],
'type' => $event['type_name'],
];
}
}

0 comments on commit e951c4e

Please sign in to comment.