* changed routes while debuging endpoints

* added corsmiddlewarefactory
* changing teams is now possible
This commit is contained in:
Dávid Danyi 2018-04-09 18:41:04 +02:00
parent 1143075e17
commit 117c10a56a
5 changed files with 62 additions and 9 deletions

View File

@ -21,6 +21,7 @@ return [
// Use 'factories' for services provided by callbacks/factory classes. // Use 'factories' for services provided by callbacks/factory classes.
'factories' => [ 'factories' => [
// Fully\Qualified\ClassName::class => Fully\Qualified\FactoryName::class, // Fully\Qualified\ClassName::class => Fully\Qualified\FactoryName::class,
Tuupola\Middleware\CorsMiddleware::class => DoctrineExpressiveModule\Middleware\CorsMiddlewareFactory::class,
], ],
], ],
]; ];

View File

@ -36,6 +36,11 @@ return function (Application $app, MiddlewareFactory $factory, ContainerInterfac
$app->get('/', App\Handler\HomePageHandler::class, 'home'); $app->get('/', App\Handler\HomePageHandler::class, 'home');
$app->get('/api/ping', App\Handler\PingHandler::class, 'api.ping'); $app->get('/api/ping', App\Handler\PingHandler::class, 'api.ping');
$app->route('/api/team[/{id:\d+}]', App\Handler\TeamHandler::class)->setName('api.team'); $app->get('/api/team', App\Handler\TeamHandler::class,'api.team.list');
$app->get('/api/team/{id:\d+}', App\Handler\TeamHandler::class,'api.team.get');
$app->post('/api/team', App\Handler\TeamHandler::class,'api.team.add');
$app->put('/api/team[/{id:\d+}]', App\Handler\TeamHandler::class,'api.team.change');
$app->delete('/api/team/{id:\d+}', App\Handler\TeamHandler::class,'api.team.delete');
$app->route('/api/slide[/{id:\d+}]', App\Handler\SlideHandler::class)->setName('api.slide'); $app->route('/api/slide[/{id:\d+}]', App\Handler\SlideHandler::class)->setName('api.slide');
}; };

View File

@ -60,6 +60,29 @@ class TeamHandler extends AbstractCrudHandler
} }
} }
/**
* @param ServerRequestInterface $request
* @return JsonResponse
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function update(ServerRequestInterface $request)
{
$id = $request->getAttribute('id');
$data = $this->getRequestData($request);
try {
return new JsonResponse($this->teamService->changeTeam((int)$id, $data));
} catch (UniqueConstraintViolationException $e) {
return new JsonResponse([
'message' => 'The field `name` must be unique',
], 500);
} catch (\InvalidArgumentException $e) {
return new JsonResponse([
'message' => $e->getMessage(),
], 500);
}
}
/** /**
* @param ServerRequestInterface $request * @param ServerRequestInterface $request
* @return JsonResponse * @return JsonResponse

View File

@ -63,14 +63,14 @@ class TeamService
$this->em->persist($entity); $this->em->persist($entity);
$this->em->flush(); $this->em->flush();
return $entity; return $entity;
} else {
$messages = $this->form->getMessages();
$fields = array_keys($messages);
throw new \InvalidArgumentException(sprintf(
"The following fields are invalid: (%s)",
implode(", ", $fields)
));
} }
$messages = $this->form->getMessages();
$fields = array_keys($messages);
throw new \InvalidArgumentException(sprintf(
"The following fields are invalid: (%s)",
implode(", ", $fields)
));
} }
/** /**
@ -93,7 +93,13 @@ class TeamService
$this->em->flush(); $this->em->flush();
return true; return true;
} }
return false;
$messages = $this->form->getMessages();
$fields = array_keys($messages);
throw new \InvalidArgumentException(sprintf(
"The following fields are invalid: (%s)",
implode(", ", $fields)
));
} }
/** /**

View File

@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace DoctrineExpressiveModule\Middleware;
use Psr\Container\ContainerInterface;
use Tuupola\Middleware\CorsMiddleware;
class CorsMiddlewareFactory
{
public function __invoke(ContainerInterface $container) : CorsMiddleware
{
return new CorsMiddleware([
"headers.allow" => ["Authorization", "If-Match", "If-Unmodified-Since", "Content-type"],
]);
}
}