From ce912e44059f2e2a3b3a37520523ff58d0d264e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Thu, 27 Jan 2022 17:28:59 +0100 Subject: [PATCH] ajout compteurs --- .../Controller/NotificationApiController.php | 14 +++++++-- .../Serializer/Model/Counter.php | 31 +++++++++++++++++++ .../AccompanyingCourseWorkApiController.php | 11 +++++++ ...nyingPeriodWorkEvaluationApiController.php | 11 +++++++ .../Controller/SingleTaskController.php | 10 +++++- 5 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 src/Bundle/ChillMainBundle/Serializer/Model/Counter.php diff --git a/src/Bundle/ChillMainBundle/Controller/NotificationApiController.php b/src/Bundle/ChillMainBundle/Controller/NotificationApiController.php index c50564d58..c43015efd 100644 --- a/src/Bundle/ChillMainBundle/Controller/NotificationApiController.php +++ b/src/Bundle/ChillMainBundle/Controller/NotificationApiController.php @@ -17,9 +17,11 @@ use Chill\MainBundle\Pagination\PaginatorFactory; use Chill\MainBundle\Repository\NotificationRepository; use Chill\MainBundle\Security\Authorization\NotificationVoter; use Chill\MainBundle\Serializer\Model\Collection; +use Chill\MainBundle\Serializer\Model\Counter; use Doctrine\ORM\EntityManagerInterface; use RuntimeException; use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Security\Core\Exception\AccessDeniedException; use Symfony\Component\Security\Core\Security; @@ -74,16 +76,24 @@ class NotificationApiController /** * @Route("/my/unread") */ - public function myUnreadNotifications(): JsonResponse + public function myUnreadNotifications(Request $request): JsonResponse { $total = $this->notificationRepository->countUnreadByUser($this->security->getUser()); + + if ($request->query->getBoolean('countOnly')) { + return new JsonResponse( + $this->serializer->serialize(new Counter($total), 'json', ['groups' => ['read']]), + JsonResponse::HTTP_OK, + [], + true + ); + } $paginator = $this->paginatorFactory->create($total); $notifications = $this->notificationRepository->findUnreadByUser( $this->security->getUser(), $paginator->getItemsPerPage(), $paginator->getCurrentPageFirstItemNumber() ); - dump($notifications); $collection = new Collection($notifications, $paginator); return new JsonResponse( diff --git a/src/Bundle/ChillMainBundle/Serializer/Model/Counter.php b/src/Bundle/ChillMainBundle/Serializer/Model/Counter.php new file mode 100644 index 000000000..e0b471e71 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Serializer/Model/Counter.php @@ -0,0 +1,31 @@ +counter = $counter; + } + + public function getCounter(): ?int + { + return $this->counter; + } + + public function setCounter(?int $counter): Counter + { + $this->counter = $counter; + return $this; + } + + public function jsonSerialize() + { + return ['count' => $this->counter]; + } + + +} diff --git a/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseWorkApiController.php b/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseWorkApiController.php index 7f3caa675..fb25a3ff7 100644 --- a/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseWorkApiController.php +++ b/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseWorkApiController.php @@ -13,6 +13,7 @@ namespace Chill\PersonBundle\Controller; use Chill\MainBundle\CRUD\Controller\ApiController; use Chill\MainBundle\Serializer\Model\Collection; +use Chill\MainBundle\Serializer\Model\Counter; use Chill\PersonBundle\Repository\AccompanyingPeriod\AccompanyingPeriodWorkRepository; use DateInterval; use DateTimeImmutable; @@ -40,6 +41,16 @@ class AccompanyingCourseWorkApiController extends ApiController ->add(new DateInterval('P' . $request->query->getInt('since', 15) . 'D')); $total = $this->accompanyingPeriodWorkRepository ->countNearEndDateByUser($this->getUser(), $since, $until); + + if ($request->query->getBoolean('countOnly', false)) { + return $this->json( + new Counter($total), + JsonResponse::HTTP_OK, + [], + ['groups' => ['read']] + ); + } + $paginator = $this->getPaginatorFactory()->create($total); $works = $this->accompanyingPeriodWorkRepository ->findNearEndDateByUser($this->getUser(), $since, $until, $paginator->getItemsPerPage(), $paginator->getCurrentPageFirstItemNumber()); diff --git a/src/Bundle/ChillPersonBundle/Controller/AccompanyingPeriodWorkEvaluationApiController.php b/src/Bundle/ChillPersonBundle/Controller/AccompanyingPeriodWorkEvaluationApiController.php index e82877637..02d97b58f 100644 --- a/src/Bundle/ChillPersonBundle/Controller/AccompanyingPeriodWorkEvaluationApiController.php +++ b/src/Bundle/ChillPersonBundle/Controller/AccompanyingPeriodWorkEvaluationApiController.php @@ -15,6 +15,7 @@ use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\DocGeneratorBundle\Repository\DocGeneratorTemplateRepository; use Chill\MainBundle\Pagination\PaginatorFactory; use Chill\MainBundle\Serializer\Model\Collection; +use Chill\MainBundle\Serializer\Model\Counter; use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation; use Chill\PersonBundle\Entity\SocialWork\Evaluation; use Chill\PersonBundle\Repository\AccompanyingPeriod\AccompanyingPeriodWorkEvaluationRepository; @@ -95,6 +96,16 @@ class AccompanyingPeriodWorkEvaluationApiController { $total = $this->accompanyingPeriodWorkEvaluationRepository ->countNearMaxDateByUser($this->security->getUser()); + + if ($request->query->getBoolean('countOnly', false)) { + return new JsonResponse( + $this->serializer->serialize(new Counter($total), 'json', ['groups' => 'read']), + JsonResponse::HTTP_OK, + [], + true + ); + } + $paginator = $this->paginatorFactory->create($total); $works = $this->accompanyingPeriodWorkEvaluationRepository ->findNearMaxDateByUser( diff --git a/src/Bundle/ChillTaskBundle/Controller/SingleTaskController.php b/src/Bundle/ChillTaskBundle/Controller/SingleTaskController.php index 61ca37ba0..b4b6727b2 100644 --- a/src/Bundle/ChillTaskBundle/Controller/SingleTaskController.php +++ b/src/Bundle/ChillTaskBundle/Controller/SingleTaskController.php @@ -14,6 +14,7 @@ namespace Chill\TaskBundle\Controller; use Chill\MainBundle\Pagination\PaginatorFactory; use Chill\MainBundle\Security\Resolver\CenterResolverDispatcherInterface; use Chill\MainBundle\Serializer\Model\Collection; +use Chill\MainBundle\Serializer\Model\Counter; use Chill\MainBundle\Templating\Listing\FilterOrderHelper; use Chill\MainBundle\Templating\Listing\FilterOrderHelperFactoryInterface; use Chill\MainBundle\Timeline\TimelineBuilder; @@ -442,7 +443,7 @@ final class SingleTaskController extends AbstractController * defaults={"_format": "json"} * ) */ - public function myTasksAction(string $_format) + public function myTasksAction(string $_format, Request $request) { $this->denyAccessUnlessGranted('ROLE_USER'); @@ -455,6 +456,13 @@ final class SingleTaskController extends AbstractController $filterOrder->getQueryString(), $flags ); + + if ('json' === $_format && $request->query->getBoolean('countOnly')) { + return $this->json( + new Counter($nb), + ); + } + $paginator = $this->paginatorFactory->create($nb); $tasks = $this->singleTaskAclAwareRepository->findByCurrentUsersTasks( $filterOrder->getQueryString(),