From da98ee87e92ed6ad332cc19f3b0fa13f89b30930 Mon Sep 17 00:00:00 2001 From: IsharaEkanayaka Date: Fri, 11 Oct 2024 13:25:12 +0530 Subject: [PATCH] feat: add taxonomy model, migration, controller, factory --- app/Domains/Taxonomy/Models/Taxonomy.php | 49 ++++++++++ .../Models/Traits/Scope/TaxonomyScope.php | 11 +++ .../Taxonomy/Services/TaxonomyService.php | 22 +++++ .../Backend/TaxonomyController.php | 91 +++++++++++++++++++ database/factories/TaxonomyFactory.php | 54 +++++++++++ ...4_10_11_120037_create_taxonomies_table.php | 37 ++++++++ database/seeders/TaxonomySeeder.php | 16 ++++ 7 files changed, 280 insertions(+) create mode 100644 app/Domains/Taxonomy/Models/Taxonomy.php create mode 100644 app/Domains/Taxonomy/Models/Traits/Scope/TaxonomyScope.php create mode 100644 app/Domains/Taxonomy/Services/TaxonomyService.php create mode 100644 app/Http/Controllers/Backend/TaxonomyController.php create mode 100644 database/factories/TaxonomyFactory.php create mode 100644 database/migrations/2024_10_11_120037_create_taxonomies_table.php create mode 100644 database/seeders/TaxonomySeeder.php diff --git a/app/Domains/Taxonomy/Models/Taxonomy.php b/app/Domains/Taxonomy/Models/Taxonomy.php new file mode 100644 index 00000000..dadd4cd1 --- /dev/null +++ b/app/Domains/Taxonomy/Models/Taxonomy.php @@ -0,0 +1,49 @@ +belongsTo(User::class, 'created_by'); + } + + /** + * Create a new factory instance for the model. + * + * @return \Illuminate\Database\Eloquent\Factories\Factory + */ + protected static function newFactory() + { + return TaxonomyFactory::new(); + } +} \ No newline at end of file diff --git a/app/Domains/Taxonomy/Models/Traits/Scope/TaxonomyScope.php b/app/Domains/Taxonomy/Models/Traits/Scope/TaxonomyScope.php new file mode 100644 index 00000000..f64bb2ab --- /dev/null +++ b/app/Domains/Taxonomy/Models/Traits/Scope/TaxonomyScope.php @@ -0,0 +1,11 @@ +model = $taxonomy; + } +} diff --git a/app/Http/Controllers/Backend/TaxonomyController.php b/app/Http/Controllers/Backend/TaxonomyController.php new file mode 100644 index 00000000..b8e97c5f --- /dev/null +++ b/app/Http/Controllers/Backend/TaxonomyController.php @@ -0,0 +1,91 @@ + $ex->getMessage()]); + return abort(500); + } + } + /** + * Store a newly created resource in storage. + * + * @param \Illuminate\Http\Request $request + * @return \Illuminate\Http\RedirectResponse|void + */ + public function store(Request $request) + { + + } + /** + * Show the form for editing the specified resource. + * + * @param \App\Domains\Taxonomy\Models\Taxonomy $taxonomy + * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View + */ + public function edit(Taxonomy $taxonomy) + { + try{ + return view('backend.taxonomy.edit', ['taxonomy' => $taxonomy]); + }catch (\Exception $ex) { + Log::error('Failed to load taxonomy edit page', ['error' => $ex->getMessage()]); + return abort(500); + } + } + /** + * Update the specified resource in storage. + * + * @param \Illuminate\Http\Request $request + * @param \App\Domains\Taxonomy\Models\Taxonomy $taxonomy + * @return \Illuminate\Http\RedirectResponse + */ + public function update(Request $request, Taxonomy $taxonomy) + { + + } + /** + * Confirm to delete the specified resource from storage. + * + * @param \App\Domains\Taxonomy\Models\Taxonomy $taxonomy + * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View + */ + public function delete(Taxonomy $taxonomy) + { + return view('backend.taxonomy.delete', compact('taxonomy')); + } + + + /** + * Remove the specified resource from storage. + * + * @param \App\Domains\Taxonomy\Models\Taxonomy $taxonomy + * @return \Illuminate\Http\RedirectResponse|null + */ + public function destroy(Taxonomy $taxonomy) + { + try { + $taxonomy->delete(); + return redirect()->route('dashboard.taxonomy.index')->with('Success', 'Taxonomy was deleted !'); + } catch (\Exception $ex) { + Log::error('Failed to delete taxonomy', ['taxonomy_id' => $taxonomy->id, 'error' => $ex->getMessage()]); + return abort(500); + } + } +} + diff --git a/database/factories/TaxonomyFactory.php b/database/factories/TaxonomyFactory.php new file mode 100644 index 00000000..536a6e0d --- /dev/null +++ b/database/factories/TaxonomyFactory.php @@ -0,0 +1,54 @@ + $this->faker->unique()->lexify('????'), + 'name' => $this->faker->word, + 'description' => $this->faker->sentence, + 'properties' => json_encode([ + [ + 'code' => 'country_name', + 'name' => 'Country', + 'data_type' => 'string' + ], + [ + 'code' => 'country_code', + 'name' => 'Country Code', + 'data_type' => 'number' + ], + [ + 'code' => 'visible', + 'name' => 'Visibility', + 'data_type' => 'boolean' + ] + ]), + 'created_by' => 1, + 'updated_by' => 1, + 'created_at' => now(), + 'updated_at' => now(), + ]; + } +} diff --git a/database/migrations/2024_10_11_120037_create_taxonomies_table.php b/database/migrations/2024_10_11_120037_create_taxonomies_table.php new file mode 100644 index 00000000..85527acf --- /dev/null +++ b/database/migrations/2024_10_11_120037_create_taxonomies_table.php @@ -0,0 +1,37 @@ +id(); + $table->string('code', 32)->unique(); + $table->string('name', 191); + $table->string('description')->nullable(); + $table->json('properties'); + $table->foreignId('created_by')->constrained('users')->onUpdate('cascade')->onDelete('set null'); + $table->foreignId('updated_by')->constrained('users')->onUpdate('cascade')->onDelete('set null'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('taxonomies'); + } +} diff --git a/database/seeders/TaxonomySeeder.php b/database/seeders/TaxonomySeeder.php new file mode 100644 index 00000000..fde4ce55 --- /dev/null +++ b/database/seeders/TaxonomySeeder.php @@ -0,0 +1,16 @@ +