From 553f413e16cba494bd0eb69f4549eff8d9e7d829 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Danyi?= Date: Tue, 17 Apr 2018 17:21:44 +0200 Subject: [PATCH] * slide position handling --- config/autoload/doctrine.global.php | 2 +- config/routes.php | 1 + src/App/ConfigProvider.php | 1 + src/App/Entity/Slide.php | 27 +++++++++++++ src/App/Handler/SlidePositionHandler.php | 39 +++++++++++++++++++ .../Handler/SlidePositionHandlerFactory.php | 22 +++++++++++ src/App/Service/SlideManager.php | 28 +++++++++++-- 7 files changed, 115 insertions(+), 5 deletions(-) create mode 100644 src/App/Handler/SlidePositionHandler.php create mode 100644 src/App/Handler/SlidePositionHandlerFactory.php diff --git a/config/autoload/doctrine.global.php b/config/autoload/doctrine.global.php index 16be3aa..62f5716 100644 --- a/config/autoload/doctrine.global.php +++ b/config/autoload/doctrine.global.php @@ -67,7 +67,7 @@ return [ // 'Gedmo\Translatable\TranslatableListener', // 'Gedmo\Blameable\BlameableListener', // 'Gedmo\Loggable\LoggableListener', - // 'Gedmo\Sortable\SortableListener', + Gedmo\Sortable\SortableListener::class, // 'Gedmo\Sluggable\SluggableListener', ], ], diff --git a/config/routes.php b/config/routes.php index a0ac2f6..52e238b 100644 --- a/config/routes.php +++ b/config/routes.php @@ -42,5 +42,6 @@ return function (Application $app, MiddlewareFactory $factory, ContainerInterfac $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->put('/api/slide-position/{id:\d+}', App\Handler\SlidePositionHandler::class,'api.slide.position'); $app->route('/api/slide[/{id:\d+}]', App\Handler\SlideHandler::class)->setName('api.slide'); }; diff --git a/src/App/ConfigProvider.php b/src/App/ConfigProvider.php index 4713ff8..bd66889 100644 --- a/src/App/ConfigProvider.php +++ b/src/App/ConfigProvider.php @@ -39,6 +39,7 @@ class ConfigProvider Handler\HomePageHandler::class => Handler\HomePageHandlerFactory::class, Handler\TeamHandler::class => Handler\TeamHandlerFactory::class, Handler\SlideHandler::class => Handler\SlideHandlerFactory::class, + Handler\SlidePositionHandler::class => Handler\SlidePositionHandlerFactory::class, Service\TeamService::class => Service\TeamServiceFactory::class, Service\SlideManager::class => Service\SlideManagerFactory::class, diff --git a/src/App/Entity/Slide.php b/src/App/Entity/Slide.php index 27ba3ee..2462288 100644 --- a/src/App/Entity/Slide.php +++ b/src/App/Entity/Slide.php @@ -11,6 +11,7 @@ use JsonSerializable; /** * @ORM\Entity * @ORM\Table(name="slides") + * @ORM\Entity(repositoryClass="Gedmo\Sortable\Entity\Repository\SortableRepository") */ class Slide implements JsonSerializable { @@ -61,6 +62,13 @@ class Slide implements JsonSerializable */ private $updatedAt; + /** + * @ORM\Column(name="position", type="integer") + * @Gedmo\SortablePosition + * @var int + */ + private $position; + /** * @return int */ @@ -187,6 +195,24 @@ class Slide implements JsonSerializable return $this; } + /** + * @return int + */ + public function getPosition(): ?int + { + return $this->position; + } + + /** + * @param int $position + * @return Slide + */ + public function setPosition(?int $position): Slide + { + $this->position = $position; + return $this; + } + /** * @return array */ @@ -204,6 +230,7 @@ class Slide implements JsonSerializable 'updatedAt' => $this->getUpdatedAt() ? $this->getUpdatedAt()->format("Y-m-d H:i:s") : null, + 'position' => $this->getPosition(), ]; } } diff --git a/src/App/Handler/SlidePositionHandler.php b/src/App/Handler/SlidePositionHandler.php new file mode 100644 index 0000000..4be8331 --- /dev/null +++ b/src/App/Handler/SlidePositionHandler.php @@ -0,0 +1,39 @@ +slideManager = $teamService; + } + + /** + * @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->slideManager->changePosition((int)$id, (int)$data['position'])); + } catch (\InvalidArgumentException $e) { + return new JsonResponse([ + 'message' => $e->getMessage(), + ], 500); + } + } +} diff --git a/src/App/Handler/SlidePositionHandlerFactory.php b/src/App/Handler/SlidePositionHandlerFactory.php new file mode 100644 index 0000000..b3e3bdf --- /dev/null +++ b/src/App/Handler/SlidePositionHandlerFactory.php @@ -0,0 +1,22 @@ +get(SlideManager::class); + return new SlidePositionHandler($slideManager); + } +} diff --git a/src/App/Service/SlideManager.php b/src/App/Service/SlideManager.php index ce493a7..24cfd53 100644 --- a/src/App/Service/SlideManager.php +++ b/src/App/Service/SlideManager.php @@ -30,7 +30,9 @@ class SlideManager */ public function listSlides(): array { - return $this->em->getRepository(self::ENTITY_NAME)->findAll(); + return $this->em->getRepository(self::ENTITY_NAME)->findBy([], [ + 'position' => 'ASC', + ]); } /** @@ -51,7 +53,7 @@ class SlideManager * @throws \Doctrine\ORM\OptimisticLockException * @throws \Doctrine\DBAL\Exception\UniqueConstraintViolationException */ - public function addSlide(array $data) + public function addSlide(array $data): Slide { $entity = new Slide(); $this->form @@ -101,6 +103,24 @@ class SlideManager )); } + /** + * @param int $id + * @param int $position + * @return Slide[] + * @throws EntityNotFoundException + * @throws \Doctrine\ORM\ORMException + * @throws \Doctrine\ORM\OptimisticLockException + */ + public function changePosition(int $id, int $position): array + { + if (null === ($entity = $this->getSlide($id))) { + throw new EntityNotFoundException(); + } + $entity->setPosition($position); + $this->em->flush(); + return $this->listSlides(); + } + /** * @param int $id * @return bool @@ -119,11 +139,11 @@ class SlideManager /** * @param int $id * @param bool $isVisible - * @return bool + * @return Slide * @throws \Doctrine\ORM\ORMException * @throws \Doctrine\ORM\OptimisticLockException */ - public function setSlideVisible(int $id, bool $isVisible): bool + public function setSlideVisible(int $id, bool $isVisible): Slide { return $this->changeSlide($id, ['isVisible' => $isVisible]); }