2024-01-10 15:10:21 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Http\Requests\CarPostRequest;
|
|
|
|
use App\Models\Car;
|
2024-01-16 15:14:13 +04:00
|
|
|
use App\Models\User;
|
2024-01-10 15:10:21 +04:00
|
|
|
use App\Repositories\Interfaces\CarRepositoryInterface;
|
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
use Illuminate\View\View;
|
|
|
|
|
|
|
|
class CarController extends Controller
|
|
|
|
{
|
|
|
|
public function __construct(
|
2024-01-16 15:14:13 +04:00
|
|
|
private readonly CarRepositoryInterface $carRepository
|
2024-01-10 15:10:21 +04:00
|
|
|
){
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*/
|
|
|
|
public function index(): View
|
|
|
|
{
|
|
|
|
return view('cars.index', [
|
|
|
|
'cars' => $this->carRepository->getAllCars(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
*/
|
2024-01-16 15:14:13 +04:00
|
|
|
public function create(User $user): View
|
2024-01-10 15:10:21 +04:00
|
|
|
{
|
2024-01-18 16:41:28 +04:00
|
|
|
$this->authorize('create', $user);
|
|
|
|
|
2024-01-16 15:14:13 +04:00
|
|
|
return view('cars.create', [
|
|
|
|
'user' => $user,
|
|
|
|
]);
|
2024-01-10 15:10:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*/
|
2024-01-16 15:14:13 +04:00
|
|
|
public function store(CarPostRequest $request, User $user): RedirectResponse
|
2024-01-10 15:10:21 +04:00
|
|
|
{
|
2024-01-18 16:41:28 +04:00
|
|
|
$this->authorize('create', $user);
|
2024-01-10 15:10:21 +04:00
|
|
|
$this->carRepository->createCar($request->validated());
|
|
|
|
|
2024-01-16 15:14:13 +04:00
|
|
|
return redirect()->route('users.show', $user);
|
2024-01-10 15:10:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*/
|
2024-01-16 15:14:13 +04:00
|
|
|
public function show(User $user, Car $car): View
|
2024-01-10 15:10:21 +04:00
|
|
|
{
|
|
|
|
return view('cars.show', [
|
|
|
|
'car' => $car,
|
2024-01-16 15:14:13 +04:00
|
|
|
'user' => $user,
|
2024-01-10 15:10:21 +04:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for editing the specified resource.
|
|
|
|
*/
|
2024-01-16 15:14:13 +04:00
|
|
|
public function edit(User $user, Car $car): View
|
2024-01-10 15:10:21 +04:00
|
|
|
{
|
2024-01-18 16:41:28 +04:00
|
|
|
$this->authorize('update', $car);
|
|
|
|
|
2024-01-10 15:10:21 +04:00
|
|
|
return view('cars.edit', [
|
|
|
|
'car' => $car,
|
2024-01-16 15:14:13 +04:00
|
|
|
'user' => $user,
|
2024-01-10 15:10:21 +04:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*/
|
2024-01-16 15:14:13 +04:00
|
|
|
public function update(CarPostRequest $request, User $user, Car $car): RedirectResponse
|
2024-01-10 15:10:21 +04:00
|
|
|
{
|
2024-01-18 16:41:28 +04:00
|
|
|
$this->authorize('update', $car);
|
2024-01-10 15:10:21 +04:00
|
|
|
$this->carRepository->updateCar($car, $request->validated());
|
|
|
|
|
2024-01-16 15:14:13 +04:00
|
|
|
return redirect()->route('users.show', $user);
|
2024-01-10 15:10:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*/
|
2024-01-16 15:14:13 +04:00
|
|
|
public function destroy(User $user, Car $car): RedirectResponse
|
2024-01-10 15:10:21 +04:00
|
|
|
{
|
2024-01-18 16:41:28 +04:00
|
|
|
$this->authorize('delete', $car);
|
2024-01-10 15:10:21 +04:00
|
|
|
$this->carRepository->deleteCar($car);
|
|
|
|
|
2024-01-16 15:14:13 +04:00
|
|
|
return redirect()->route('users.show', [
|
|
|
|
'user' => $user,
|
|
|
|
]);
|
2024-01-10 15:10:21 +04:00
|
|
|
}
|
|
|
|
}
|