184 lines
6.3 KiB
PHP
184 lines
6.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\StoreClientLocationRequest;
|
|
use App\Http\Requests\UpdateClientLocationRequest;
|
|
use App\Http\Resources\Client\ClientLocationResource;
|
|
use App\Http\Resources\Client\ClientLocationCollection;
|
|
use App\Repositories\ClientLocationRepositoryInterface;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Http\Request;
|
|
|
|
class ClientLocationController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly ClientLocationRepositoryInterface $clientLocationRepository
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* Display a listing of client locations.
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
try {
|
|
$filters = $request->only(['client_id', 'is_default', 'search']);
|
|
$perPage = $request->get('per_page', 10);
|
|
|
|
$clientLocations = $this->clientLocationRepository->getPaginated($filters, $perPage);
|
|
return new ClientLocationCollection($clientLocations);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error fetching client locations: ' . $e->getMessage(), [
|
|
'exception' => $e,
|
|
'trace' => $e->getTraceAsString(),
|
|
]);
|
|
|
|
return response()->json([
|
|
'message' => 'Une erreur est survenue lors de la récupération des lieux clients.',
|
|
'error' => config('app.debug') ? $e->getMessage() : null,
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Store a newly created client location.
|
|
*/
|
|
public function store(StoreClientLocationRequest $request): ClientLocationResource|JsonResponse
|
|
{
|
|
try {
|
|
$clientLocation = $this->clientLocationRepository->create($request->validated());
|
|
return new ClientLocationResource($clientLocation);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error creating client location: ' . $e->getMessage(), [
|
|
'exception' => $e,
|
|
'trace' => $e->getTraceAsString(),
|
|
'data' => $request->validated(),
|
|
]);
|
|
|
|
return response()->json([
|
|
'message' => 'Une erreur est survenue lors de la création du lieu client.',
|
|
'error' => config('app.debug') ? $e->getMessage() : null,
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Display the specified client id.
|
|
*/
|
|
public function getLocationsByClient(string $id)
|
|
{
|
|
try {
|
|
$clientLocations = $this->clientLocationRepository->getByClientId((int)$id);
|
|
return ClientLocationResource::collection($clientLocations);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error fetching client location: ' . $e->getMessage(), [
|
|
'exception' => $e,
|
|
'trace' => $e->getTraceAsString(),
|
|
'client_location_id' => $id,
|
|
]);
|
|
|
|
return response()->json([
|
|
'message' => 'Une erreur est survenue lors de la récupération du lieu client.',
|
|
'error' => config('app.debug') ? $e->getMessage() : null,
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Display the specified client location.
|
|
*/
|
|
public function show(string $id): ClientLocationResource|JsonResponse
|
|
{
|
|
try {
|
|
$clientLocation = $this->clientLocationRepository->find($id);
|
|
|
|
if (!$clientLocation) {
|
|
return response()->json([
|
|
'message' => 'Lieu client non trouvé.',
|
|
], 404);
|
|
}
|
|
|
|
return new ClientLocationResource($clientLocation);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error fetching client location: ' . $e->getMessage(), [
|
|
'exception' => $e,
|
|
'trace' => $e->getTraceAsString(),
|
|
'client_location_id' => $id,
|
|
]);
|
|
|
|
return response()->json([
|
|
'message' => 'Une erreur est survenue lors de la récupération du lieu client.',
|
|
'error' => config('app.debug') ? $e->getMessage() : null,
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update the specified client location.
|
|
*/
|
|
public function update(UpdateClientLocationRequest $request, string $id): ClientLocationResource|JsonResponse
|
|
{
|
|
try {
|
|
$updated = $this->clientLocationRepository->update($id, $request->validated());
|
|
|
|
if (!$updated) {
|
|
return response()->json([
|
|
'message' => 'Lieu client non trouvé ou échec de la mise à jour.',
|
|
], 404);
|
|
}
|
|
|
|
$clientLocation = $this->clientLocationRepository->find($id);
|
|
return new ClientLocationResource($clientLocation);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error updating client location: ' . $e->getMessage(), [
|
|
'exception' => $e,
|
|
'trace' => $e->getTraceAsString(),
|
|
'client_location_id' => $id,
|
|
'data' => $request->validated(),
|
|
]);
|
|
|
|
return response()->json([
|
|
'message' => 'Une erreur est survenue lors de la mise à jour du lieu client.',
|
|
'error' => config('app.debug') ? $e->getMessage() : null,
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified client location.
|
|
*/
|
|
public function destroy(string $id): JsonResponse
|
|
{
|
|
try {
|
|
$deleted = $this->clientLocationRepository->delete($id);
|
|
|
|
if (!$deleted) {
|
|
return response()->json([
|
|
'message' => 'Lieu client non trouvé ou échec de la suppression.',
|
|
], 404);
|
|
}
|
|
|
|
return response()->json([
|
|
'message' => 'Lieu client supprimé avec succès.',
|
|
], 200);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error deleting client location: ' . $e->getMessage(), [
|
|
'exception' => $e,
|
|
'trace' => $e->getTraceAsString(),
|
|
'client_location_id' => $id,
|
|
]);
|
|
|
|
return response()->json([
|
|
'message' => 'Une erreur est survenue lors de la suppression du lieu client.',
|
|
'error' => config('app.debug') ? $e->getMessage() : null,
|
|
], 500);
|
|
}
|
|
}
|
|
}
|