* slide position handling
This commit is contained in:
parent
71d6577810
commit
553f413e16
@ -67,7 +67,7 @@ return [
|
||||
// 'Gedmo\Translatable\TranslatableListener',
|
||||
// 'Gedmo\Blameable\BlameableListener',
|
||||
// 'Gedmo\Loggable\LoggableListener',
|
||||
// 'Gedmo\Sortable\SortableListener',
|
||||
Gedmo\Sortable\SortableListener::class,
|
||||
// 'Gedmo\Sluggable\SluggableListener',
|
||||
],
|
||||
],
|
||||
|
||||
@ -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');
|
||||
};
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
39
src/App/Handler/SlidePositionHandler.php
Normal file
39
src/App/Handler/SlidePositionHandler.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Handler;
|
||||
|
||||
use App\Service\SlideManager;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
|
||||
class SlidePositionHandler extends AbstractCrudHandler
|
||||
{
|
||||
/** @var SlideManager */
|
||||
private $slideManager;
|
||||
|
||||
public function __construct(SlideManager $teamService)
|
||||
{
|
||||
$this->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);
|
||||
}
|
||||
}
|
||||
}
|
||||
22
src/App/Handler/SlidePositionHandlerFactory.php
Normal file
22
src/App/Handler/SlidePositionHandlerFactory.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Handler;
|
||||
|
||||
use App\Service\SlideManager;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
|
||||
class SlidePositionHandlerFactory
|
||||
{
|
||||
/**
|
||||
* @param ContainerInterface $container
|
||||
* @return RequestHandlerInterface
|
||||
*/
|
||||
public function __invoke(ContainerInterface $container) : RequestHandlerInterface
|
||||
{
|
||||
$slideManager = $container->get(SlideManager::class);
|
||||
return new SlidePositionHandler($slideManager);
|
||||
}
|
||||
}
|
||||
@ -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]);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user