Skip to content

Commit

Permalink
event is input into the database, just one event at a time works at t…
Browse files Browse the repository at this point in the history
…he moment
  • Loading branch information
IrisOlfermann committed Jul 31, 2024
1 parent 0873ff8 commit ab1a956
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 36 deletions.
36 changes: 29 additions & 7 deletions src/Mealz/MealBundle/Controller/MealAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace App\Mealz\MealBundle\Controller;

use App\Mealz\MealBundle\Entity\Day;
use App\Mealz\MealBundle\Entity\Event;
use App\Mealz\MealBundle\Entity\EventParticipation;
use App\Mealz\MealBundle\Entity\Dish;
use App\Mealz\MealBundle\Entity\Meal;
use App\Mealz\MealBundle\Entity\Week;
Expand All @@ -24,6 +26,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Psr\Log\LoggerInterface;

#[IsGranted('ROLE_KITCHEN_STAFF')]
class MealAdminController extends BaseController

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

View workflow job for this annotation

GitHub Actions / PHPMD

The class MealAdminController has an overall complexity of 57 which is very high. The configured complexity threshold is 55.
Expand All @@ -37,7 +40,8 @@ public function __construct(
private readonly DayService $dayService,
private readonly DishService $dishService,
private readonly EntityManagerInterface $em,
private readonly MealAdminHelper $mealAdminHelper
private readonly MealAdminHelper $mealAdminHelper,
private readonly LoggerInterface $logger
) {
}

Expand Down Expand Up @@ -141,7 +145,6 @@ public function edit(Request $request, Week $week): JsonResponse
) {
return new JsonResponse(['message' => '101: invalid json'], Response::HTTP_INTERNAL_SERVER_ERROR);
}

$days = $data['days'];
$week->setEnabled($data['enabled']);

Expand Down Expand Up @@ -199,10 +202,14 @@ private function handleDay(array $day): void
if (null !== $day['enabled']) {
$dayEntity->setEnabled($day['enabled']);
}

$this->setLockParticipationForDay($dayEntity, $day);
$mealCollection = $day['meals'];

$eventCollection = $day['events'];
foreach ($eventCollection as $event) {
$this->handleEventArr($event, $dayEntity);
}

