interventionRepository = $interventionRepository; } /** * Display a listing of the resource. */ public function index(Request $request): JsonResponse { try { $filters = $request->only([ 'client_id', 'deceased_id', 'status', 'type', 'start_date', 'end_date', 'sort_by', 'sort_order' ]); $perPage = $request->input('per_page', 15); $interventions = $this->interventionRepository->getAllPaginated($filters, $perPage); return response()->json(new InterventionCollection($interventions)); } catch (\Exception $e) { Log::error('Error fetching interventions list: ' . $e->getMessage()); return response()->json([ 'message' => 'Une erreur est survenue lors de la récupération des interventions.', 'error' => $e->getMessage() ], Response::HTTP_INTERNAL_SERVER_ERROR); } } /** * Store a newly created resource in storage. */ public function store(StoreInterventionRequest $request): JsonResponse { try { $validated = $request->validated(); $intervention = $this->interventionRepository->create($validated); return response()->json(new InterventionResource($intervention), Response::HTTP_CREATED); } catch (\Exception $e) { Log::error('Error creating intervention: ' . $e->getMessage()); return response()->json([ 'message' => 'Une erreur est survenue lors de la création de l\'intervention.', 'error' => $e->getMessage() ], Response::HTTP_INTERNAL_SERVER_ERROR); } } /** * Display the specified resource. */ public function show(int $id): JsonResponse { try { $intervention = $this->interventionRepository->findById($id); return response()->json(new InterventionResource($intervention)); } catch (\Exception $e) { Log::error('Error fetching intervention details: ' . $e->getMessage()); return response()->json([ 'message' => 'Intervention non trouvée ou une erreur est survenue.', 'error' => $e->getMessage() ], Response::HTTP_NOT_FOUND); } } /** * Update the specified resource in storage. */ public function update(UpdateInterventionRequest $request, int $id): JsonResponse { try { $intervention = $this->interventionRepository->findById($id); $validated = $request->validated(); $updatedIntervention = $this->interventionRepository->update($intervention, $validated); return response()->json(new InterventionResource($updatedIntervention)); } catch (\Exception $e) { Log::error('Error updating intervention: ' . $e->getMessage()); return response()->json([ 'message' => 'Une erreur est survenue lors de la mise à jour de l\'intervention.', 'error' => $e->getMessage() ], Response::HTTP_INTERNAL_SERVER_ERROR); } } /** * Remove the specified resource from storage. */ public function destroy(int $id): JsonResponse { try { $intervention = $this->interventionRepository->findById($id); $this->interventionRepository->delete($intervention); return response()->json(null, Response::HTTP_NO_CONTENT); } catch (\Exception $e) { Log::error('Error deleting intervention: ' . $e->getMessage()); return response()->json([ 'message' => 'Une erreur est survenue lors de la suppression de l\'intervention.', 'error' => $e->getMessage() ], Response::HTTP_INTERNAL_SERVER_ERROR); } } /** * Change the status of an intervention. */ public function changeStatus(Request $request, int $id): JsonResponse { try { $validated = $request->validate([ 'status' => 'required|in:demande,planifie,en_cours,termine,annule' ]); $intervention = $this->interventionRepository->findById($id); $updatedIntervention = $this->interventionRepository->changeStatus( $intervention, $validated['status'] ); return response()->json(new InterventionResource($updatedIntervention)); } catch (\Exception $e) { Log::error('Error changing intervention status: ' . $e->getMessage()); return response()->json([ 'message' => 'Une erreur est survenue lors de la modification du statut de l\'intervention.', 'error' => $e->getMessage() ], Response::HTTP_INTERNAL_SERVER_ERROR); } } /** * Get interventions for a specific month. */ public function byMonth(Request $request): JsonResponse { try { $validated = $request->validate([ 'year' => 'required|integer|min:2000|max:2100', 'month' => 'required|integer|min:1|max:12' ]); $interventions = $this->interventionRepository->getByMonth( $validated['year'], $validated['month'] ); return response()->json([ 'data' => $interventions->map(function ($intervention) { return new InterventionResource($intervention); }), 'meta' => [ 'total' => $interventions->count(), 'year' => $validated['year'], 'month' => $validated['month'], ] ]); } catch (\Exception $e) { Log::error('Error fetching interventions by month: ' . $e->getMessage()); return response()->json([ 'message' => 'Une erreur est survenue lors de la récupération des interventions du mois.', 'error' => $e->getMessage() ], Response::HTTP_INTERNAL_SERVER_ERROR); } } }