refactor CalendarRange repository and available range action

This commit is contained in:
Julien Fastré 2022-05-17 12:26:34 +02:00
parent 8b1271a466
commit 9ceb66e2da
4 changed files with 159 additions and 52 deletions

View File

@ -11,38 +11,63 @@ declare(strict_types=1);
namespace Chill\CalendarBundle\Controller; namespace Chill\CalendarBundle\Controller;
use Chill\CalendarBundle\Repository\CalendarRangeRepository;
use Chill\MainBundle\CRUD\Controller\ApiController; use Chill\MainBundle\CRUD\Controller\ApiController;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Serializer\Model\Collection;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
use function count; use function count;
class CalendarRangeAPIController extends ApiController class CalendarRangeAPIController extends ApiController
{ {
/** private CalendarRangeRepository $calendarRangeRepository;
* @Route("/api/1.0/calendar/calendar-range-available.{_format}", name="chill_api_single_calendar_range_available")
*/ public function __construct(CalendarRangeRepository $calendarRangeRepository)
public function availableRanges(Request $request, string $_format): JsonResponse
{ {
$em = $this->getDoctrine()->getManager(); $this->calendarRangeRepository = $calendarRangeRepository;
}
$sql = 'SELECT c FROM ChillCalendarBundle:CalendarRange c /**
WHERE NOT EXISTS (SELECT cal.id FROM ChillCalendarBundle:Calendar cal WHERE cal.calendarRange = c.id)'; * @Route("/api/1.0/calendar/calendar-range-available/{id}.{_format}",
* name="chill_api_single_calendar_range_available",
* requirements={"_format": "json"}
* )
*/
public function availableRanges(User $user, Request $request, string $_format): JsonResponse
{
//return new JsonResponse(['ok' => true], 200, [], false);
$this->denyAccessUnlessGranted('ROLE_USER');
if ($request->query->has('user')) { if (!$request->query->has('dateFrom')) {
$user = $request->query->get('user'); throw new BadRequestHttpException('You must provide a dateFrom parameter');
$sql = $sql . ' AND c.user = :user';
$query = $em->createQuery($sql)
->setParameter('user', $user);
} else {
$query = $em->createQuery($sql);
} }
$results = $query->getResult(); if (false === $dateFrom = \DateTimeImmutable::createFromFormat(\DateTimeImmutable::ATOM,
$request->query->get('dateFrom'))) {
throw new BadRequestHttpException('dateFrom not parsable');
}
return $this->json(['count' => count($results), 'results' => $results], Response::HTTP_OK, [], ['groups' => ['read']]); if (!$request->query->has('dateTo')) {
//TODO use also the paginator, eg return $this->serializeCollection('get', $request, $_format, $paginator, $results); throw new BadRequestHttpException('You must provide a dateTo parameter');
}
if (false === $dateTo = \DateTimeImmutable::createFromFormat(\DateTimeImmutable::ATOM,
$request->query->get('dateTo'))) {
throw new BadRequestHttpException('dateTo not parsable');
}
$total = $this->calendarRangeRepository->countByAvailableRangesForUser($user, $dateFrom, $dateTo);
$paginator = $this->getPaginatorFactory()->create($total);
$ranges = $this->calendarRangeRepository->findByAvailableRangesForUser($user, $dateFrom, $dateTo,
$paginator->getItemsPerPage(), $paginator->getCurrentPageFirstItemNumber());
$collection = new Collection($ranges, $paginator);
return $this->json($collection, Response::HTTP_OK, [], ['groups' => ['read']]);
} }
} }

View File

