From 20a4950c60a6d493f7d4b637e68ed50f17d57284 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Sun, 23 Jan 2022 13:23:50 +0100 Subject: [PATCH] accompanying period work list for my, near end date --- .../AccompanyingCourseWorkApiController.php | 32 ++++++++++++++ .../AccompanyingPeriodWorkRepository.php | 42 +++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseWorkApiController.php b/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseWorkApiController.php index 95cbaf023..7b6013019 100644 --- a/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseWorkApiController.php +++ b/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseWorkApiController.php @@ -12,10 +12,21 @@ declare(strict_types=1); namespace Chill\PersonBundle\Controller; use Chill\MainBundle\CRUD\Controller\ApiController; +use Chill\MainBundle\Serializer\Model\Collection; +use Chill\PersonBundle\Repository\AccompanyingPeriod\AccompanyingPeriodWorkRepository; +use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\Routing\Annotation\Route; class AccompanyingCourseWorkApiController extends ApiController { + private AccompanyingPeriodWorkRepository $accompanyingPeriodWorkRepository; + + public function __construct(AccompanyingPeriodWorkRepository $accompanyingPeriodWorkRepository) + { + $this->accompanyingPeriodWorkRepository = $accompanyingPeriodWorkRepository; + } + protected function getContextForSerialization(string $action, Request $request, string $_format, $entity): array { switch ($action) { @@ -28,4 +39,25 @@ class AccompanyingCourseWorkApiController extends ApiController return parent::getContextForSerialization($action, $request, $_format, $entity); } + + /** + * @Route("/api/1.0/person/accompanying-period/work/my-near-end") + * + * @param Request $request + * @return JsonResponse + */ + public function myWorksNearEndDate(Request $request): JsonResponse + { + $since = (new \DateTimeImmutable('now'))->sub(new \DateInterval('P15D')); + $until = (new \DateTimeImmutable('now'))->add(new \DateInterval('P15D')); + $total = $this->accompanyingPeriodWorkRepository + ->countNearEndDateByUser($this->getUser(), $since, $until); + $paginator = $this->getPaginatorFactory()->create($total); + $works = $this->accompanyingPeriodWorkRepository + ->findNearEndDateByUser($this->getUser(), $since, $until, $paginator->getItemsPerPage(), $paginator->getCurrentPageFirstItemNumber()); + + $collection = new Collection($works, $paginator); + + return $this->json($collection, 200, [], ['groups' => ['read']]); + } } diff --git a/src/Bundle/ChillPersonBundle/Repository/AccompanyingPeriod/AccompanyingPeriodWorkRepository.php b/src/Bundle/ChillPersonBundle/Repository/AccompanyingPeriod/AccompanyingPeriodWorkRepository.php index 23ccf57a9..b7ea9459e 100644 --- a/src/Bundle/ChillPersonBundle/Repository/AccompanyingPeriod/AccompanyingPeriodWorkRepository.php +++ b/src/Bundle/ChillPersonBundle/Repository/AccompanyingPeriod/AccompanyingPeriodWorkRepository.php @@ -11,10 +11,12 @@ declare(strict_types=1); namespace Chill\PersonBundle\Repository\AccompanyingPeriod; +use Chill\MainBundle\Entity\User; use Chill\PersonBundle\Entity\AccompanyingPeriod; use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWork; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityRepository; +use Doctrine\ORM\QueryBuilder; use Doctrine\Persistence\ObjectRepository; final class AccompanyingPeriodWorkRepository implements ObjectRepository @@ -111,4 +113,44 @@ final class AccompanyingPeriodWorkRepository implements ObjectRepository return $qb; } + + public function findNearEndDateByUser(User $user, \DateTimeImmutable $since, \DateTimeImmutable $until, int $limit = 20, int $offset = 0): array + { + return $this->buildQueryNearEndDateByUser($user, $since, $until) + ->select('w') + ->setFirstResult($offset) + ->setMaxResults($limit) + ->getQuery() + ->getResult(); + } + + public function countNearEndDateByUser(User $user, \DateTimeImmutable $since, \DateTimeImmutable $until): int + { + return $this->buildQueryNearEndDateByUser($user, $since, $until) + ->select('count(w)')->getQuery()->getSingleScalarResult(); + } + + public function buildQueryNearEndDateByUser(User $user, \DateTimeImmutable $since, \DateTimeImmutable $until): QueryBuilder + { + $qb = $this->repository->createQueryBuilder('w'); + + $qb + ->join('w.accompanyingPeriod', 'period') + ->where( + $qb->expr()->andX( + $qb->expr()->eq('period.user', ':user'), + $qb->expr()->gte('w.endDate', ':since'), + $qb->expr()->lte('w.startDate', ':until') + ) + ) + ->setParameters([ + 'user' => $user, + 'since' => $since, + 'until' => $until + ]); + + return $qb; + } + + }