Skip to content

Commit

Permalink
Taxonomy Integration - Release 3.0.0 (#171) (#239)
Browse files Browse the repository at this point in the history
Co-authored-by: Supun Dulara <e20087@eng.pdn.ac.lk>
Co-authored-by: Hasan10100 <150885485+Hasan10100@users.noreply.github.com>
Co-authored-by: kusaljayawardhana <e20179@eng.pdn.ac.lk>
Co-authored-by: IsharaEkanayaka <e20094@eng.pdn.ac.lk>
  • Loading branch information
5 people authored Dec 18, 2024
1 parent e95ba8f commit e75ca50
Show file tree
Hide file tree
Showing 40 changed files with 2,938 additions and 1,323 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);
}
}
}
94 changes: 94 additions & 0 deletions app/Domains/Taxonomy/Models/Taxonomy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?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 App\Domains\Taxonomy\Models\TaxonomyTerm;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Domains\Taxonomy\Models\Traits\Scope\TaxonomyScope;

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


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

protected $fillable = [
'code',
'name',
'description',
'properties',
];

public static $propertyType = [
'string' => 'String',
'integer' => 'Integer Number',
'float' => 'Floating Point Number',
'date' => 'Date',
'datetime' => 'Date Time',
'boolean' => 'Boolean',
'url' => 'URL',
// 'image' => 'Image'
// 'pdf' => 'PDF File'
];

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

public function user()
{
return $this->belongsTo(User::class, 'created_by');
}

public function user_created()
{
return $this->belongsTo(User::class, 'created_by');
}

public function user_updated()
{
return $this->belongsTo(User::class, 'created_by');
}

public function terms()
{
return $this->hasMany(TaxonomyTerm::class, 'taxonomy_id')
->orderBy('parent_id', 'asc')
->orderBy('code', 'asc');
}

public function first_child_terms()
{
return $this->hasMany(TaxonomyTerm::class, 'taxonomy_id')
->whereNull('parent_id')
->orderBy('code', 'asc');
}

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

protected static function newFactory()
{
return TaxonomyFactory::new();
}
}
151 changes: 151 additions & 0 deletions app/Domains/Taxonomy/Models/TaxonomyTerm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php

namespace App\Domains\Taxonomy\Models;

use App\Domains\Auth\Models\User;
use Database\Factories\TaxonomyTermFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\Traits\LogsActivity;
use Illuminate\Database\Eloquent\Factories\HasFactory;

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

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

protected $fillable = [
'code',
'name',
'metadata',
'taxonomy_id',
'parent_id',
];

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

public function getFormattedMetadataAttribute()
{
$response = array();
if (is_array($this->metadata)) {
$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');
}

public function user_created()
{
return $this->belongsTo(User::class, 'created_by');
}

public function user_updated()
{
return $this->belongsTo(User::class, 'created_by');
}

public function taxonomy()
{
return $this->belongsTo(Taxonomy::class, 'taxonomy_id');
}

public function parent()
{
return $this->belongsTo(self::class, 'parent_id');
}

public function children()
{
return $this->hasMany(self::class, 'parent_id')
->orderBy('code', 'asc');;
}

public function getMetadata($code)
{
if (is_array($this->metadata)) {
foreach ($this->metadata as $item) {
if ($item['code'] === $code && $item['value'] != null) {
return $item['value'];
}
}
}
return null;
}

protected static function boot()
{
parent::boot();

static::deleting(function ($taxonomyTerm) {
$taxonomyTerm->children()->delete();
});
}

public static function getHierarchicalPath($id)
{
$term = TaxonomyTerm::find($id);
if ($term != null) {
if ($term->parent_id != null) {
return TaxonomyTerm::getHierarchicalPath($term->parent_id) . " > " . $term->name;
} else {
return $term->name;
}
} else {
return '';
}
}

public static function getByTaxonomy($taxonomyId, $parent = null)
{
if ($parent == null) {
$res = TaxonomyTerm::where('taxonomy_id', $taxonomyId)->whereNull('parent_id');
} else {
$res = TaxonomyTerm::where('taxonomy_id', $taxonomyId)->where('parent_id', $parent);
}

$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();
}
}
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;
}
}
82 changes: 82 additions & 0 deletions app/Http/Controllers/API/TaxonomyApiController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use App\Http\Resources\TaxonomyListResource;
use App\Http\Resources\TaxonomyResource;
use App\Http\Resources\TaxonomyTermResource;
use App\Domains\Taxonomy\Models\Taxonomy;
use App\Domains\Taxonomy\Models\TaxonomyTerm;
use Illuminate\Support\Facades\Log;

class TaxonomyApiController extends Controller
{

public function index()
{
try {
$result = Taxonomy::all();

if ($result) {
return response()->json(
[
'status' => 'success',
'data' => TaxonomyListResource::collection($result)
]
);
} else {
return response()->json(['status' => 'error', 'message' => 'No Taxonomies found'], 404);
}
} catch (\Exception $e) {
Log::error('Error in TaxonomyApiController@index', ['error' => $e->getMessage()]);
return response()->json(['status' => 'error', 'message' => 'An error occurred while fetching taxonomy index'], 500);
}
}

public function get_taxonomy($taxonomy_code)
{
try {
$result = Taxonomy::where('code', $taxonomy_code)->first();

if ($result) {
return response()->json(
[
'status' => 'success',
'data' => TaxonomyResource::collection([$result])->resolve()[0]
]
);
} else {
return response()->json(['status' => 'error', 'message' => 'Taxonomy not found'], 404);
}
} catch (\Exception $e) {
Log::error('Error in TaxonomyApiController@get_taxonomy', ['error' => $e->getMessage()]);
return response()->json(['status' => 'error', 'message' => 'An error occurred while fetching a taxonomy'], 500);
}
}


public function get_term($term_code)
{
try {
$result = TaxonomyTerm::where('code', $term_code)->first();

if ($result) {
$data = TaxonomyTermResource::collection([$result])->resolve()[0];
$data['taxonomy'] = $result->taxonomy->code; // Add the taxonomy code at the top-level term

return response()->json(
[
'status' => 'success',
'data' => $data
]
);
} else {
return response()->json(['status' => 'error', 'message' => 'Taxonomy term not found'], 404);
}
} catch (\Exception $e) {
Log::error('Error in TaxonomyApiController@get_term', ['error' => $e->getMessage()]);
return response()->json(['status' => 'error', 'message' => 'An error occurred while fetching a taxonomy term'], 500);
}
}
}
Loading

0 comments on commit e75ca50

Please sign in to comment.