Skip to content

Commit

Permalink
Merge pull request #402 from AOEpeople/feature/#261401-endpoint-event…
Browse files Browse the repository at this point in the history
…-booking

added endpoints to join and leave events
  • Loading branch information
MalibusParty authored Jan 10, 2024
2 parents 78649cc + b9ce313 commit 6ed4ed2
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 11 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
3 changes: 1 addition & 2 deletions psalm.baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,7 @@
</UndefinedClass>
</file>
<file src="src/Mealz/MealBundle/Service/Doorman.php">
<UndefinedInterfaceMethod occurrences="2">
<code>getProfile</code>
<UndefinedInterfaceMethod occurrences="3">
<code>getProfile</code>
</UndefinedInterfaceMethod>
</file>
Expand Down
46 changes: 45 additions & 1 deletion src/Mealz/MealBundle/Controller/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

namespace App\Mealz\MealBundle\Controller;

use App\Mealz\MealBundle\Entity\Day;
use App\Mealz\MealBundle\Entity\Event;
use App\Mealz\MealBundle\Event\EventParticipationUpdateEvent;
use App\Mealz\MealBundle\Repository\EventRepositoryInterface;
use App\Mealz\MealBundle\Service\EventParticipationService;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

Expand All @@ -19,13 +23,19 @@ class EventController extends BaseListController
{
private EntityManagerInterface $em;
private EventRepositoryInterface $eventRepo;
private EventDispatcherInterface $eventDispatcher;
private EventParticipationService $eventPartSrv;

public function __construct(
EntityManagerInterface $entityManager,
EventRepositoryInterface $eventRepository
EventRepositoryInterface $eventRepository,
EventDispatcherInterface $eventDispatcher,
EventParticipationService $eventPartSrv
) {
$this->em = $entityManager;
$this->eventRepo = $eventRepository;
$this->eventDispatcher = $eventDispatcher;
$this->eventPartSrv = $eventPartSrv;
}

public function getEventList(): JsonResponse
Expand Down Expand Up @@ -88,4 +98,38 @@ public function delete(Event $event): JsonResponse
return new JsonResponse(['message' => $e->getMessage(), 500]);
}
}

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->eventPartSrv->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->eventPartSrv->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);
}
}
9 changes: 5 additions & 4 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 = null)
{
$this->profile = $profile;
$this->meal = $meal;
$this->event = $eventParticipation;
$this->combinedDishes = new DishCollection();
}

Expand Down Expand Up @@ -137,7 +138,7 @@ public function setMeal(Meal $meal): void
$this->meal = $meal;
}

public function getMeal(): Meal
public function getMeal(): ?Meal
{
return $this->meal;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Mealz/MealBundle/Event/MealOfferAcceptedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct(Participant $participant, Profile $offerer)
$this->offerer = $offerer;
}

public function getMeal(): Meal
public function getMeal(): ?Meal
{
return $this->participant->getMeal();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Mealz/MealBundle/Event/MealOfferCancelledEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(Participant $participant)
$this->participant = $participant;
}

public function getMeal(): Meal
public function getMeal(): ?Meal
{
return $this->participant->getMeal();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Mealz/MealBundle/Event/MealOfferedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function __construct(Participant $participant)
$this->participant = $participant;
}

public function getMeal(): Meal
public function getMeal(): ?Meal
{
return $this->participant->getMeal();
}
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\EventController::join }
methods: [ POST ]

MealzMealBundle_api_events_leave:
path: /api/events/participation
defaults: { _controller: App\Mealz\MealBundle\Controller\EventController::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
39 changes: 39 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,36 @@ 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);

$eventParticipation->removeParticipant($participation);
$this->em->remove($participation);
$this->em->flush();

return $eventParticipation;
}

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

private function createEventParticipation(Profile $profile, EventParticipation $eventParticiation): ?Participant
{
return new Participant($profile, null, $eventParticiation);
}
}
2 changes: 1 addition & 1 deletion src/Mealz/MealBundle/Service/ParticipationServiceTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private function slotIsAvailable(Slot $slot, DateTime $date): bool
*/
private function createParticipation(Profile $profile, Meal $meal, ?Slot $slot = null, array $dishSlugs = []): Participant
{
$participant = new Participant($profile, $meal);
$participant = new Participant($profile, $meal, null);
if (null !== $slot) {
$participant->setSlot($slot);
}
Expand Down

0 comments on commit 6ed4ed2

Please sign in to comment.