Added relationships, validation, controllers

This commit is contained in:
m.zargarov 2024-01-10 15:10:21 +04:00
parent efaf634867
commit 08e43ceb44
16 changed files with 506 additions and 2 deletions

15
app/Enums/RoleEnum.php Normal file
View File

@ -0,0 +1,15 @@
<?php
namespace App\Enums;
enum RoleEnum: int
{
case ADMIN = 1;
case DISPATCHER = 2;
case CLIENT = 3;
public static function getRange()
{
return [RoleEnum::ADMIN, RoleEnum::DISPATCHER, RoleEnum::CLIENT];
}
}

15
app/Enums/StatusEnum.php Normal file
View File

@ -0,0 +1,15 @@
<?php
namespace App\Enums;
enum StatusEnum: int
{
case CREATED = 1;
case SENT = 2;
case DELIVERED = 3;
public static function getRange()
{
return [StatusEnum::CREATED, StatusEnum::SENT, StatusEnum::DELIVERED];
}
}

View File

@ -0,0 +1,85 @@
<?php
namespace App\Http\Controllers;
use App\Http\Requests\CarPostRequest;
use App\Models\Car;
use App\Repositories\Interfaces\CarRepositoryInterface;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
class CarController extends Controller
{
public function __construct(
private CarRepositoryInterface $carRepository
){
}
/**
* 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.
*/
public function create(): View
{
return view('cars.create');
}
/**
* Store a newly created resource in storage.
*/
public function store(CarPostRequest $request): RedirectResponse
{
$this->carRepository->createCar($request->validated());
return redirect()->route('cars.index');
}
/**
* Display the specified resource.
*/
public function show(Car $car): View
{
return view('cars.show', [
'car' => $car,
]);
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Car $car): View
{
return view('cars.edit', [
'car' => $car,
]);
}
/**
* Update the specified resource in storage.
*/
public function update(CarPostRequest $request, Car $car): RedirectResponse
{
$this->carRepository->updateCar($car, $request->validated());
return redirect()->route('cars.index');
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Car $car): RedirectResponse
{
$this->carRepository->deleteCar($car);
return redirect()->route('cars.index');
}
}

View File

@ -0,0 +1,86 @@
<?php
namespace App\Http\Controllers;
use App\Http\Requests\DeliveryPostRequest;
use App\Models\Delivery;
use App\Repositories\Interfaces\DeliveryRepositoryInterface;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
class DeliveryController extends Controller
{
public function __construct(
private DeliveryRepositoryInterface $deliveryRepository
){
}
/**
* Display a listing of the resource.
*/
public function index(): View
{
return view('deliveries.index', [
'deliveries' => $this->deliveryRepository->getAllDeliveries(),
]);
}
/**
* Show the form for creating a new resource.
*/
public function create(): View
{
return view('deliveries.create');
}
/**
* Store a newly created resource in storage.
*/
public function store(DeliveryPostRequest $request): RedirectResponse
{
$this->deliveryRepository->createDelivery($request->validated());
return redirect()->route('deliveries.index');
}
/**
* Display the specified resource.
*/
public function show(Delivery $delivery): View
{
return view('deliveries.show', [
'delivery' => $delivery,
]);
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Delivery $delivery): View
{
return view('deliveries.edit', [
'delivery' => $delivery,
]);
}
/**
* Update the specified resource in storage.
*/
public function update(DeliveryPostRequest $request, Delivery $delivery): RedirectResponse
{
$this->deliveryRepository->updateDelivery($delivery, $request->validated());
return redirect()->route('deliveries.index');
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Delivery $delivery): RedirectResponse
{
$this->deliveryRepository->deleteDelivery($delivery);
return redirect()->route('deliveries.index');
}
}

View File

@ -0,0 +1,85 @@
<?php
namespace App\Http\Controllers;
use App\Http\Requests\UserPostRequest;
use App\Models\User;
use App\Repositories\Interfaces\UserRepositoryInterface;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
class UserController extends Controller
{
public function __construct(
private UserRepositoryInterface $userRepository
){
}
/**
* Display a listing of the resource.
*/
public function index(): View
{
return view('users.index', [
'users' => $this->userRepository->getAllUsers()
]);
}
/**
* Show the form for creating a new resource.
*/
public function create(): View
{
return view('users.create');
}
/**
* Store a newly created resource in storage.
*/
public function store(UserPostRequest $request): RedirectResponse
{
$this->userRepository->createUser($request->validated());
return redirect()->route('users.index');
}
/**
* Display the specified resource.
*/
public function show(User $user): View
{
return view('users.show', [
'user' => $user,
]);
}
/**
* Show the form for editing the specified resource.
*/
public function edit(User $user): View
{
return view('users.edit', [
'user' => $user,
]);
}
/**
* Update the specified resource in storage.
*/
public function update(UserPostRequest $request, User $user): RedirectResponse
{
$this->userRepository->updateUser($user, $request->validated());
return redirect()->route('users.index');
}
/**
* Remove the specified resource from storage.
*/
public function destroy(User $user): RedirectResponse
{
$this->userRepository->deleteUser($user);
return redirect()->route('users.index');
}
}