$mealCollection = $day['meals'];
/*
* 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,9 +240,9 @@ private function handleNewDay($dayData, Day $day): void

$this->setLockParticipationForDay($day, $dayData);

$eventCollection = $day['events'];
foreach($eventCollection as $event){
$this->mealAdminHelper->handleEventParticipation($day, $event);
$eventCollection = $dayData['events'];
foreach($eventCollection as $eventArr){
$this->handleEventArr($eventArr, $day);
}
$mealCollection = $dayData['meals'];
// max 2 main meals allowed
Expand Down Expand Up @@ -280,6 +287,21 @@ private function handleMealArray(array $mealArr, Day $dayEntity): void
}
}
}
private function handleEventArr(array $eventArr, Day $day): void{
$this->logger->info('Handle Event Arr');
foreach($eventArr as $event){
$this->addEvent($event, $day);
}
}
private function addEvent(array $event, Day $dayEntity){
$this->logger->info('addEvent');
$eventEntity = $this->mealAdminHelper->findEvent($event['eventId']);
$eventExistsForDayAlready = $this->mealAdminHelper->checkIfEventExistsForDay($event['eventId'], $dayEntity);

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

View workflow job for this annotation

GitHub Actions / PHPMD

Avoid excessively long variable names like $eventExistsForDayAlready. Keep variable name length under 20.
if(!$eventExistsForDayAlready){
$eventParticipationEntity = new EventParticipation($dayEntity, $eventEntity);

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

View workflow job for this annotation

GitHub Actions / PHPMD

Avoid excessively long variable names like $eventParticipationEntity. Keep variable name length under 20.
$dayEntity->addEvent($eventParticipationEntity);
}
}

private function createMeal(Dish $dishEntity, Day $dayEntity, array $meal): void
{
Expand Down
19 changes: 9 additions & 10 deletions src/Mealz/MealBundle/Entity/Day.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ class Day extends AbstractMessage implements JsonSerializable
#[ORM\OneToMany(mappedBy: 'day', targetEntity: Meal::class, cascade: ['all'])]
private Collection $meals;

// #[ORM\OneToOne(mappedBy: 'day', targetEntity: EventParticipation::class, cascade: ['all'])]
// #[ORM\JoinColumn(name: 'event_id', referencedColumnName: 'id', nullable: true)]
// private ?EventParticipation $event = null;

/**
* @var Collection<int, EventParticipation>
*/
Expand Down Expand Up @@ -85,23 +81,24 @@ public function setWeek(Week $week): void
}
public function getEvents(): EventCollection
{
if (false === ($this->events instanceof Collection)) {
if ($this->events instanceof Collection) {
$this->events = new 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){
if($event->getId() === $id && $event->getDay() === $day){
return $event;
}
}
return null;
}

public function setEvent(EventParticipation $event){
public function addEvent(EventParticipation $event){
$this->events->add($event);
}

Expand Down Expand Up @@ -190,10 +187,12 @@ public function jsonSerialize(): array
}
}
foreach ($this->getEvents() as $event) {
if(null !== $event){
$event->jsonSerialize();
if ($event !== null && $event instanceof EventParticipation) {
$eventId = $event->getId();
if (isset($eventId)) {
$events[$eventId] = $event->jsonSerialize();
}
}

}

return [

Check failure on line 198 in src/Mealz/MealBundle/Entity/Day.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis

InvalidReturnStatement

src/Mealz/MealBundle/Entity/Day.php:198:16: InvalidReturnStatement: The inferred type 'array{dateTime: DateTime, enabled: bool, events: array<int, array<array-key, mixed>>, lockParticipationDateTime: DateTime, meals: array<''|int, non-empty-list<array{dateTime: DateTime, day: int|null, dish: null|string, id: int|null, lockTime: DateTime, participationLimit: int}>>, week: int|null}' does not match the declared return type 'array{dateTime: DateTime, enabled: bool, event: int|null, lockParticipationDateTime: DateTime, meals: array<''|int, non-empty-list<array<array-key, mixed>>>, week: int|null}' for App\Mealz\MealBundle\Entity\Day::jsonSerialize due to additional array shape fields (events) (see https://psalm.dev/128)
Expand Down
2 changes: 1 addition & 1 deletion src/Mealz/MealBundle/Entity/EventParticipation.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class EventParticipation
#[ORM\JoinColumn(name: 'day', referencedColumnName: 'id')]
private Day $day;

#[ORM\ManyToOne(targetEntity: Event::class)]
#[ORM\ManyToOne(targetEntity: Event::class, cascade: ["persist"])]
#[ORM\JoinColumn(name: 'event', referencedColumnName: 'id')]
private Event $event;

Expand Down
26 changes: 16 additions & 10 deletions src/Mealz/MealBundle/Helper/MealAdminHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
namespace App\Mealz\MealBundle\Helper;

use App\Mealz\MealBundle\Entity\Day;
use App\Mealz\MealBundle\Entity\EventParticipation;
use App\Mealz\MealBundle\Entity\Event;
use App\Mealz\MealBundle\Entity\Meal;
use App\Mealz\MealBundle\Repository\EventRepository;
use App\Mealz\MealBundle\Repository\EventParticipationRepository;
use App\Mealz\MealBundle\Service\EventParticipationService;

class MealAdminHelper
{
private EventParticipationService $eventService;

public function __construct(
EventParticipationService $eventService
) {
$this->eventService = $eventService;
}
private readonly EventParticipationService $eventService,
private readonly EventRepository $eventRepository,
private readonly EventParticipationRepository $eventParticipationRepository

Check warning on line 19 in src/Mealz/MealBundle/Helper/MealAdminHelper.php

View workflow job for this annotation

GitHub Actions / PHPMD

Avoid excessively long variable names like $eventParticipationRepository. Keep variable name length under 20.
) {}

public function setParticipationLimit(Meal $mealEntity, array $meal): void
{
Expand All @@ -31,9 +31,15 @@ public function setParticipationLimit(Meal $mealEntity, array $meal): void
$mealEntity->setParticipationLimit(0);
}
}

public function handleEventParticipation(Day $day, EventParticipation $event): void
public function findEvent(int $eventId): Event

Check failure on line 34 in src/Mealz/MealBundle/Helper/MealAdminHelper.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis

InvalidNullableReturnType

src/Mealz/MealBundle/Helper/MealAdminHelper.php:34:46: InvalidNullableReturnType: The declared return type 'App\Mealz\MealBundle\Entity\Event' for App\Mealz\MealBundle\Helper\MealAdminHelper::findEvent is not nullable, but 'App\Mealz\MealBundle\Entity\Event|null' contains null (see https://psalm.dev/144)
{
$this->eventService->handleEventParticipation($day, $event);
return $this->eventRepository->find($eventId);

Check failure on line 36 in src/Mealz/MealBundle/Helper/MealAdminHelper.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis

NullableReturnStatement

src/Mealz/MealBundle/Helper/MealAdminHelper.php:36:16: NullableReturnStatement: The declared return type 'App\Mealz\MealBundle\Entity\Event' for App\Mealz\MealBundle\Helper\MealAdminHelper::findEvent is not nullable, but the function returns 'App\Mealz\MealBundle\Entity\Event|null' (see https://psalm.dev/139)
}
public function checkIfEventExistsForDay(int $eventId, Day $day): bool
{
if($this->eventParticipationRepository->findByEventIdAndDay($day, $eventId)){
return true;
}
return false;
}
}
14 changes: 14 additions & 0 deletions src/Mealz/MealBundle/Repository/EventParticipationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ public function findByEventAndDay(Day $day, Event $event): ?EventParticipation

$result = $queryBuilder->getQuery()->getResult();

return count($result) ? $result[0] : null;
}
public function findByEventIdAndDay(Day $day, int $eventId): ?EventParticipation
{
$queryBuilder = $this->createQueryBuilder('m');

// WHERE
$queryBuilder->andWhere('m.day = :day');
$queryBuilder->andWhere('m.event = :event');
$queryBuilder->setParameter('day', $day->getId());
$queryBuilder->setParameter('event', $eventId);

$result = $queryBuilder->getQuery()->getResult();

return count($result) ? $result[0] : null;
}
}
10 changes: 5 additions & 5 deletions src/Mealz/MealBundle/Service/ApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,15 @@ public function getEventParticipationData(Day $day, ?Profile $profile = null): ?
*
* @psalm-return array{name: string, participants: array}|null
*/
public function getEventParticipationInfo(Day $day): ?array
public function getEventParticipationInfo(Day $day, Int $eventId): ?array
{
if (null === $day->getEvents()) {
return null;
}

return [
'name' => $day->getEvents()->getEvent()->getTitle(),
'participants' => $this->getEventParticipants($day),
'name' => $day->getEvent($day, $eventId)->getEvent()->getTitle(),
'participants' => $this->getEventParticipants($day, $eventId),
];
}

Expand Down Expand Up @@ -175,8 +175,8 @@ public function isMealOpen(Meal $meal): bool
|| false === $this->hasCombiReachedLimit($meal->getDay()));
}

