New-Thanasoft/thanasoft-back/app/Http/Controllers/Api/ThanatopractitionerController.php
2025-11-21 17:26:43 +03:00

358 lines
13 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Http\Requests\StoreThanatopractitionerRequest;
use App\Http\Requests\UpdateThanatopractitionerRequest;
use App\Http\Resources\Employee\ThanatopractitionerResource;
use App\Http\Resources\Employee\ThanatopractitionerCollection;
use App\Repositories\ThanatopractitionerRepositoryInterface;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
class ThanatopractitionerController extends Controller
{
public function __construct(
private readonly ThanatopractitionerRepositoryInterface $thanatopractitionerRepository
) {
}
/**
* Display a listing of thanatopractitioners (paginated).
*/
public function index(Request $request): JsonResponse
{
try {
$perPage = (int) $request->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->findById((int) $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);
}
}
/**
* Search thanatopractitioners by employee name.
*/
public function searchByEmployeeName(Request $request): JsonResponse
{
try {
$query = $request->get('query', '');
if (strlen($query) < 2) {
return response()->json([
'data' => [],
'message' => 'Veuillez entrer au moins 2 caractères pour la recherche.',
], 200);
}
$thanatopractitioners = $this->thanatopractitionerRepository->searchByEmployeeName($query);
return response()->json([
'data' => new ThanatopractitionerCollection($thanatopractitioners),
'message' => 'Recherche effectuée avec succès.',
], 200);
} catch (\Exception $e) {
Log::error('Error searching thanatopractitioners by employee name: ' . $e->getMessage(), [
'exception' => $e,
'trace' => $e->getTraceAsString(),
'query' => $request->get('query'),
]);
return response()->json([
'message' => 'Une erreur est survenue lors de la recherche.',
'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);
}
}
}