Skip to content

Commit

Permalink
fixed eventParticipation collection in Controller, changed getEvents(…
Browse files Browse the repository at this point in the history
…) and added getEvent(),changed DataFixtures, updated Day.php
  • Loading branch information
IrisOlfermann committed Jul 26, 2024
1 parent a29a5e5 commit 0477a42
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 49 deletions.
7 changes: 5 additions & 2 deletions src/Mealz/MealBundle/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion src/Mealz/MealBundle/Controller/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
8 changes: 5 additions & 3 deletions src/Mealz/MealBundle/Controller/MealAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Check warning on line 205 in src/Mealz/MealBundle/Controller/MealAdminController.php

View workflow job for this annotation

GitHub Actions / PHPMD

Avoid unused local variables such as '$eventCollection'.
/*
* 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
Expand Down Expand Up @@ -233,8 +232,11 @@ private function handleNewDay($dayData, Day $day): void
}

$this->setLockParticipationForDay($day, $dayData);
$this->mealAdminHelper->handleEventParticipation($day, $dayData['event']);

$eventCollection = $day['events'];

Check failure on line 236 in src/Mealz/MealBundle/Controller/MealAdminController.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis

UndefinedMethod

src/Mealz/MealBundle/Controller/MealAdminController.php:236:28: UndefinedMethod: Method App\Mealz\MealBundle\Entity\Day::offsetGet does not exist (see https://psalm.dev/022)
foreach($eventCollection as $event){
$this->mealAdminHelper->handleEventParticipation($day, $event);
}
$mealCollection = $dayData['meals'];
// max 2 main meals allowed
if (2 < count($mealCollection)) {
Expand Down
5 changes: 5 additions & 0 deletions src/Mealz/MealBundle/DataFixtures/ORM/LoadEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 19 additions & 0 deletions src/Mealz/MealBundle/Entity/Day.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/Mealz/MealBundle/Helper/MealAdminHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
}
4 changes: 2 additions & 2 deletions src/Mealz/MealBundle/Service/ApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {

Check failure on line 137 in src/Mealz/MealBundle/Service/ApiService.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis

TypeDoesNotContainNull

src/Mealz/MealBundle/Service/ApiService.php:137:13: TypeDoesNotContainNull: App\Mealz\MealBundle\Entity\EventCollection does not contain null (see https://psalm.dev/090)
return null;
}

return [
'name' => $day->getEvent()->getEvent()->getTitle(),
'name' => $day->getEvents()->getEvent()->getTitle(),
'participants' => $this->getEventParticipants($day),
];
}
Expand Down
62 changes: 31 additions & 31 deletions src/Mealz/MealBundle/Service/EventParticipationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions src/Resources/src/stores/weeksStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,17 @@ export interface SimpleDay {
lockParticipationDateTime: DateTime;
week: number;
meals: Dictionary<SimpleMeal[]>;
event: number | null;
events: Dictionary<SimpleEvent[]>;
enabled: boolean;
}

export interface SimpleEvent {
id: number,
event: string,
day: number,
participations: [],
}

export interface SimpleMeal {
id: number;
dish: string;
Expand Down Expand Up @@ -238,7 +245,7 @@ export function useWeeks() {
meals: {},
id: parseInt(dayId),
enabled: day.enabled,
event: day.event,
events: {},
date: day.dateTime,
lockDate: day.lockParticipationDateTime
};
Expand Down
6 changes: 0 additions & 6 deletions src/Resources/tests/unit/fixtures/getEvents.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@
"slug": "afterwork",
"public": true
},
{
"id": 48,
"title": "Lunch Roulette",
"slug": "lunch-roulette",
"public": true
},
{
"id": 49,
"title": "Alumni Afterwork",
Expand Down

0 comments on commit 0477a42

Please sign in to comment.