157 lines
4.9 KiB
PHP
157 lines
4.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\StoreClientRequest;
|
|
use App\Http\Requests\UpdateClientRequest;
|
|
use App\Http\Resources\Client\ClientResource;
|
|
use App\Repositories\ClientRepositoryInterface;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class ClientController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly ClientRepositoryInterface $clientRepository
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* Display a listing of clients.
|
|
*/
|
|
public function index(): AnonymousResourceCollection|JsonResponse
|
|
{
|
|
try {
|
|
$clients = $this->clientRepository->all();
|
|
return ClientResource::collection($clients);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error fetching clients: ' . $e->getMessage(), [
|
|
'exception' => $e,
|
|
'trace' => $e->getTraceAsString(),
|
|
]);
|
|
|
|
return response()->json([
|
|
'message' => 'Une erreur est survenue lors de la récupération des clients.',
|
|
'error' => config('app.debug') ? $e->getMessage() : null,
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Store a newly created client.
|
|
*/
|
|
public function store(StoreClientRequest $request): ClientResource|JsonResponse
|
|
{
|
|
try {
|
|
$client = $this->clientRepository->create($request->validated());
|
|
return new ClientResource($client);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error creating client: ' . $e->getMessage(), [
|
|
'exception' => $e,
|
|
'trace' => $e->getTraceAsString(),
|
|
'data' => $request->validated(),
|
|
]);
|
|
|
|
return response()->json([
|
|
'message' => 'Une erreur est survenue lors de la création du client.',
|
|
'error' => config('app.debug') ? $e->getMessage() : null,
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Display the specified client.
|
|
*/
|
|
public function show(string $id): ClientResource|JsonResponse
|
|
{
|
|
try {
|
|
$client = $this->clientRepository->find($id);
|
|
|
|
if (!$client) {
|
|
return response()->json([
|
|
'message' => 'Client non trouvé.',
|
|
], 404);
|
|
}
|
|
|
|
return new ClientResource($client);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error fetching client: ' . $e->getMessage(), [
|
|
'exception' => $e,
|
|
'trace' => $e->getTraceAsString(),
|
|
'client_id' => $id,
|
|
]);
|
|
|
|
return response()->json([
|
|
'message' => 'Une erreur est survenue lors de la récupération du client.',
|
|
'error' => config('app.debug') ? $e->getMessage() : null,
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update the specified client.
|
|
*/
|
|
public function update(UpdateClientRequest $request, string $id): ClientResource|JsonResponse
|
|
{
|
|
try {
|
|
$updated = $this->clientRepository->update($id, $request->validated());
|
|
|
|
if (!$updated) {
|
|
return response()->json([
|
|
'message' => 'Client non trouvé ou échec de la mise à jour.',
|
|
], 404);
|
|
}
|
|
|
|
$client = $this->clientRepository->find($id);
|
|
return new ClientResource($client);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error updating client: ' . $e->getMessage(), [
|
|
'exception' => $e,
|
|
'trace' => $e->getTraceAsString(),
|
|
'client_id' => $id,
|
|
'data' => $request->validated(),
|
|
]);
|
|
|
|
return response()->json([
|
|
'message' => 'Une erreur est survenue lors de la mise à jour du client.',
|
|
'error' => config('app.debug') ? $e->getMessage() : null,
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified client.
|
|
*/
|
|
public function destroy(string $id): JsonResponse
|
|
{
|
|
try {
|
|
$deleted = $this->clientRepository->delete($id);
|
|
|
|
if (!$deleted) {
|
|
return response()->json([
|
|
'message' => 'Client non trouvé ou échec de la suppression.',
|
|
], 404);
|
|
}
|
|
|
|
return response()->json([
|
|
'message' => 'Client supprimé avec succès.',
|
|
], 200);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error deleting client: ' . $e->getMessage(), [
|
|
'exception' => $e,
|
|
'trace' => $e->getTraceAsString(),
|
|
'client_id' => $id,
|
|
]);
|
|
|
|
return response()->json([
|
|
'message' => 'Une erreur est survenue lors de la suppression du client.',
|
|
'error' => config('app.debug') ? $e->getMessage() : null,
|
|
], 500);
|
|
}
|
|
}
|
|
}
|