View File

@ -0,0 +1,85 @@
<?php
namespace App\Http\Controllers;
use App\Http\Requests\WarehousePostRequest;
use App\Models\Warehouse;
use App\Repositories\Interfaces\WarehouseRepositoryInterface;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
class WarehouseController extends Controller
{
public function __construct(
private WarehouseRepositoryInterface $warehouseRepository
){
}
/**
* Display a listing of the resource.
*/
public function index(): View
{
return view('warehouses.index', [
'warehouses' => $this->warehouseRepository->getAllWarehouses(),
]);
}
/**
* Show the form for creating a new resource.
*/
public function create(): View
{
return view('warehouses.view');
}
/**
* Store a newly created resource in storage.
*/
public function store(WarehousePostRequest $request): RedirectResponse
{
$this->warehouseRepository->createWarehouse($request->validated());
return redirect()->route('warehouses.index');
}
/**
* Display the specified resource.
*/
public function show(Warehouse $warehouse): View
{
return view('warehouses.show', [
'warehouse' => $warehouse,
]);
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Warehouse $warehouse): View
{
return view('warehouse.edit', [
'warehouse' => $warehouse,
]);
}
/**
* Update the specified resource in storage.
*/
public function update(WarehousePostRequest $request, Warehouse $warehouse): RedirectResponse
{
$this->warehouseRepository->updateWarehouse($warehouse, $request->validated());
return redirect()->route('warehouses.index');
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Warehouse $warehouse): RedirectResponse
{
$this->warehouseRepository->deleteWarehouse($warehouse);
return redirect()->route('warehouses.index');
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class CarPostRequest extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'name' => 'required|string|max:255',
'number' => 'required|string|max:255',
'driver_id' => 'required|exists:users,id',
];
}
}

View File

@ -0,0 +1,20 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class DeliveryPostRequest extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'delivery' => 'required|string|max:255',
];
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace App\Http\Requests;
use App\Enums\RoleEnum;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class UserPostRequest extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'name' => 'required|string|max:255',
'surname' => 'required|string|max:255',
'patronymic' => 'required|string|max:255',
'email' => 'required|max:255|email|lowercase',
'password' => 'required|max:255',
'role' => ['required', Rule::in(RoleEnum::getRange())],
'phone_number' => 'required|regex:/^([0-9\s\-\+\(\)]*)$/|min:10',
];
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class WarehousePostRequest extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'address' => 'required|string|max:255',
'phone_number' => 'required|regex:/^([0-9\s\-\+\(\)]*)$/|min:10',
];
}
}

View File

@ -4,6 +4,7 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Car extends Model class Car extends Model
{ {
@ -14,4 +15,9 @@ class Car extends Model
'number', 'number',
'driver_id', 'driver_id',
]; ];
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
} }

View File

@ -4,6 +4,8 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class Delivery extends Model class Delivery extends Model
{ {
@ -15,4 +17,14 @@ class Delivery extends Model
'sending_date', 'sending_date',
'delivery_date', 'delivery_date',
]; ];
public function users(): BelongsToMany
{
return $this->belongsToMany(User::class);
}
public function warehouse(): BelongsTo
{
return $this->belongsTo(Warehouse::class);
}
} }

View File

@ -4,6 +4,8 @@ namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail; // use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable; use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens; use Laravel\Sanctum\HasApiTokens;
@ -46,4 +48,14 @@ class User extends Authenticatable
'email_verified_at' => 'datetime', 'email_verified_at' => 'datetime',
'password' => 'hashed', 'password' => 'hashed',
]; ];
public function car(): HasOne
{
return $this->hasOne(Car::class);
}
public function deliveries(): BelongsToMany
{
return $this->belongsToMany(Delivery::class);
}
} }

View File

@ -4,6 +4,7 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Warehouse extends Model class Warehouse extends Model
{ {
@ -13,4 +14,9 @@ class Warehouse extends Model
'address', 'address',
'phoneNumber', 'phoneNumber',
]; ];
public function delivery(): HasMany
{
return $this->hasMany(Delivery::class);
}
} }

View File

@ -14,8 +14,9 @@ return new class extends Migration
Schema::create('deliveries', function (Blueprint $table) { Schema::create('deliveries', function (Blueprint $table) {
$table->id(); $table->id();
$table->unsignedInteger('status'); $table->unsignedInteger('status');
$table->date('sending_date'); $table->date('sending_date')->nullable();
$table->date('delivery_date'); $table->date('delivery_date')->nullable();
$table->foreignId('warehouse_id')->constrained('warehouses');
$table->timestamps(); $table->timestamps();
}); });
} }

View File

@ -1,5 +1,6 @@
<?php <?php
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
/* /*
@ -16,3 +17,7 @@ use Illuminate\Support\Facades\Route;
Route::get('/', function () { Route::get('/', function () {
return view('welcome'); return view('welcome');
}); });
Route::resources([
'users' => UserController::class,
]);