Skip to content

Commit

Permalink
fixed backend structure of events works but destroyed join and leave …
Browse files Browse the repository at this point in the history
…methods
  • Loading branch information
IrisOlfermann committed Aug 28, 2024
1 parent c4f60db commit ea88936
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 39 deletions.
15 changes: 9 additions & 6 deletions src/Mealz/MealBundle/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 8 additions & 2 deletions src/Mealz/MealBundle/Entity/EventParticipation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 9 additions & 1 deletion src/Mealz/MealBundle/Service/ApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
13 changes: 10 additions & 3 deletions src/Mealz/MealBundle/Service/EventParticipationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Doctrine\ORM\EntityManagerInterface;
use Exception;

use function Amp\Iterator\toArray;

class EventParticipationService
{
private Doorman $doorman;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/src/api/getDashboardData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export type Slot = {
};

export type EventParticipation = {
eventId: number;
id: number;
event: Event;
day: Date;
participationId: number;
Expand Down
19 changes: 9 additions & 10 deletions src/Resources/src/components/dashboard/EventData.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<span
class="inline-block grow self-start break-words text-[12px] font-bold leading-[20px] tracking-[0.5px] text-primary-1 max-[380px]:basis-9/12 min-[380px]:self-center min-[380px]:text-note"
>
{{ event.event.title }}
{{ getEventById(event?.id ?? -1)?.title }}
</span>
<div class="flex w-fit flex-row items-center gap-1 self-end justify-self-end max-[380px]:basis-3/12">
<GuestButton
Expand All @@ -40,7 +40,7 @@
class="col-start-1 w-[24px] text-center"
/>
<EventPopup
:event-title="event.event.title"
:event-title="getEventById(event?.id ?? -1)?.title"
:date="day.date.date"
/>
<ParticipationCounter
Expand All @@ -52,7 +52,7 @@
<CheckBox
:isActive="new Date(day.date.date) > new Date()"
:isChecked="event?.isParticipating ?? false"
@click="handleClick"
@click="handleClick(event)"
/>
</div>
</div>
Expand All @@ -62,7 +62,7 @@
</template>

<script setup lang="ts">
import { type Day } from '@/api/getDashboardData';
import { type Day, type EventParticipation } from '@/api/getDashboardData';
import { useEvents } from '@/stores/eventsStore';
import ParticipationCounter from '../menuCard/ParticipationCounter.vue';
import CheckBox from '../misc/CheckBox.vue';
Expand All @@ -80,20 +80,19 @@ const props = defineProps<{
}>();
const twoEvents: boolean = props.day?.events !== undefined && Object.keys(props.day?.events).length === 2;
console.log('Events am Tag '+JSON.stringify(props.day.events));
const { t } = useI18n();
const { joinEvent, leaveEvent } = useEvents();
const { getEventById, joinEvent, leaveEvent } = useEvents();
const { addLock, isLocked, removeLock } = useLockRequests();
async function handleClick() {
async function handleClick(event: EventParticipation) {
if (isLocked(props.dayId) === true || isEventPast() === true) {
return;
}
addLock(props.dayId);
if (props.day.events.EventParticipation?.isParticipating === false) {
await joinEvent(props.day.date.date, props.day.events.EventParticipation.eventId);
if (event?.isParticipating === undefined || event?.isParticipating === false) {
await joinEvent(props.day.date.date, event.id);
} else {
await leaveEvent(props.day.date.date, props.day.events.EventParticipation.eventId);
await leaveEvent(props.day.date.date, event.id);
}
removeLock(props.dayId);
}
Expand Down
29 changes: 14 additions & 15 deletions src/Resources/src/stores/dashboardStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ class DashboardStore extends Store<Dashboard> {
const week = this.getWeek(weekID);
if (week !== undefined) {
const day = week.days[dayID as number];
console.log(JSON.stringify(day.events));

return day;
}
return undefined;
Expand All @@ -45,10 +43,11 @@ class DashboardStore extends Store<Dashboard> {
public getDayByEventParticipationId(eventParticipationId: number): Day | undefined {
for (const week of Object.values(this.state.weeks)) {
for (const day of Object.values(week.days)) {
for(const event of Object.values(day.events))
if (
day.events.EventParticipation.eventId !== null &&
day.events.EventParticipation.eventId !== undefined &&
day.events.EventParticipation.eventId === eventParticipationId
event.id !== null &&
event.id !== undefined &&
event.id === eventParticipationId
) {
return day;
}
Expand Down Expand Up @@ -110,20 +109,20 @@ class DashboardStore extends Store<Dashboard> {

public updateEventParticipation(weekId: number, dayId: number, eventId: number, participations: number) {
const day = this.getDay(weekId, dayId);
if (
day !== null &&
day !== undefined &&
day.events.Dictionary !== null &&
day.events.Dictionary.eventId === eventId
) {
day.events.EventParticipation.participations = participations;
}
if (
day !== null &&
day !== undefined &&
day.events !== null &&
day.events[eventId] !== undefined
) {
day.events[eventId].participations = participations;
}
}

public setIsParticipatingEvent(participationId: number, isParticipating: boolean) {
const day = this.getDayByEventParticipationId(participationId);
if (day !== undefined) {
day.events.EventParticipation.isParticipating = isParticipating;
if (day !== undefined && day.events[participationId] !== undefined) {
day.events[participationId].isParticipating = isParticipating;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Resources/src/stores/eventsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ export function useEvents() {
const { error, events } = await getEvents();
if (isResponseArrayOkay<Event>(error, events, isEvent) === true) {
EventsState.events = events.value as Event[];
console.log('fetch Events ' + JSON.stringify(EventsState.events));
EventsState.error = '';
} else {
EventsState.error = 'Error on fetching events';
Expand Down Expand Up @@ -190,6 +189,7 @@ export function useEvents() {
* @param date The date the event is on (format: 'YYYY-mm-dd hh:MM:ss')
*/
async function joinEvent(date: string, eventId: number) {
console.log('Join event im EventStore ' + eventId);
const { error, response } = await postJoinEvent(date, eventId);

if (error.value === true && isMessage(response.value) === true) {
Expand Down

0 comments on commit ea88936

Please sign in to comment.