155 lines
4.0 KiB
PHP
155 lines
4.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Api\V1;
|
|
|
|
use App\Enums\RsvpStatus;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Api\V1\InviteToEventRequest;
|
|
use App\Http\Requests\Api\V1\RsvpEventRequest;
|
|
use App\Http\Requests\Api\V1\StoreEventRequest;
|
|
use App\Http\Requests\Api\V1\UpdateEventRequest;
|
|
use App\Http\Resources\Api\V1\EventCollection;
|
|
use App\Http\Resources\Api\V1\EventInvitationResource;
|
|
use App\Http\Resources\Api\V1\EventResource;
|
|
use App\Models\Event;
|
|
use App\Models\EventInvitation;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Support\Facades\Gate;
|
|
|
|
final class EventController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of events.
|
|
*/
|
|
public function index(): EventCollection
|
|
{
|
|
Gate::authorize('viewAny', Event::class);
|
|
|
|
$events = Event::query()
|
|
->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'
|
|
);
|
|
}
|
|
}
|
|
|