From 0477a428e12e7441e5386e76e5b8d0166e16dfc0 Mon Sep 17 00:00:00 2001 From: Iris Olfermann Date: Fri, 26 Jul 2024 13:57:40 +0200 Subject: [PATCH] fixed eventParticipation collection in Controller, changed getEvents() and added getEvent(),changed DataFixtures, updated Day.php --- .../MealBundle/Controller/ApiController.php | 7 ++- .../MealBundle/Controller/EventController.php | 2 +- .../Controller/MealAdminController.php | 8 ++- .../DataFixtures/ORM/LoadEvents.php | 5 ++ src/Mealz/MealBundle/Entity/Day.php | 19 ++++++ .../MealBundle/Helper/MealAdminHelper.php | 5 +- src/Mealz/MealBundle/Service/ApiService.php | 4 +- .../Service/EventParticipationService.php | 62 +++++++++---------- src/Resources/src/stores/weeksStore.ts | 11 +++- .../tests/unit/fixtures/getEvents.json | 6 -- 10 files changed, 80 insertions(+), 49 deletions(-) diff --git a/src/Mealz/MealBundle/Controller/ApiController.php b/src/Mealz/MealBundle/Controller/ApiController.php index dad7bef94..04daceda4 100644 --- a/src/Mealz/MealBundle/Controller/ApiController.php +++ b/src/Mealz/MealBundle/Controller/ApiController.php @@ -18,6 +18,7 @@ use App\Mealz\MealBundle\Service\SlotService; use App\Mealz\MealBundle\Service\WeekService; use App\Mealz\UserBundle\Entity\Profile; +use App\Mealz\MealBundle\Service\EventParticipationService; use DateTime; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; @@ -31,6 +32,7 @@ class ApiController extends BaseController private ApiService $apiSrv; private OfferService $offerSrv; private GuestParticipationService $guestPartiSrv; + private EventParticipationService $eventService; public function __construct( DishService $dishSrv, @@ -102,7 +104,7 @@ public function getDashboardData(ParticipationCountService $partCountSrv): JsonR 'slots' => [], 'meals' => [], 'isEnabled' => $day->isEnabled(), - 'event' => $this->apiSrv->getEventParticipationData($day, $profile), + 'events' => [], ]; $this->addSlots($response[$week->getId()]['days'][$day->getId()]['slots'], $slots, $day, $activeParticipations); @@ -169,7 +171,8 @@ public function list(): JsonResponse $list['meals'] = $list['meals'] + $this->getDishData($meal); } - $list['event'] = $this->apiSrv->getEventParticipationInfo($day); + $events = $this->eventService->getEventParticipationData($day); + $list['events'] = [$events]; $list['day'] = $day->getDateTime(); diff --git a/src/Mealz/MealBundle/Controller/EventController.php b/src/Mealz/MealBundle/Controller/EventController.php index 6f496e312..18bcfb523 100644 --- a/src/Mealz/MealBundle/Controller/EventController.php +++ b/src/Mealz/MealBundle/Controller/EventController.php @@ -35,7 +35,7 @@ public function __construct( public function getEventList(): JsonResponse { $events = $this->eventRepository->findBy(['deleted' => 0]); - + $this->logger->info('Anzahl Events:' . count($events) ); return new JsonResponse($events, Response::HTTP_OK); } diff --git a/src/Mealz/MealBundle/Controller/MealAdminController.php b/src/Mealz/MealBundle/Controller/MealAdminController.php index a3cae6020..577e44c8a 100644 --- a/src/Mealz/MealBundle/Controller/MealAdminController.php +++ b/src/Mealz/MealBundle/Controller/MealAdminController.php @@ -201,9 +201,8 @@ private function handleDay(array $day): void } $this->setLockParticipationForDay($dayEntity, $day); - $this->mealAdminHelper->handleEventParticipation($dayEntity, $day['event']); - $mealCollection = $day['meals']; + $eventCollection = $day['events']; /* * 3 Meals are comprised of 2 main meals and a potential combined meal. * The combined meal is also in the collection, because meals that are @@ -233,8 +232,11 @@ private function handleNewDay($dayData, Day $day): void } $this->setLockParticipationForDay($day, $dayData); - $this->mealAdminHelper->handleEventParticipation($day, $dayData['event']); + $eventCollection = $day['events']; + foreach($eventCollection as $event){ + $this->mealAdminHelper->handleEventParticipation($day, $event); + } $mealCollection = $dayData['meals']; // max 2 main meals allowed if (2 < count($mealCollection)) { diff --git a/src/Mealz/MealBundle/DataFixtures/ORM/LoadEvents.php b/src/Mealz/MealBundle/DataFixtures/ORM/LoadEvents.php index 87de9634c..ea536f8ec 100644 --- a/src/Mealz/MealBundle/DataFixtures/ORM/LoadEvents.php +++ b/src/Mealz/MealBundle/DataFixtures/ORM/LoadEvents.php @@ -29,6 +29,11 @@ public function load(ObjectManager $manager): void 'public' => true, 'slug' => 'alumni-afterwork', ], + [ + 'title' => 'Lunch Roulette', + 'public' => true, + 'slug' => 'lunch-roulette', + ] ]; foreach ($eventItems as $item) { diff --git a/src/Mealz/MealBundle/Entity/Day.php b/src/Mealz/MealBundle/Entity/Day.php index ee531b586..be31ad14b 100644 --- a/src/Mealz/MealBundle/Entity/Day.php +++ b/src/Mealz/MealBundle/Entity/Day.php @@ -92,6 +92,25 @@ public function getEvents(): EventCollection return new EventCollection($this->events->toArray()); } + public function getEvent(Day $day, int $id):EventParticipation | null{ + foreach ($this->getEvents() as $event) { + if($event->getId() === $id && $event->getDay() == $day){ + return $event; + } + } + return null; + } + + public function setEvent(EventParticipation $event){ + $this->events->add($event); + } + + public function removeEvent(EventParticipation $event){ + if($this->events->contains($event)){ + $this->events->removeElement($event); + } + } + public function setEvents(EventCollection $events): void { $this->events = $events; diff --git a/src/Mealz/MealBundle/Helper/MealAdminHelper.php b/src/Mealz/MealBundle/Helper/MealAdminHelper.php index b068fe572..682746745 100644 --- a/src/Mealz/MealBundle/Helper/MealAdminHelper.php +++ b/src/Mealz/MealBundle/Helper/MealAdminHelper.php @@ -5,6 +5,7 @@ namespace App\Mealz\MealBundle\Helper; use App\Mealz\MealBundle\Entity\Day; +use App\Mealz\MealBundle\Entity\EventParticipation; use App\Mealz\MealBundle\Entity\Meal; use App\Mealz\MealBundle\Service\EventParticipationService; @@ -31,8 +32,8 @@ public function setParticipationLimit(Meal $mealEntity, array $meal): void } } - public function handleEventParticipation(Day $day, ?int $eventId = null): void + public function handleEventParticipation(Day $day, EventParticipation $event): void { - $this->eventService->handleEventParticipation($day, $eventId); + $this->eventService->handleEventParticipation($day, $event); } } diff --git a/src/Mealz/MealBundle/Service/ApiService.php b/src/Mealz/MealBundle/Service/ApiService.php index 7f70ae07f..33758b41d 100644 --- a/src/Mealz/MealBundle/Service/ApiService.php +++ b/src/Mealz/MealBundle/Service/ApiService.php @@ -134,12 +134,12 @@ public function getEventParticipationData(Day $day, ?Profile $profile = null): ? */ public function getEventParticipationInfo(Day $day): ?array { - if (null === $day->getEvent()) { + if (null === $day->getEvents()) { return null; } return [ - 'name' => $day->getEvent()->getEvent()->getTitle(), + 'name' => $day->getEvents()->getEvent()->getTitle(), 'participants' => $this->getEventParticipants($day), ]; } diff --git a/src/Mealz/MealBundle/Service/EventParticipationService.php b/src/Mealz/MealBundle/Service/EventParticipationService.php index 7e1cc3451..a81b71ad5 100644 --- a/src/Mealz/MealBundle/Service/EventParticipationService.php +++ b/src/Mealz/MealBundle/Service/EventParticipationService.php @@ -39,12 +39,12 @@ public function __construct( * if an eventId is passed in as a parameter. If no eventId is present * the eventparticipation will get removed from the day. */ - public function handleEventParticipation(Day $day, ?int $eventId = null): void + public function handleEventParticipation(Day $day, EventParticipation $event): void { - if (null === $eventId) { - $this->removeEventFromDay($day); + if (null === $event) { + $this->removeEventFromDay($day, $event->getId()); } else { - $event = $this->eventRepo->find($eventId); + $event = $this->eventRepo->find($event->getId()); $this->addEventToDay($day, $event); } } @@ -54,25 +54,28 @@ public function handleEventParticipation(Day $day, ?int $eventId = null): void * * @psalm-return array{eventId: int, participationId: int|null, participations: int, isPublic: bool, isParticipating?: bool}|null */ - public function getEventParticipationData(Day $day, ?Profile $profile = null): ?array + public function getEventParticipationData(Day $day, ?int $eventId = null, ?Profile $profile = null,): ?array { - $eventParticipation = $day->getEvent(); - if (null === $eventParticipation) { - return null; - } - - $participationData = [ - 'eventId' => $eventParticipation->getEvent()->getId(), - 'participationId' => $eventParticipation->getId(), - 'participations' => count($eventParticipation->getParticipants()), - 'isPublic' => $eventParticipation->getEvent()->isPublic(), - ]; - - if (null !== $profile) { - $participationData['isParticipating'] = null !== $eventParticipation->getParticipant($profile); - } - - return $participationData; + if($eventId === null){ + return $day->getEvents(); + } else{ + $eventParticipation = $day->getEvent($day,$eventId); + if (null === $eventParticipation) { + return null; + } + $participationData = [ + 'eventId' => $eventParticipation->getEvent()->getId(), + 'participationId' => $eventParticipation->getId(), + 'participations' => count($eventParticipation->getParticipants()), + 'isPublic' => $eventParticipation->getEvent()->isPublic(), + ]; + + if (null !== $profile) { + $participationData['isParticipating'] = null !== $eventParticipation->getParticipant($profile); + } + + return $participationData; + } } public function join(Profile $profile, Day $day): ?EventParticipation @@ -157,24 +160,21 @@ public function getParticipants(Day $day): array ); } + /** + * adds new event to the eventCollection + */ private function addEventToDay(Day $day, ?Event $event): void { // new eventparticipation - if (null !== $event && null === $day->getEvent()) { + if (null !== $event && null === $day->getEvents()) { $eventParticipation = new EventParticipation($day, $event); $day->setEvent($eventParticipation); - } elseif (null !== $event && $day->getEvent()->getEvent()->getId() !== $event->getId()) { - // edit eventparticipation - $day->getEvent()->setEvent($event); } } - private function removeEventFromDay(Day $day): void + private function removeEventFromDay(Day $day, EventParticipation $event): void { - if (null !== $day->getEvent()) { - $this->em->remove($day->getEvent()); - $day->setEvent(null); - } + $day->removeEvent($event); } private function createEventParticipation(Profile $profile, EventParticipation $eventParticiation): Participant diff --git a/src/Resources/src/stores/weeksStore.ts b/src/Resources/src/stores/weeksStore.ts index b64a33043..a9a8cb095 100644 --- a/src/Resources/src/stores/weeksStore.ts +++ b/src/Resources/src/stores/weeksStore.ts @@ -26,10 +26,17 @@ export interface SimpleDay { lockParticipationDateTime: DateTime; week: number; meals: Dictionary; - event: number | null; + events: Dictionary; enabled: boolean; } +export interface SimpleEvent { + id: number, + event: string, + day: number, + participations: [], +} + export interface SimpleMeal { id: number; dish: string; @@ -238,7 +245,7 @@ export function useWeeks() { meals: {}, id: parseInt(dayId), enabled: day.enabled, - event: day.event, + events: {}, date: day.dateTime, lockDate: day.lockParticipationDateTime }; diff --git a/src/Resources/tests/unit/fixtures/getEvents.json b/src/Resources/tests/unit/fixtures/getEvents.json index 9b5abb31d..27bb1efd3 100644 --- a/src/Resources/tests/unit/fixtures/getEvents.json +++ b/src/Resources/tests/unit/fixtures/getEvents.json @@ -5,12 +5,6 @@ "slug": "afterwork", "public": true }, - { - "id": 48, - "title": "Lunch Roulette", - "slug": "lunch-roulette", - "public": true - }, { "id": 49, "title": "Alumni Afterwork",