From b5a264d92f9c57d88b58c62c87f71cf32221e453 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Wed, 14 Jun 2023 16:58:35 +0300 Subject: [PATCH] Added Category model, resource and its API. Updated Company model to have Categories. --- .../Controllers/API/v2/CategoryController.php | 71 +++++++++++++++++++ app/Http/Requests/StoreCategoryRequest.php | 28 ++++++++ app/Http/Requests/UpdateCategoryRequest.php | 28 ++++++++ .../Resources/API/v2/CategoryCollection.php | 19 +++++ .../Resources/API/v2/CategoryResource.php | 24 +++++++ app/Models/Category.php | 23 ++++++ app/Models/Company.php | 6 ++ app/Policies/CategoryPolicy.php | 66 +++++++++++++++++ database/factories/CategoryFactory.php | 23 ++++++ ...3_06_14_133652_create_categories_table.php | 35 +++++++++ database/seeders/CategorySeeder.php | 17 +++++ routes/api.php | 1 + 12 files changed, 341 insertions(+) create mode 100644 app/Http/Controllers/API/v2/CategoryController.php create mode 100644 app/Http/Requests/StoreCategoryRequest.php create mode 100644 app/Http/Requests/UpdateCategoryRequest.php create mode 100644 app/Http/Resources/API/v2/CategoryCollection.php create mode 100644 app/Http/Resources/API/v2/CategoryResource.php create mode 100644 app/Models/Category.php create mode 100644 app/Policies/CategoryPolicy.php create mode 100644 database/factories/CategoryFactory.php create mode 100644 database/migrations/2023_06_14_133652_create_categories_table.php create mode 100644 database/seeders/CategorySeeder.php diff --git a/app/Http/Controllers/API/v2/CategoryController.php b/app/Http/Controllers/API/v2/CategoryController.php new file mode 100644 index 0000000..f8867b2 --- /dev/null +++ b/app/Http/Controllers/API/v2/CategoryController.php @@ -0,0 +1,71 @@ + + */ + public function rules(): array + { + return [ + // + ]; + } +} diff --git a/app/Http/Requests/UpdateCategoryRequest.php b/app/Http/Requests/UpdateCategoryRequest.php new file mode 100644 index 0000000..836eb1e --- /dev/null +++ b/app/Http/Requests/UpdateCategoryRequest.php @@ -0,0 +1,28 @@ + + */ + public function rules(): array + { + return [ + // + ]; + } +} diff --git a/app/Http/Resources/API/v2/CategoryCollection.php b/app/Http/Resources/API/v2/CategoryCollection.php new file mode 100644 index 0000000..a6083b2 --- /dev/null +++ b/app/Http/Resources/API/v2/CategoryCollection.php @@ -0,0 +1,19 @@ + + */ + public function toArray(Request $request): array + { + return parent::toArray($request); + } +} diff --git a/app/Http/Resources/API/v2/CategoryResource.php b/app/Http/Resources/API/v2/CategoryResource.php new file mode 100644 index 0000000..0681979 --- /dev/null +++ b/app/Http/Resources/API/v2/CategoryResource.php @@ -0,0 +1,24 @@ + + */ + public function toArray(Request $request): array + { + return [ + 'id' => $this->id, + 'uuid' => $this->uuid, + 'name' => $this->name, + 'url' => $this->url, + ]; + } +} diff --git a/app/Models/Category.php b/app/Models/Category.php new file mode 100644 index 0000000..b5b1b1d --- /dev/null +++ b/app/Models/Category.php @@ -0,0 +1,23 @@ +belongsTo(Company::class); + } +} diff --git a/app/Models/Company.php b/app/Models/Company.php index 3e2f1a1..101aa67 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -4,6 +4,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\HasMany; class Company extends Model { @@ -15,4 +16,9 @@ class Company extends Model 'image', 'description', ]; + + public function categories(): HasMany + { + return $this->hasMany(Category::class); + } } diff --git a/app/Policies/CategoryPolicy.php b/app/Policies/CategoryPolicy.php new file mode 100644 index 0000000..b7782e8 --- /dev/null +++ b/app/Policies/CategoryPolicy.php @@ -0,0 +1,66 @@ + + */ +class CategoryFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + // + ]; + } +} diff --git a/database/migrations/2023_06_14_133652_create_categories_table.php b/database/migrations/2023_06_14_133652_create_categories_table.php new file mode 100644 index 0000000..fc7061d --- /dev/null +++ b/database/migrations/2023_06_14_133652_create_categories_table.php @@ -0,0 +1,35 @@ +id(); + $table->unsignedBiginteger('company_id')->unsigned(); + $table->string('uuid'); + $table->string('name'); + $table->string('url'); + + $table->foreign('company_id')->references('id') + ->on('companies')->onDelete('cascade'); + + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('categories'); + } +}; diff --git a/database/seeders/CategorySeeder.php b/database/seeders/CategorySeeder.php new file mode 100644 index 0000000..bc3c0e2 --- /dev/null +++ b/database/seeders/CategorySeeder.php @@ -0,0 +1,17 @@ +