Added Item model, resource and its API.

Updated Category model to have Items.
main
KKlochko 2 years ago
parent b5a264d92f
commit 18bd849827

@ -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)
{
//
}
}

@ -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 [
//
];
}
}

@ -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,
];
}
}

@ -5,6 +5,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; use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Category extends Model class Category extends Model
{ {
@ -20,4 +21,9 @@ class Category extends Model
{ {
return $this->belongsTo(Company::class); return $this->belongsTo(Company::class);
} }
public function items(): HasMany
{
return $this->hasMany(Item::class);
}
} }

@ -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);
}
}

@ -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
{
//
}
}

@ -0,0 +1,23 @@
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Item>
*/
class ItemFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
//
];
}
}

@ -0,0 +1,38 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('items', function (Blueprint $table) {
$table->id();
$table->unsignedBiginteger('category_id')->unsigned();
$table->string('uuid');
$table->string('name');
$table->string('url');
$table->string('description');
$table->decimal('price', 8, 2);
$table->string('image');
$table->foreign('category_id')->references('id')
->on('categories')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('items');
}
};

@ -0,0 +1,17 @@
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class ItemSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
//
}
}

@ -27,6 +27,7 @@ Route::group([
Route::apiResource('cities', CityController::class); Route::apiResource('cities', CityController::class);
Route::apiResource('companies', CompanyController::class); Route::apiResource('companies', CompanyController::class);
Route::apiResource('categories', CategoryController::class); Route::apiResource('categories', CategoryController::class);
Route::apiResource('items', ItemController::class);
} }
); );

Loading…
Cancel
Save