Skip to content

Commit

Permalink
Taxonomy Preview implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
NuwanJ committed Oct 17, 2024
1 parent b4db0cb commit 43fa4aa
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ public function scopeOfVersion($query, $version)
{
return $query->where('version', $version);
}
}
}
19 changes: 16 additions & 3 deletions app/Domains/Taxonomy/Models/Taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ class Taxonomy extends Model
protected static $logFillable = true;
protected static $logOnlyDirty = true;

/**
* @var string[]
*/
protected $fillable = [
'code',
'name',
Expand All @@ -44,6 +41,11 @@ class Taxonomy extends Model
'image' => 'Image'
];

protected $casts = [
'created_at' => 'datetime',
'updated_at' => 'datetime',
];

public function user()
{
return $this->belongsTo(User::class, 'created_by');
Expand All @@ -64,6 +66,17 @@ public function terms()
return $this->hasMany(TaxonomyTerm::class, 'taxonomy_id');
}

public function to_dict()
{
$taxonomy = $this->toArray();
foreach (['properties', 'created_at', 'updated_at', 'created_by', 'updated_by'] as $attribute) {
unset($taxonomy[$attribute]);
}
$taxonomy['properties'] = json_decode($this->properties);
$taxonomy['terms'] = TaxonomyTerm::getByTaxonomy($this->id);
return $taxonomy;
}

