Added Item model, resource and its API.

Updated Category model to have Items.
This commit is contained in:
2023-06-14 17:16:50 +03:00
parent b5a264d92f
commit 18bd849827
12 changed files with 350 additions and 0 deletions
@@ -0,0 +1,71 @@
<?php
namespace App\Http\Controllers\API\v2;
use App\Http\Requests\StoreItemRequest;
use App\Http\Requests\UpdateItemRequest;
use App\Models\Item;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Resources\API\v2\ItemResource;
use App\Http\Resources\API\v2\ItemCollection;
class ItemController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
return new ItemCollection(Item::all());
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(StoreItemRequest $request)
{
//
}
/**
* Display the specified resource.
*/
public function show(Item $item)
{
return new ItemResource($item);
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Item $item)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(UpdateItemRequest $request, Item $item)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Item $item)
{
//
}
}
+28
View File
@@ -0,0 +1,28 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreItemRequest 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 UpdateItemRequest 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 ItemCollection 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,27 @@
<?php
namespace App\Http\Resources\API\v2;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class ItemResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'uuid' => $this->uuid,
'name' => $this->name,
'url' => $this->url,
'description' => $this->description,
'price' => $this->price,
'image' => $this->image,
];
}
}
+6
View File
@@ -5,6 +5,7 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Category extends Model
{
@@ -20,4 +21,9 @@ class Category extends Model
{
return $this->belongsTo(Company::class);
}
public function items(): HasMany
{
return $this->hasMany(Item::class);
}
}
+26
View File
@@ -0,0 +1,26 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Item extends Model
{
use HasFactory;
protected $fillable = [
'uuid',
'name',
'url',
'description',
'price',
'image',
];
public function category(): BelongsTo
{
return $this->belongsTo(Category::class);
}
}
+66
View File
@@ -0,0 +1,66 @@
<?php
namespace App\Policies;
use App\Models\Item;
use App\Models\User;
use Illuminate\Auth\Access\Response;
class ItemPolicy
{
/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user): bool
{
//
}
/**
* Determine whether the user can view the model.
*/
public function view(User $user, Item $item): bool
{
//
}
/**
* Determine whether the user can create models.
*/
public function create(User $user): bool
{
//
}
/**
* Determine whether the user can update the model.
*/
public function update(User $user, Item $item): bool
{
//
}
/**
* Determine whether the user can delete the model.
*/
public function delete(User $user, Item $item): bool
{
//
}
/**
* Determine whether the user can restore the model.
*/
public function restore(User $user, Item $item): bool
{
//
}
/**
* Determine whether the user can permanently delete the model.
*/
public function forceDelete(User $user, Item $item): bool
{
//
}
}