Add Cart model, resource and its API, Pivot table with Items.

Update Items model to have relationship with cart.
This commit is contained in:
2023-06-15 17:43:59 +03:00
parent ea363b956e
commit 4a4d5e7335
12 changed files with 388 additions and 0 deletions
@@ -0,0 +1,67 @@
<?php
namespace App\Http\Controllers\API\v2;
use App\Http\Requests\StoreCartRequest;
use App\Http\Requests\UpdateCartRequest;
use App\Http\Resources\API\v2\CartResource;
use App\Models\Cart;
class CartController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(StoreCartRequest $request)
{
//
}
/**
* Display the specified resource.
*/
public function show(Cart $cart)
{
return new CartResource($cart);
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Cart $cart)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(UpdateCartRequest $request, Cart $cart)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Cart $cart)
{
//
}
}
+28
View File
@@ -0,0 +1,28 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreCartRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return false;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
*/
public function rules(): array
{
return [
//
];
}
}
+28
View File
@@ -0,0 +1,28 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UpdateCartRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return false;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
*/
public function rules(): array
{
return [
//
];
}
}
@@ -0,0 +1,19 @@
<?php
namespace App\Http\Resources\API\v2;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;
class CartCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @return array<int|string, mixed>
*/
public function toArray(Request $request): array
{
return parent::toArray($request);
}
}
@@ -0,0 +1,28 @@
<?php
namespace App\Http\Resources\API\v2;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class CartResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
$city_name = $this->city->name;
$company_name = $this->company->name;
$items = $this->items;
return [
'id' => $this->id,
'cityName' => $city_name,
'companyName' => $company_name,
'item' => new ItemCollection($items),
];
}
}