protected static function newFactory()
{
return TaxonomyFactory::new();
Expand Down
60 changes: 55 additions & 5 deletions app/Domains/Taxonomy/Models/TaxonomyTerm.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,37 @@ class TaxonomyTerm extends Model
use HasFactory,
LogsActivity;


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

/**
* @var string[]
*/
protected $fillable = [
'code',
'name',
'metadata',
'taxonomy_id',
'parent_id',
];

protected $casts = [
'metadata' => 'json',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];

public function getFormattedMetadataAttribute()
{
$response = array();
$filteredMetadata = array_filter($this->metadata, function ($value) {
return !is_null($value['value']);
});

foreach ($filteredMetadata as $metadata) {
$response[$metadata['code']] = $metadata['value'];
}

return $response;
}

public function user()
{
return $this->belongsTo(User::class, 'created_by');
Expand Down Expand Up @@ -68,14 +83,49 @@ public function getMetadata($code)

if (is_array($metadata)) {
foreach ($metadata as $item) {
if ($item['code'] === $code) {
if ($item['code'] === $code && $item['value'] != null) {
return $item['value'];
}
}
}
return null;
}

public static function getByTaxonomy($taxonomyId, $parent = null)
{

if ($parent == null) {
$res = TaxonomyTerm::where(function ($query) use ($taxonomyId) {
$query->whereNull('parent_id')->whereAnd('parent_id', $taxonomyId);
});
} else {
$res = TaxonomyTerm::where(function ($query) use ($taxonomyId, $parent) {
$query->where('parent_id', $parent)->whereAnd('parent_id', $taxonomyId);
});
}

$taxonomyTerms = [];
foreach ($res->get() as $term) {
$termData = $term->to_dict();

if ($term->children()->count() > 0) {
$termData['terms'] = $term->getByTaxonomy($taxonomyId, $term->id);
}
array_push($taxonomyTerms, $termData);
}
return $taxonomyTerms;
}

public function to_dict()
{
$taxonomyTerm = $this->toArray();
foreach (['id', 'taxonomy_id', 'parent_id', 'metadata', 'created_at', 'updated_at', 'created_by', 'updated_by'] as $attribute) {
unset($taxonomyTerm[$attribute]);
}
$taxonomyTerm['metadata'] = $this->formatted_metadata;
return $taxonomyTerm;
}

protected static function newFactory()
{
return TaxonomyTermFactory::new();
Expand Down
47 changes: 29 additions & 18 deletions app/Http/Controllers/Backend/TaxonomyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,26 @@ class TaxonomyController extends Controller
*/
public function create()
{
try{
try {
return view('backend.taxonomy.create');
}catch (\Exception $ex) {
Log::error('Failed to load taxonomy creation page', ['error' => $ex->getMessage()]);
} catch (\Exception $ex) {
Log::error('Failed to load taxonomy creation page', ['error' => $ex->getMessage()]);
return abort(500);
}
}
}

/**
* Preview the resource .
*
* @param \App\Domains\Taxonomy\Models\Taxonomy $taxonomy
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
*/
public function view(Taxonomy $taxonomy)
{
$taxonomyData = $taxonomy->to_dict();
return view('backend.taxonomy.view', compact('taxonomyData'));
}

/**
* Store a newly created resource in storage.
*
Expand All @@ -32,19 +45,19 @@ public function create()
*/
public function store(Request $request)
{
$validatedData =$request->validate([
$validatedData = $request->validate([
'code' => 'required|unique:taxonomies',
'name' => 'required',
'description' => 'nullable',
]);
try{

try {
$taxonomy = new Taxonomy($validatedData);
$taxonomy->properties = $request->properties;
$taxonomy->created_by = Auth::user()->id;
$taxonomy->save();
return redirect()->route('dashboard.taxonomy.index')->with('Success', 'Taxonomy created successfully');
}catch (\Exception $ex) {
} catch (\Exception $ex) {
Log::error('Failed to create taxonomy', ['error' => $ex->getMessage()]);
return abort(500);
}
Expand All @@ -66,7 +79,7 @@ public function edit(Taxonomy $taxonomy)
return abort(500);
}
}


/**
* Update the specified resource in storage.
Expand All @@ -80,22 +93,21 @@ public function update(Request $request, Taxonomy $taxonomy)
$data = $request->validate([
'code' => 'required',
'name' => 'required',
'description' => 'nullable',
'description' => 'nullable',
]);
try{

try {
$taxonomy->update($data);
$taxonomy->properties = $request->properties;
$taxonomy->updated_by = Auth::user()->id;
$taxonomy->updated_by = Auth::user()->id;
$taxonomy->save();
return redirect()->route('dashboard.taxonomy.index')->with('Success', 'Taxonomy updated successfully');
}catch (\Exception $ex) {
} catch (\Exception $ex) {
Log::error('Failed to update taxonomy', ['error' => $ex->getMessage()]);
return abort(500);
}

}
/**
/**
* Confirm to delete the specified resource from storage.
*
* @param \App\Domains\Taxonomy\Models\Taxonomy $taxonomy
Expand Down Expand Up @@ -123,5 +135,4 @@ public function destroy(Taxonomy $taxonomy)
return abort(500);
}
}
}

}
12 changes: 6 additions & 6 deletions resources/views/backend/taxonomy/index-table-row.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@

<x-livewire-tables::table.cell>
<div class="d-flex px-0 mt-0 mb-0">
<!-- Manage Button -->
<a href="{{ route('dashboard.taxonomy.terms.index', $row) }}" class="btn btn-sm btn-secondary me-3">
<i class="fa fa-list" title="Manage"></i>
</a>

<div class="btn-group" role="group" aria-label="">

<!-- View Button -->
{{-- <a href="{{ route('taxonomy.view', $row) }}" class="btn btn-sm btn-primary">
<a href="{{ route('dashboard.taxonomy.view', $row) }}" class="btn btn-sm btn-primary">
<i class="fa fa-eye" title="View"></i>
</a> --}}

<!-- Manage Button -->
<a href="{{ route('dashboard.taxonomy.terms.index', $row) }}" class="btn btn-sm btn-secondary">
<i class="fa fa-list" title="Manage"></i>
</a>

<!-- Edit Button -->
Expand Down
23 changes: 23 additions & 0 deletions resources/views/backend/taxonomy/view.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@extends('backend.layouts.app')

@section('title', __('View Taxonomy'))

@section('content')

<div class="container mt-4">

<h3>Taxonomy: {{ $taxonomyData['name'] }} ({{ $taxonomyData['code'] }})</h3>

<p>API Endpoint:
<a target="_blank" href="{{ route('api.taxonomy.get', ['taxonomy_code' => $taxonomyData['code']]) }}">
{{ route('api.taxonomy.get', ['taxonomy_code' => $taxonomyData['code']]) }}
</a>
</p>

<h4>Response: </h4>
<pre class="p-3 border rounded">
{{ json_encode($taxonomyData, JSON_PRETTY_PRINT) }}
</pre>
</div>

@endsection
17 changes: 16 additions & 1 deletion routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,19 @@
Route::get('/semesters', [SemesterApiController::class, 'index']);
});

// TODO: Implement postgraduate courses API
// TODO: Implement postgraduate courses API


Route::group(['prefix' => 'taxonomy/v1/', 'as' => 'api.taxonomy.'], function () {
Route::get('/{taxonomy_code}', function ($taxonomy_code) {
// TODO implement via a Controller
return [];
})->name('get');
Route::get(
'/term/{taxonomy_term_code}',
function ($taxonomy_term_code) {
// TODO implement via a Controller
return [];
}
)->name('term.get');
});
14 changes: 13 additions & 1 deletion routes/backend/taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
->breadcrumbs(function (Trail $trail, $taxonomy) {
$trail->push(__('Home'), route('dashboard.home'))
->push(__('Taxonomy'), route('dashboard.taxonomy.index'))
->push($taxonomy->name)
->push(__('Edit'), route('dashboard.taxonomy.edit', $taxonomy));
});

Expand All @@ -48,9 +49,20 @@
->breadcrumbs(function (Trail $trail, $taxonomy) {
$trail->push(__('Home'), route('dashboard.home'))
->push(__('Taxonomy'), route('dashboard.taxonomy.index'))
->push($taxonomy->name)
->push(__('Delete'));
});


// View
Route::get('taxonomy/view/{taxonomy}', [TaxonomyController::class, 'view'])
->name('taxonomy.view')->breadcrumbs(function (Trail $trail, $taxonomy) {
$trail->push(__('Home'), route('dashboard.home'))
->push(__('Taxonomy'), route('dashboard.taxonomy.index'))
->push($taxonomy->name)
->push(__('View'));
});

// Destroy
Route::delete('taxonomy/{taxonomy}', [TaxonomyController::class, 'destroy'])
->name('taxonomy.destroy');
Expand Down Expand Up @@ -112,4 +124,4 @@
Route::delete('/{term}', [TaxonomyTermController::class, 'destroy'])
->name('taxonomy.terms.destroy');
});
});
});

0 comments on commit 43fa4aa

Please sign in to comment.