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.8 KiB
PHP
145 lines
4.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\StoreVehicleRequest;
|
|
use App\Http\Requests\UpdateVehicleRequest;
|
|
use App\Http\Resources\Vehicle\VehicleResource;
|
|
use App\Repositories\VehicleRepositoryInterface;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class VehicleController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly VehicleRepositoryInterface $vehicleRepository
|
|
) {
|
|
}
|
|
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$vehicles = $this->vehicleRepository->paginate(
|
|
(int) $request->integer('per_page', 15),
|
|
$request->only(['search', 'status', 'vehicle_type', 'sort_by', 'sort_direction'])
|
|
);
|
|
|
|
return response()->json([
|
|
'data' => VehicleResource::collection($vehicles->items()),
|
|
'meta' => [
|
|
'current_page' => $vehicles->currentPage(),
|
|
'last_page' => $vehicles->lastPage(),
|
|
'per_page' => $vehicles->perPage(),
|
|
'total' => $vehicles->total(),
|
|
],
|
|
'status' => 'success',
|
|
]);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error fetching vehicles: ' . $e->getMessage());
|
|
|
|
return response()->json([
|
|
'message' => 'An error occurred while fetching vehicles.',
|
|
'error' => config('app.debug') ? $e->getMessage() : null,
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
public function store(StoreVehicleRequest $request): JsonResponse
|
|
{
|
|
try {
|
|
$vehicle = $this->vehicleRepository->create($request->validated());
|
|
|
|
return response()->json([
|
|
'data' => new VehicleResource($vehicle->load('primaryUser')),
|
|
'message' => 'Vehicle created successfully.',
|
|
'status' => 'success',
|
|
], 201);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error creating vehicle: ' . $e->getMessage());
|
|
|
|
return response()->json([
|
|
'message' => 'An error occurred while creating the vehicle.',
|
|
'error' => config('app.debug') ? $e->getMessage() : null,
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
public function show(string $id): JsonResponse
|
|
{
|
|
try {
|
|
$vehicle = $this->vehicleRepository->find((int) $id);
|
|
|
|
if (! $vehicle) {
|
|
return response()->json(['message' => 'Vehicle not found.'], 404);
|
|
}
|
|
|
|
$vehicle->load(['primaryUser', 'convoys']);
|
|
|
|
return response()->json([
|
|
'data' => new VehicleResource($vehicle),
|
|
'status' => 'success',
|
|
]);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error fetching vehicle: ' . $e->getMessage());
|
|
|
|
return response()->json([
|
|
'message' => 'An error occurred while fetching the vehicle.',
|
|
'error' => config('app.debug') ? $e->getMessage() : null,
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
public function update(UpdateVehicleRequest $request, string $id): JsonResponse
|
|
{
|
|
try {
|
|
$updated = $this->vehicleRepository->update((int) $id, $request->validated());
|
|
|
|
if (! $updated) {
|
|
return response()->json(['message' => 'Vehicle not found or update failed.'], 404);
|
|
}
|
|
|
|
$vehicle = $this->vehicleRepository->find((int) $id);
|
|
|
|
return response()->json([
|
|
'data' => new VehicleResource($vehicle->load('primaryUser')),
|
|
'message' => 'Vehicle updated successfully.',
|
|
'status' => 'success',
|
|
]);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error updating vehicle: ' . $e->getMessage());
|
|
|
|
return response()->json([
|
|
'message' => 'An error occurred while updating the vehicle.',
|
|
'error' => config('app.debug') ? $e->getMessage() : null,
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
public function destroy(string $id): JsonResponse
|
|
{
|
|
try {
|
|
$deleted = $this->vehicleRepository->delete((int) $id);
|
|
|
|
if (! $deleted) {
|
|
return response()->json(['message' => 'Vehicle not found or delete failed.'], 404);
|
|
}
|
|
|
|
return response()->json([
|
|
'message' => 'Vehicle deleted successfully.',
|
|
'status' => 'success',
|
|
]);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error deleting vehicle: ' . $e->getMessage());
|
|
|
|
return response()->json([
|
|
'message' => 'An error occurred while deleting the vehicle.',
|
|
'error' => config('app.debug') ? $e->getMessage() : null,
|
|
], 500);
|
|
}
|
|
}
|
|
}
|