Skip to content

Commit

Permalink
added endpoints to join and leave events
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Ruf committed Jan 10, 2024
1 parent 78649cc commit a7d31b8
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,7 @@ IDP_CLIENT_SECRET=client-secret
* 601: The amount of cash that will be added, has to be more than zero
*EventController 7xx*
* 701: Event creation parameters are missing
*EventParticipationController 8xx*
* 801: User has no permission to enter or leave
* 802: User could not join the event
* 803: User could not leave the event
61 changes: 61 additions & 0 deletions src/Mealz/MealBundle/Controller/EventParticipationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace App\Mealz\MealBundle\Controller;

use App\Mealz\MealBundle\Entity\Day;
use App\Mealz\MealBundle\Event\EventParticipationUpdateEvent;
use App\Mealz\MealBundle\Service\EventParticipationService;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\JsonResponse;

/**
* @Security("is_granted('ROLE_USER')")
*/
class EventParticipationController extends BaseController
{

private EventDispatcherInterface $eventDispatcher;
private EventParticipationService $eventParticipationService;

Check warning on line 20 in src/Mealz/MealBundle/Controller/EventParticipationController.php

View workflow job for this annotation

GitHub Actions / PHPMD

Avoid excessively long variable names like $eventParticipationService. Keep variable name length under 20.

Check warning on line 20 in src/Mealz/MealBundle/Controller/EventParticipationController.php

View workflow job for this annotation

GitHub Actions / PHPMD

Avoid excessively long variable names like $eventParticipationService. Keep variable name length under 20.

public function __construct(
EventDispatcherInterface $eventDispatcher,
EventParticipationService $eventParticipationService

Check warning on line 24 in src/Mealz/MealBundle/Controller/EventParticipationController.php

View workflow job for this annotation

GitHub Actions / PHPMD

Avoid excessively long variable names like $eventParticipationService. Keep variable name length under 20.

Check warning on line 24 in src/Mealz/MealBundle/Controller/EventParticipationController.php

View workflow job for this annotation

GitHub Actions / PHPMD

Avoid excessively long variable names like $eventParticipationService. Keep variable name length under 20.
) {
$this->eventDispatcher = $eventDispatcher;
$this->eventParticipationService = $eventParticipationService;
}

public function join(Day $day): JsonResponse
{
$profile = $this->getProfile();
if (null === $profile) {
return new JsonResponse(['messasge' => '801: User is not allowed to join'], 403);
}

$eventParticipation = $this->eventParticipationService->join($profile, $day);
if (null === $eventParticipation) {
return new JsonResponse(['messasge' => '802: User could not join the event'], 500);
}

$this->eventDispatcher->dispatch(new EventParticipationUpdateEvent($eventParticipation));
return new JsonResponse(['isParticipating' => true], 200);
}

public function leave(Day $day): JsonResponse
{
$profile = $this->getProfile();
if (null === $profile) {
return new JsonResponse(['messasge' => '801: User is not allowed to leave'], 403);
}

$eventParticipation = $this->eventParticipationService->leave($profile, $day);
if (null === $eventParticipation) {
return new JsonResponse(['messasge' => '802: User could not leave the event'], 500);
}

$this->eventDispatcher->dispatch(new EventParticipationUpdateEvent($eventParticipation));
return new JsonResponse(['isParticipating' => false], 200);
}
}
10 changes: 10 additions & 0 deletions src/Mealz/MealBundle/Entity/EventParticipation.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,14 @@ public function getParticipants(): ArrayCollection

return new ArrayCollection($this->participants->toArray());
}

public function addParticipant(Participant $participant): void
{
$this->participants->add($participant);
}

public function removeParticipant(Participant $participant): bool
{
return $this->participants->removeElement($participant);
}
}
7 changes: 4 additions & 3 deletions src/Mealz/MealBundle/Entity/Participant.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class Participant
* @Assert\NotNull()
* @Assert\Type(type="App\Mealz\MealBundle\Entity\Meal")
* @ORM\ManyToOne(targetEntity="Meal",inversedBy="participants")
* @ORM\JoinColumn(name="meal_id", referencedColumnName="id")
* @ORM\JoinColumn(name="meal_id", referencedColumnName="id", nullable=true)
*/
private Meal $meal;
private ?Meal $meal;

/**
* @Assert\NotNull()
Expand Down Expand Up @@ -85,10 +85,11 @@ class Participant
*/
private bool $confirmed = false;

public function __construct(Profile $profile, ?Meal $meal)
public function __construct(Profile $profile, ?Meal $meal, ?EventParticipation $eventParticipation)
{
$this->profile = $profile;
$this->meal = $meal;
$this->event = $eventParticipation;
$this->combinedDishes = new DishCollection();
}

