New-Thanasoft/thanasoft-back/app/Http/Controllers/Api/InterventionController.php
2025-11-11 17:45:58 +03:00

177 lines
5.8 KiB
PHP

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Http\Requests\StoreInterventionRequest;
use App\Http\Requests\UpdateInterventionRequest;
use App\Http\Resources\Intervention\InterventionResource;
use App\Http\Resources\Intervention\InterventionCollection;
use App\Repositories\InterventionRepositoryInterface;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Symfony\Component\HttpFoundation\Response;
class InterventionController extends Controller
{
/**
* @var InterventionRepositoryInterface
*/
protected $interventionRepository;
/**
* InterventionController constructor.
*
* @param InterventionRepositoryInterface $interventionRepository
*/
public function __construct(InterventionRepositoryInterface $interventionRepository)
{
$this->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);
}
}
}