@ -25,7 +25,7 @@ use Symfony\Component\Serializer\Annotation\Groups;
/** /**
* @ORM\Table(name="chill_calendar.calendar_range", indexes={@ORM\Index(name="idx_calendar_range_remote", columns={"remoteId"})}) * @ORM\Table(name="chill_calendar.calendar_range", indexes={@ORM\Index(name="idx_calendar_range_remote", columns={"remoteId"})})
* @ORM\Entity(repositoryClass=CalendarRangeRepository::class) * @ORM\Entity
*/ */
class CalendarRange implements TrackCreationInterface, TrackUpdateInterface class CalendarRange implements TrackCreationInterface, TrackUpdateInterface
{ {

View File

@ -12,48 +12,107 @@ declare(strict_types=1);
namespace Chill\CalendarBundle\Repository; namespace Chill\CalendarBundle\Repository;
use Chill\CalendarBundle\Entity\CalendarRange; use Chill\CalendarBundle\Entity\CalendarRange;
use Chill\MainBundle\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry; use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectRepository;
use Monolog\DateTimeImmutable;
use UnexpectedValueException;
/** class CalendarRangeRepository implements ObjectRepository
* @method CalendarRange|null find($id, $lockMode = null, $lockVersion = null)
* @method CalendarRange|null findOneBy(array $criteria, array $orderBy = null)
* @method CalendarRange[] findAll()
* @method CalendarRange[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class CalendarRangeRepository extends ServiceEntityRepository
{ {
public function __construct(ManagerRegistry $registry) private EntityRepository $repository;
public function __construct(EntityManagerInterface $entityManager)
{ {
parent::__construct($registry, CalendarRange::class); $this->repository = $entityManager->getRepository(CalendarRange::class);
} }
// /** public function find($id): ?CalendarRange
// * @return CalendarRange[] Returns an array of CalendarRange objects
// */
/*
public function findByExampleField($value)
{ {
return $this->createQueryBuilder('c') return $this->repository->find($id);
->andWhere('c.exampleField = :val')
->setParameter('val', $value)
->orderBy('c.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
} }
*/
/* /**
public function findOneBySomeField($value): ?CalendarRange * @return array|CalendarRange[]
*/
public function findAll(): array
{ {
return $this->createQueryBuilder('c') return $this->repository->findAll();
->andWhere('c.exampleField = :val') }
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult() /**
* @return array|CalendarRange[]
*/
public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null)
{
return $this->repository->findBy($criteria, $orderBy, $limit, $offset);
}
public function findOneBy(array $criteria): ?CalendarRange
{
return $this->repository->findOneBy($criteria);
}
public function getClassName(): string
{
return CalendarRange::class;
}
public function countByAvailableRangesForUser(User $user, \DateTimeImmutable $from, \DateTimeImmutable $to): int
{
return $this->buildQueryAvailableRangesForUser($user, $from, $to)
->select('COUNT(cr)')
->getQuery()->getSingleScalarResult();
}
/**
* @return array|CalendarRange[]
*/
public function findByAvailableRangesForUser(
User $user,
\DateTimeImmutable $from,
\DateTimeImmutable $to,
?int $limit = null,
?int $offset = null
): array {
$qb = $this->buildQueryAvailableRangesForUser($user, $from, $to);
if ($limit !== null) {
$qb->setMaxResults($limit);
}
if ($offset !== null) {
$qb->setFirstResult($offset);
}
return $qb->getQuery()->getResult();
}
private function buildQueryAvailableRangesForUser(User $user, \DateTimeImmutable $from, \DateTimeImmutable $to): QueryBuilder
{
$qb = $this->repository->createQueryBuilder('cr');
return $qb
->where(
$qb->expr()->andX(
$qb->expr()->eq('cr.user', ':user'),
$qb->expr()->gte('cr.startDate', ':startDate'),
$qb->expr()->lte('cr.endDate', ':endDate'),
$qb->expr()->eq(0, 'SIZE(cr.calendars)')
)
)
->setParameters([
'user' => $user,
'startDate' => $from,
'endDate' => $to,
])
; ;
} }
*/
} }

View File

@ -170,12 +170,35 @@ paths:
description: "OK" description: "OK"
422: 422:
description: "object with validation errors" description: "object with validation errors"
/1.0/calendar/calendar-range-available.json: /1.0/calendar/calendar-range-available/{userId}.json:
get: get:
tags: tags:
- calendar - calendar
summary: Return a list of available calendar range items. Available means calendar-range not being taken by a calendar entity summary: Return a list of available calendar range items. Available means calendar-range not being taken by a calendar entity
parameters:
- name: userId
in: path
required: true
description: The user id
schema:
type: integer
format: integer
minimum: 1
- name: dateFrom
in: query
required: true
description: The date from, formatted as ISO8601 string
schema:
type: string
format: date-time
- name: dateTo
in: query
required: true
description: The date to, formatted as ISO8601 string
schema:
type: string
format: date-time
responses: responses:
200: 200:
description: "ok" description: "ok"