get('per_page', 15); $filters = [ 'search' => $request->get('search'), 'valid_authorization' => $request->get('valid_authorization'), 'sort_by' => $request->get('sort_by', 'created_at'), 'sort_direction' => $request->get('sort_direction', 'desc'), ]; // Remove null filters $filters = array_filter($filters, function ($value) { return $value !== null && $value !== ''; }); $result = $this->thanatopractitionerRepository->getPaginated($perPage, $filters); return response()->json([ 'data' => new ThanatopractitionerCollection($result['thanatopractitioners']), 'pagination' => $result['pagination'], 'message' => 'Thanatopractitioners récupérés avec succès.', ], 200); } catch (\Exception $e) { Log::error('Error fetching thanatopractitioners: ' . $e->getMessage(), [ 'exception' => $e, 'trace' => $e->getTraceAsString(), ]); return response()->json([ 'message' => 'Une erreur est survenue lors de la récupération des thanatopractitioners.', 'error' => config('app.debug') ? $e->getMessage() : null, ], 500); } } /** * Display paginated thanatopractitioners. */ public function paginated(Request $request): JsonResponse { try { $perPage = (int) $request->get('per_page', 15); $result = $this->thanatopractitionerRepository->getPaginated($perPage, []); return response()->json([ 'data' => new ThanatopractitionerCollection($result['thanatopractitioners']), 'pagination' => $result['pagination'], 'message' => 'Thanatopractitioners récupérés avec succès.', ], 200); } catch (\Exception $e) { Log::error('Error fetching paginated thanatopractitioners: ' . $e->getMessage(), [ 'exception' => $e, 'trace' => $e->getTraceAsString(), ]); return response()->json([ 'message' => 'Une erreur est survenue lors de la récupération des thanatopractitioners.', 'error' => config('app.debug') ? $e->getMessage() : null, ], 500); } } /** * Get thanatopractitioners with valid authorization. */ public function withValidAuthorization(): ThanatopractitionerCollection|JsonResponse { try { $thanatopractitioners = $this->thanatopractitionerRepository->getWithValidAuthorization(); return new ThanatopractitionerCollection($thanatopractitioners); } catch (\Exception $e) { Log::error('Error fetching thanatopractitioners with valid authorization: ' . $e->getMessage(), [ 'exception' => $e, 'trace' => $e->getTraceAsString(), ]); return response()->json([ 'message' => 'Une erreur est survenue lors de la récupération des thanatopractitioners avec autorisation valide.', 'error' => config('app.debug') ? $e->getMessage() : null, ], 500); } } /** * Get thanatopractitioners with expired authorization. */ public function withExpiredAuthorization(): ThanatopractitionerCollection|JsonResponse { try { $thanatopractitioners = $this->thanatopractitionerRepository->getWithExpiredAuthorization(); return new ThanatopractitionerCollection($thanatopractitioners); } catch (\Exception $e) { Log::error('Error fetching thanatopractitioners with expired authorization: ' . $e->getMessage(), [ 'exception' => $e, 'trace' => $e->getTraceAsString(), ]); return response()->json([ 'message' => 'Une erreur est survenue lors de la récupération des thanatopractitioners avec autorisation expirée.', 'error' => config('app.debug') ? $e->getMessage() : null, ], 500); } } /** * Get thanatopractitioners with their complete data. */ public function withRelations(): ThanatopractitionerCollection|JsonResponse { try { $thanatopractitioners = $this->thanatopractitionerRepository->getWithRelations(); return new ThanatopractitionerCollection($thanatopractitioners); } catch (\Exception $e) { Log::error('Error fetching thanatopractitioners with relations: ' . $e->getMessage(), [ 'exception' => $e, 'trace' => $e->getTraceAsString(), ]); return response()->json([ 'message' => 'Une erreur est survenue lors de la récupération des thanatopractitioners avec relations.', 'error' => config('app.debug') ? $e->getMessage() : null, ], 500); } } /** * Get thanatopractitioner statistics. */ public function statistics(): JsonResponse { try { $statistics = $this->thanatopractitionerRepository->getStatistics(); return response()->json([ 'data' => $statistics, 'message' => 'Statistiques des thanatopractitioners récupérées avec succès.', ], 200); } catch (\Exception $e) { Log::error('Error fetching thanatopractitioner statistics: ' . $e->getMessage(), [ 'exception' => $e, 'trace' => $e->getTraceAsString(), ]); return response()->json([ 'message' => 'Une erreur est survenue lors de la récupération des statistiques.', 'error' => config('app.debug') ? $e->getMessage() : null, ], 500); } } /** * Store a newly created thanatopractitioner. */ public function store(StoreThanatopractitionerRequest $request): ThanatopractitionerResource|JsonResponse { try { $thanatopractitioner = $this->thanatopractitionerRepository->create($request->validated()); return new ThanatopractitionerResource($thanatopractitioner); } catch (\Exception $e) { Log::error('Error creating thanatopractitioner: ' . $e->getMessage(), [ 'exception' => $e, 'trace' => $e->getTraceAsString(), 'data' => $request->validated(), ]); return response()->json([ 'message' => 'Une erreur est survenue lors de la création du thanatopractitioner.', 'error' => config('app.debug') ? $e->getMessage() : null, ], 500); } } /** * Display the specified thanatopractitioner. */ public function show(string $id): ThanatopractitionerResource|JsonResponse { try { $thanatopractitioner = $this->thanatopractitionerRepository->find($id); if (!$thanatopractitioner) { return response()->json([ 'message' => 'Thanatopractitioner non trouvé.', ], 404); } return new ThanatopractitionerResource($thanatopractitioner); } catch (\Exception $e) { Log::error('Error fetching thanatopractitioner: ' . $e->getMessage(), [ 'exception' => $e, 'trace' => $e->getTraceAsString(), 'thanatopractitioner_id' => $id, ]); return response()->json([ 'message' => 'Une erreur est survenue lors de la récupération du thanatopractitioner.', 'error' => config('app.debug') ? $e->getMessage() : null, ], 500); } } /** * Find a thanatopractitioner by employee ID. */ public function findByEmployee(string $employeeId): ThanatopractitionerResource|JsonResponse { try { $thanatopractitioner = $this->thanatopractitionerRepository->findByEmployeeId((int) $employeeId); if (!$thanatopractitioner) { return response()->json([ 'message' => 'Thanatopractitioner non trouvé pour cet employé.', ], 404); } return new ThanatopractitionerResource($thanatopractitioner); } catch (\Exception $e) { Log::error('Error fetching thanatopractitioner by employee: ' . $e->getMessage(), [ 'exception' => $e, 'trace' => $e->getTraceAsString(), 'employee_id' => $employeeId, ]); return response()->json([ 'message' => 'Une erreur est survenue lors de la récupération du thanatopractitioner.', 'error' => config('app.debug') ? $e->getMessage() : null, ], 500); } } /** * Update the specified thanatopractitioner. */ public function update(UpdateThanatopractitionerRequest $request, string $id): ThanatopractitionerResource|JsonResponse { try { $updated = $this->thanatopractitionerRepository->update($id, $request->validated()); if (!$updated) { return response()->json([ 'message' => 'Thanatopractitioner non trouvé ou échec de la mise à jour.', ], 404); } $thanatopractitioner = $this->thanatopractitionerRepository->find($id); return new ThanatopractitionerResource($thanatopractitioner); } catch (\Exception $e) { Log::error('Error updating thanatopractitioner: ' . $e->getMessage(), [ 'exception' => $e, 'trace' => $e->getTraceAsString(), 'thanatopractitioner_id' => $id, 'data' => $request->validated(), ]); return response()->json([ 'message' => 'Une erreur est survenue lors de la mise à jour du thanatopractitioner.', 'error' => config('app.debug') ? $e->getMessage() : null, ], 500); } } /** * Remove the specified thanatopractitioner. */ public function destroy(string $id): JsonResponse { try { $deleted = $this->thanatopractitionerRepository->delete($id); if (!$deleted) { return response()->json([ 'message' => 'Thanatopractitioner non trouvé ou échec de la suppression.', ], 404); } return response()->json([ 'message' => 'Thanatopractitioner supprimé avec succès.', ], 200); } catch (\Exception $e) { Log::error('Error deleting thanatopractitioner: ' . $e->getMessage(), [ 'exception' => $e, 'trace' => $e->getTraceAsString(), 'thanatopractitioner_id' => $id, ]); return response()->json([ 'message' => 'Une erreur est survenue lors de la suppression du thanatopractitioner.', 'error' => config('app.debug') ? $e->getMessage() : null, ], 500); } } }