private function getEventParticipants(Day $day): array
private function getEventParticipants(Day $day, Int $eventId): array
{
return $this->eventPartSrv->getParticipants($day);
return $this->eventPartSrv->getParticipants($day, $eventId);
}
}
2 changes: 1 addition & 1 deletion src/Mealz/MealBundle/Service/EventParticipationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private function addEventToDay(Day $day, ?Event $event): void
// new eventparticipation
if (null !== $event && null === $day->getEvents()) {
$eventParticipation = new EventParticipation($day, $event);
$day->setEvent($eventParticipation);
$day->addEvent($eventParticipation);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Resources/src/components/menu/MenuDay.vue
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ watch(selectedDishTwo, () => {
watch(selectedEventOne, () => {
try{
const firstKey = Object.keys(props.modelValue.events)[0];
const firstKey = Object.keys(props.modelValue.events)[0] ?? selectedEventOne.value.id;
if (selectedEventOne.value !== null && selectedEventOne.value !== undefined) {
selectedDishes.value.events[firstKey] = [{
eventId: selectedEventOne.value.id,
Expand All @@ -185,7 +185,7 @@ watch(selectedEventOne, () => {
watch(selectedEventTwo, () => {
try{
const secondKey = Object.keys(props.modelValue.events)[1];
const secondKey = Object.keys(props.modelValue.events)[1] ?? selectedEventTwo.value.id;;
if (selectedEventTwo.value !== null && selectedEventTwo.value !== undefined) {
selectedDishes.value.events[secondKey] = [{
eventId: selectedEventTwo.value.id,
Expand Down

0 comments on commit ab1a956

Please sign in to comment.