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')->findOrFail($id); if (Carbon::now()->lessThanOrEqualTo($event->end)) { $participants = $event->tasks ->flatMap(fn($task) => $task->users) ->unique('id'); if ($participants->isNotEmpty()) { Notification::send($participants, new EventDeleted($event)); foreach ($participants as $user) { if ($user->email_notifications) { Mail::to($user->email)->send(new EventParticipationCancelledMail([ 'name' => $user->name, 'lastname' => $user->lastname, 'eventName' => $event->name, 'start' => $event->start, 'end' => $event->end, ])); } } } } $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::with(['tasks' => function ($query) { $query->withCount('users'); }])->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, "event_photo_path" =>$event->event_photo_path, ]); } 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); } } public function show(int $id) { $event = Event::findOrFail($id); return response()->json([ 'event_photo_path' => $event->event_photo_path ]); } public function updatePhoto(Request $request, int $id) { $request->validate([ 'photo' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048', ]); $event = Event::findOrFail($id); $path = $request->file('photo')->store('event-photos', 'public'); if ($event->event_photo_path) { Storage::disk('public')->delete($event->event_photo_path); } $event->update([ 'event_photo_path' => $path ]); return response()->json([ 'event_photo_path' => $path ]); } public function destroy(int $id) { $event = Event::findOrFail($id); if ($event->event_photo_path) { Storage::disk('public')->delete($event->event_photo_path); $event->update([ 'event_photo_path' => null ]); } return response()->json([ 'message' => 'Photo supprimée' ]); } }