json(['message' => 'Forbidden'], 403); } try { Event::create([ "name" => $request['name'], "description" => $request['description'], "start" => $request['start'], "end" => $request['end'], ]); return response()->json(['message' => 'Event created']); } catch (\Exception $e) { Log::info($e->getMessage()); return response()->json(['message' => "Server error"], 500); } } public function update(Request $request, int $id): JsonResponse { if (Gate::denies('update', Event::class)) { return response()->json(['message' => 'Forbidden'], 403); } try { $event = Event::find($id); $event->name = $request['name']; $event->description = $request['description']; $event->start = $request['start']; $event->end = $request['end']; $event->save(); return response()->json(["message" => "Event updated"]); } catch (\Exception $e) { Log::info($e->getMessage()); return response()->json(['message' => "Server error"], 500); } } public function getEvents(Request $request): JsonResponse { try { $events = Event::with('tasks')->get(); return response()->json(['data' => $events]); } catch (\Exception $e) { Log::info($e->getMessage()); return response()->json(['message' => "Server error"], 500); } } public function delete(Request $request, int $id): JsonResponse { if (Gate::denies('delete', Event::class)) { return response()->json(['message' => 'Forbidden'], 403); } try { $event = Event::with('tasks.users')->find($id); if (!$event) { return response()->json(['message' => 'Event not found'], 404); } $participants = $event->tasks->flatMap(function ($task) { return $task->users; })->unique('id'); if (Carbon::now()->lessThanOrEqualTo($event->end)) { $notification = Notification::create([ 'content' => "L'événement '{$event->name}' a été supprimé. Vous n'y participez donc plus." ]); $notification->users()->attach($participants->pluck('id')); foreach ($participants as $user) { broadcast(new EventParticipationCancelled( $user->id, $event, $notification->id, )); $data = [ 'name' => $user->name, 'lastname' => $user->lastname, 'eventName' => $event->name, 'start' => $event->start, 'end' => $event->end, ]; Mail::to($user->email)->send(new EventParticipationCancelledMail($data)); } } $event->delete(); return response()->json(["message" => "Event deleted and participants notified"]); } catch (\Exception $e) { Log::error("Event delete error: " . $e->getMessage()); return response()->json(['message' => "Server error"], 500); } } public function getEvent(Request $request, int $id): JsonResponse { try { $event = Event::find($id); return response()->json([ "id" => $event->id, "name" => $event->name, "description" => $event->description, "start" => $event->start, "end" => $event->end, "created_at" => $event->created_at, "updated_at" => $event->updated_at, "tasks" => $event->tasks, ]); } catch (\Exception $e) { Log::info($e->getMessage()); return response()->json(['message' => "Server error"], 500); } } public function isUserAssigned(Request $request, int $id): JsonResponse { try { $task = Task::findOrFail($id); $isAssigned = $task->users() ->where('users.id', $request->user()->id) ->exists(); return response()->json([ 'assigned' => $isAssigned ], 200); } catch (\Exception $e) { Log::error($e->getMessage()); return response()->json([ 'message' => 'Server error' ], 500); } } }