From ea8893682accd65998b3927ce9415c9ce253e32f Mon Sep 17 00:00:00 2001 From: Iris Olfermann Date: Wed, 28 Aug 2024 14:48:30 +0200 Subject: [PATCH] fixed backend structure of events works but destroyed join and leave methods --- .../MealBundle/Controller/ApiController.php | 15 ++++++---- .../MealBundle/Entity/EventParticipation.php | 10 +++++-- src/Mealz/MealBundle/Service/ApiService.php | 10 ++++++- .../Service/EventParticipationService.php | 13 +++++++-- src/Resources/src/api/getDashboardData.ts | 2 +- .../src/components/dashboard/EventData.vue | 19 ++++++------ src/Resources/src/stores/dashboardStore.ts | 29 +++++++++---------- src/Resources/src/stores/eventsStore.ts | 2 +- 8 files changed, 61 insertions(+), 39 deletions(-) diff --git a/src/Mealz/MealBundle/Controller/ApiController.php b/src/Mealz/MealBundle/Controller/ApiController.php index 775c06d51..23ea58481 100644 --- a/src/Mealz/MealBundle/Controller/ApiController.php +++ b/src/Mealz/MealBundle/Controller/ApiController.php @@ -104,13 +104,16 @@ public function getDashboardData(ParticipationCountService $partCountSrv): JsonR 'slots' => [], 'meals' => [], 'isEnabled' => $day->isEnabled(), - 'events' => [], + 'events' => $this->apiSrv->getEventParticipationsData($day, $profile), ]; - $events = []; - foreach($day->getEvents() as $event){ - $events = $this->setEventData($event, $events); - } - $response[$week->getId()]['days'][$day->getId()]['events'] = $events; + // $events = []; + // // if($day->getId() == 1111){ + // // var_dump($this->apiSrv->getEventParticipationData($day, $profile)); + // // } + // foreach($day->getEvents() as $event){ + // $events = $this->setEventData($event, $events); + // } + // $response[$week->getId()]['days'][$day->getId()]['events'] = $events; $this->addSlots($response[$week->getId()]['days'][$day->getId()]['slots'], $slots, $day, $activeParticipations); /** @var Meal $meal */ foreach ($day->getMeals() as $meal) { diff --git a/src/Mealz/MealBundle/Entity/EventParticipation.php b/src/Mealz/MealBundle/Entity/EventParticipation.php index 18087531b..42d2c8b12 100644 --- a/src/Mealz/MealBundle/Entity/EventParticipation.php +++ b/src/Mealz/MealBundle/Entity/EventParticipation.php @@ -30,14 +30,15 @@ class EventParticipation #[ORM\JoinColumn(name: 'event', referencedColumnName: 'id')] public Event $event; - #[ORM\OneToMany(mappedBy: 'event', targetEntity: Participant::class)] + #[ORM\OneToMany(mappedBy: 'event', targetEntity: Participant::class, cascade: ['persist', 'remove'])] public ?Collection $participants = null; + public function __construct(Day $day, Event $event, ?Collection $participants = null) { $this->day = $day; $this->event = $event; - $this->participants = $participants; + $this->participants = $participants ?? new ArrayCollection(); } public function getId(): ?int @@ -80,6 +81,11 @@ public function getParticipant(Profile $profile): ?Participant return null; } + public function setParticipant(Participant $participant) : void + { + $this->participants->add($participant); + } + public function getParticipants(): ArrayCollection { if (null === $this->participants) { diff --git a/src/Mealz/MealBundle/Service/ApiService.php b/src/Mealz/MealBundle/Service/ApiService.php index 201ab60d9..8c64685ff 100644 --- a/src/Mealz/MealBundle/Service/ApiService.php +++ b/src/Mealz/MealBundle/Service/ApiService.php @@ -124,7 +124,15 @@ public function isParamValid($parameters, $key, $type): bool public function getEventParticipationData(Day $day, ?Profile $profile = null): ?array { - return $this->eventPartSrv->getEventParticipationData($day, $profile); + return $this->eventPartSrv->getEventParticipationData($day,null, $profile); + } + public function getEventParticipationsData(Day $day, ?Profile $profile = null): ?array + { + $participations = []; + foreach($day->getEvents() as $eventParticipation){ + $participations[] = $this->eventPartSrv->getEventParticipationData($day,$eventParticipation->getId(), $profile); + } + return $participations; } /** diff --git a/src/Mealz/MealBundle/Service/EventParticipationService.php b/src/Mealz/MealBundle/Service/EventParticipationService.php index 114f7b87d..411d8728a 100644 --- a/src/Mealz/MealBundle/Service/EventParticipationService.php +++ b/src/Mealz/MealBundle/Service/EventParticipationService.php @@ -12,6 +12,8 @@ use Doctrine\ORM\EntityManagerInterface; use Exception; +use function Amp\Iterator\toArray; + class EventParticipationService { private Doorman $doorman; @@ -56,7 +58,7 @@ public function handleEventParticipation(Day $day, EventParticipation $event): v public function getEventParticipationData(Day $day, ?int $eventId = null, ?Profile $profile = null,): ?array { if($eventId === null){ - return $day->getEvents(); + return $day->getEvents()->toArray(); } else{ $eventParticipation = $day->getEvent($day,$eventId); if (null === $eventParticipation) { @@ -178,9 +180,14 @@ private function removeEventFromDay(Day $day, EventParticipation $event): void $day->removeEvent($event); } - private function createEventParticipation(Profile $profile, EventParticipation $eventParticiation): Participant + private function createEventParticipation(Profile $profile, EventParticipation $eventParticipation): Participant { - return new Participant($profile, null, $eventParticiation); + $participant = new Participant($profile, null, $eventParticipation); + $eventParticipation->setParticipant($participant); + $this->em->persist($participant); + $this->em->persist($eventParticipation); + $this->em->flush(); + return $participant; } private function getParticipantName(Participant $participant): string diff --git a/src/Resources/src/api/getDashboardData.ts b/src/Resources/src/api/getDashboardData.ts index 44505795a..8d7032fd2 100644 --- a/src/Resources/src/api/getDashboardData.ts +++ b/src/Resources/src/api/getDashboardData.ts @@ -40,7 +40,7 @@ export type Slot = { }; export type EventParticipation = { - eventId: number; + id: number; event: Event; day: Date; participationId: number; diff --git a/src/Resources/src/components/dashboard/EventData.vue b/src/Resources/src/components/dashboard/EventData.vue index 1a59ccd0c..fd592bf03 100644 --- a/src/Resources/src/components/dashboard/EventData.vue +++ b/src/Resources/src/components/dashboard/EventData.vue @@ -28,7 +28,7 @@ - {{ event.event.title }} + {{ getEventById(event?.id ?? -1)?.title }}
@@ -62,7 +62,7 @@