Adds model, repo, controller, and request classes for Vehicle and Convoy. Registers routes for vehicles and convoys, updates client store. Adds front‑end files to list, add, edit vehicles. Cleans up console logging from client store.
145 lines
4.9 KiB
PHP
145 lines
4.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\StoreConvoyRequest;
|
|
use App\Http\Requests\UpdateConvoyRequest;
|
|
use App\Http\Resources\Convoy\ConvoyResource;
|
|
use App\Repositories\ConvoyRepositoryInterface;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class ConvoyController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly ConvoyRepositoryInterface $convoyRepository
|
|
) {
|
|
}
|
|
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$convoys = $this->convoyRepository->paginate(
|
|
(int) $request->integer('per_page', 15),
|
|
$request->only(['search', 'status', 'convoy_type', 'vehicle_id', 'deceased_id', 'sort_by', 'sort_direction'])
|
|
);
|
|
|
|
return response()->json([
|
|
'data' => ConvoyResource::collection($convoys->items()),
|
|
'meta' => [
|
|
'current_page' => $convoys->currentPage(),
|
|
'last_page' => $convoys->lastPage(),
|
|
'per_page' => $convoys->perPage(),
|
|
'total' => $convoys->total(),
|
|
],
|
|
'status' => 'success',
|
|
]);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error fetching convoys: ' . $e->getMessage());
|
|
|
|
return response()->json([
|
|
'message' => 'An error occurred while fetching convoys.',
|
|
'error' => config('app.debug') ? $e->getMessage() : null,
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
public function store(StoreConvoyRequest $request): JsonResponse
|
|
{
|
|
try {
|
|
$convoy = $this->convoyRepository->create($request->validated());
|
|
|
|
return response()->json([
|
|
'data' => new ConvoyResource($convoy->load(['deceased', 'client', 'vehicle', 'departureLocation'])),
|
|
'message' => 'Convoy created successfully.',
|
|
'status' => 'success',
|
|
], 201);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error creating convoy: ' . $e->getMessage());
|
|
|
|
return response()->json([
|
|
'message' => 'An error occurred while creating the convoy.',
|
|
'error' => config('app.debug') ? $e->getMessage() : null,
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
public function show(string $id): JsonResponse
|
|
{
|
|
try {
|
|
$convoy = $this->convoyRepository->find((int) $id);
|
|
|
|
if (! $convoy) {
|
|
return response()->json(['message' => 'Convoy not found.'], 404);
|
|
}
|
|
|
|
$convoy->load(['deceased', 'client', 'vehicle', 'departureLocation']);
|
|
|
|
return response()->json([
|
|
'data' => new ConvoyResource($convoy),
|
|
'status' => 'success',
|
|
]);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error fetching convoy: ' . $e->getMessage());
|
|
|
|
return response()->json([
|
|
'message' => 'An error occurred while fetching the convoy.',
|
|
'error' => config('app.debug') ? $e->getMessage() : null,
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
public function update(UpdateConvoyRequest $request, string $id): JsonResponse
|
|
{
|
|
try {
|
|
$updated = $this->convoyRepository->update((int) $id, $request->validated());
|
|
|
|
if (! $updated) {
|
|
return response()->json(['message' => 'Convoy not found or update failed.'], 404);
|
|
}
|
|
|
|
$convoy = $this->convoyRepository->find((int) $id);
|
|
|
|
return response()->json([
|
|
'data' => new ConvoyResource($convoy->load(['deceased', 'client', 'vehicle', 'departureLocation'])),
|
|
'message' => 'Convoy updated successfully.',
|
|
'status' => 'success',
|
|
]);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error updating convoy: ' . $e->getMessage());
|
|
|
|
return response()->json([
|
|
'message' => 'An error occurred while updating the convoy.',
|
|
'error' => config('app.debug') ? $e->getMessage() : null,
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
public function destroy(string $id): JsonResponse
|
|
{
|
|
try {
|
|
$deleted = $this->convoyRepository->delete((int) $id);
|
|
|
|
if (! $deleted) {
|
|
return response()->json(['message' => 'Convoy not found or delete failed.'], 404);
|
|
}
|
|
|
|
return response()->json([
|
|
'message' => 'Convoy deleted successfully.',
|
|
'status' => 'success',
|
|
]);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error deleting convoy: ' . $e->getMessage());
|
|
|
|
return response()->json([
|
|
'message' => 'An error occurred while deleting the convoy.',
|
|
'error' => config('app.debug') ? $e->getMessage() : null,
|
|
], 500);
|
|
}
|
|
}
|
|
}
|