Expand Down
10 changes: 10 additions & 0 deletions src/Mealz/MealBundle/Resources/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,16 @@ MealzMealBundle_api_events_delete:
defaults: { _controller: App\Mealz\MealBundle\Controller\EventController::delete }
methods: [ DELETE ]

MealzMealBundle_api_events_join:
path: /api/events/participation
defaults: { _controller: App\Mealz\MealBundle\Controller\EventParticipationController::join }
methods: [ POST ]

MealzMealBundle_api_events_join:
path: /api/events/participation
defaults: { _controller: App\Mealz\MealBundle\Controller\EventParticipationController::leave }
methods: [ DELETE ]

MealzMealBundle_Meal_offers:
path: /menu/{date}/{dish}/offers
defaults: { _controller: App\Mealz\MealBundle\Controller\MealController::getOffers }
Expand Down
10 changes: 10 additions & 0 deletions src/Mealz/MealBundle/Service/Doorman.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Mealz\MealBundle\Service;

use App\Mealz\MealBundle\Entity\EventParticipation;
use App\Mealz\MealBundle\Entity\Meal;
use App\Mealz\MealBundle\Entity\Participant;
use App\Mealz\UserBundle\Entity\Profile;
Expand Down Expand Up @@ -66,6 +67,15 @@ public function isUserAllowedToJoin(Meal $meal, array $dishSlugs = []): bool
&& $this->hasAccessTo(self::AT_MEAL_PARTICIPATION, ['meal' => $meal]);
}

public function isUserAllowedToJoinEvent(EventParticipation $eventParticipation): bool
{
if (false === $this->security->getUser()->getProfile() instanceof Profile) {
return false;
}

return $this->isToggleParticipationAllowed($eventParticipation->getDay()->getLockParticipationDateTime());
}

public function isOfferAvailable(Meal $meal): bool
{
if (false === $this->security->getUser()->getProfile() instanceof Profile) {
Expand Down
42 changes: 42 additions & 0 deletions src/Mealz/MealBundle/Service/EventParticipationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,26 @@
use App\Mealz\MealBundle\Entity\Day;
use App\Mealz\MealBundle\Entity\Event;
use App\Mealz\MealBundle\Entity\EventParticipation;
use App\Mealz\MealBundle\Entity\Participant;
use App\Mealz\MealBundle\Repository\EventParticipationRepositoryInterface;
use App\Mealz\MealBundle\Repository\EventRepositoryInterface;
use App\Mealz\UserBundle\Entity\Profile;
use Doctrine\ORM\EntityManagerInterface;

class EventParticipationService
{
private Doorman $doorman;
private EntityManagerInterface $em;
private EventParticipationRepositoryInterface $eventPartRepo;
private EventRepositoryInterface $eventRepo;

public function __construct(
Doorman $doorman,
EntityManagerInterface $em,
EventRepositoryInterface $eventRepo,
EventParticipationRepositoryInterface $eventPartRepo
) {
$this->doorman = $doorman;
$this->em = $em;
$this->eventPartRepo = $eventPartRepo;
$this->eventRepo = $eventRepo;
Expand Down Expand Up @@ -55,6 +59,39 @@ public function getEventParticipationData(Day $day, Profile $profile): ?array
];
}

public function join(Profile $profile, Day $day): ?EventParticipation
{
$eventParticipation = $day->getEvent();
if (null !== $eventParticipation && true === $this->doorman->isUserAllowedToJoinEvent($eventParticipation)) {
$participation = $this->createEventParticipation($profile, $eventParticipation);
if (null !== $participation) {
$this->em->persist($participation);
$this->em->flush();

$eventParticipation->addParticipant($participation);
return $eventParticipation;
}
}

return null;
}

public function leave(Profile $profile, Day $day): ?EventParticipation
{
$eventParticipation = $day->getEvent();
$participation = $eventParticipation->getParticipant($profile);

if (null !== $eventParticipation) {
$eventParticipation->removeParticipant($participation);
$this->em->remove($participation);
$this->em->flush();

return $eventParticipation;
}

return null;
}

private function addEventToDay(Day $day, ?Event $event)
{
// new eventparticipation
Expand All @@ -74,4 +111,9 @@ private function removeEventFromDay(Day $day)
$day->setEvent(null);
}
}

private function createEventParticipation(Profile $profile, EventParticipation $eventParticiation): ?Participant
{
return new Participant($profile, null, $eventParticiation);
}
}

0 comments on commit a7d31b8

Please sign in to comment.