with(['location', 'customer']) ->latest('event_date') ->paginate(); return new EventCollection($events); } /** * Store a newly created event. */ public function store(StoreEventRequest $request): JsonResponse { Gate::authorize('create', Event::class); $data = $request->validated(); $data['created_by'] = auth()->id(); $data['currency'] = $data['currency'] ?? 'EUR'; $event = Event::create($data); return $this->created( new EventResource($event->load(['location', 'customer'])), 'Event created successfully' ); } /** * Display the specified event. */ public function show(Event $event): EventResource { Gate::authorize('view', $event); return new EventResource( $event->load(['location', 'customer', 'setlist.items.musicNumber', 'invitations.user', 'creator']) ); } /** * Update the specified event. */ public function update(UpdateEventRequest $request, Event $event): JsonResponse { Gate::authorize('update', $event); $event->update($request->validated()); return $this->success( new EventResource($event->load(['location', 'customer'])), 'Event updated successfully' ); } /** * Remove the specified event. */ public function destroy(Event $event): JsonResponse { Gate::authorize('delete', $event); $event->delete(); return $this->success(null, 'Event deleted successfully'); } /** * Invite members to an event. */ public function invite(InviteToEventRequest $request, Event $event): JsonResponse { Gate::authorize('invite', $event); $userIds = $request->validated()['user_ids']; $invitedCount = 0; foreach ($userIds as $userId) { // Skip if already invited if ($event->invitations()->where('user_id', $userId)->exists()) { continue; } $event->invitations()->create([ 'user_id' => $userId, 'rsvp_status' => RsvpStatus::Pending, 'invited_at' => now(), ]); $invitedCount++; } return $this->success( EventInvitationResource::collection( $event->invitations()->with('user')->get() ), "{$invitedCount} member(s) invited successfully" ); } /** * Respond to an event invitation (RSVP). */ public function rsvp(RsvpEventRequest $request, Event $event): JsonResponse { Gate::authorize('rsvp', $event); $invitation = EventInvitation::where('event_id', $event->id) ->where('user_id', auth()->id()) ->firstOrFail(); $data = $request->validated(); $invitation->update([ 'rsvp_status' => $data['status'], 'rsvp_note' => $data['note'] ?? null, 'rsvp_responded_at' => now(), ]); return $this->success( new EventInvitationResource($invitation->load('user')), 'RSVP updated successfully' ); } }