Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add taxonomy model, migration, controller, factory #170

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions app/Domains/Taxonomy/Models/Taxonomy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Domains\Taxonomy\Models;

use App\Domains\Auth\Models\User;
use Database\Factories\TaxonomyFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\Traits\LogsActivity;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Domains\Taxonomy\Models\Traits\Scope\TaxonomyScope;

/**
* Class Taxonomy.
*/
class Taxonomy extends Model
{
use TaxonomyScope,
HasFactory,
LogsActivity;


protected static $logFillable = true;
protected static $logOnlyDirty = true;

/**
* @var string[]
*/
protected $fillable = [
'code',
'name',
'description',
'properties',
];

public function user()
{
return $this->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();
}
}
11 changes: 11 additions & 0 deletions app/Domains/Taxonomy/Models/Traits/Scope/TaxonomyScope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Domains\Taxonomy\Models\Traits\Scope;

/**
* Class TaxonomyScope.
*/
trait TaxonomyScope
{

}
22 changes: 22 additions & 0 deletions app/Domains/Taxonomy/Services/TaxonomyService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Domains\Taxonomy\Services;

use App\Domains\Taxonomy\Models\Taxonomy;
use App\Services\BaseService;

/**
* Class TaxonomyService.
*/
class TaxonomyService extends BaseService
{
/**
* TaxonomyService constructor.
*
* @param Taxonomy $taxonomy
*/
public function __construct(Taxonomy $taxonomy)
{
$this->model = $taxonomy;
}
}
91 changes: 91 additions & 0 deletions app/Http/Controllers/Backend/TaxonomyController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace App\Http\Controllers\Backend;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\Controller;
use App\Domains\Taxonomy\Models\Taxonomy;

class TaxonomyController extends Controller
{
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
*/
public function create()
{
try{
return view('backend.taxonomy.create');
}catch (\Exception $ex) {
Log::error('Failed to load taxonomy creation page', ['error' => $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);
}
}
}

54 changes: 54 additions & 0 deletions database/factories/TaxonomyFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Database\Factories;

use App\Domains\Taxonomy\Models\Taxonomy;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* Class TaxonomyFactory.
*/
class TaxonomyFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Taxonomy::class;

/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'code' => $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(),
];
}
}
37 changes: 37 additions & 0 deletions database/migrations/2024_10_11_120037_create_taxonomies_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTaxonomiesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('taxonomies', function (Blueprint $table) {
$table->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');
}
}
16 changes: 16 additions & 0 deletions database/seeders/TaxonomySeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class NewsSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(){

}

}
Loading