diff --git a/app/Domains/Taxonomy/Models/Taxonomy.php b/app/Domains/Taxonomy/Models/Taxonomy.php index 2472420..08d8aca 100644 --- a/app/Domains/Taxonomy/Models/Taxonomy.php +++ b/app/Domains/Taxonomy/Models/Taxonomy.php @@ -15,8 +15,7 @@ */ class Taxonomy extends Model { - use TaxonomyScope, - HasFactory, + use HasFactory, LogsActivity; @@ -43,6 +42,7 @@ class Taxonomy extends Model ]; protected $casts = [ + 'properties' => 'json', 'created_at' => 'datetime', 'updated_at' => 'datetime', ]; @@ -69,13 +69,20 @@ public function terms() ->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'] = json_decode($this->properties); + $taxonomy['properties'] = $this->properties; $taxonomy['terms'] = TaxonomyTerm::getByTaxonomy($this->id); return $taxonomy; } diff --git a/app/Domains/Taxonomy/Models/TaxonomyTerm.php b/app/Domains/Taxonomy/Models/TaxonomyTerm.php index 1880f69..a53a102 100644 --- a/app/Domains/Taxonomy/Models/TaxonomyTerm.php +++ b/app/Domains/Taxonomy/Models/TaxonomyTerm.php @@ -36,14 +36,15 @@ class TaxonomyTerm extends Model public function getFormattedMetadataAttribute() { $response = array(); - $filteredMetadata = array_filter(json_decode($this->metadata, true), function ($value) { - return !is_null($value['value']); - }); + 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']; + foreach ($filteredMetadata as $metadata) { + $response[$metadata['code']] = $metadata['value']; + } } - return $response; } @@ -74,15 +75,14 @@ public function parent() public function children() { - return $this->hasMany(self::class, 'parent_id'); + return $this->hasMany(self::class, 'parent_id') + ->orderBy('code', 'asc');; } public function getMetadata($code) { - $metadata = json_decode($this->metadata, true); - - if (is_array($metadata)) { - foreach ($metadata as $item) { + if (is_array($this->metadata)) { + foreach ($this->metadata as $item) { if ($item['code'] === $code && $item['value'] != null) { return $item['value']; } diff --git a/app/Domains/Taxonomy/Models/Traits/Scope/TaxonomyScope.php b/app/Domains/Taxonomy/Models/Traits/Scope/TaxonomyScope.php deleted file mode 100644 index f64bb2a..0000000 --- a/app/Domains/Taxonomy/Models/Traits/Scope/TaxonomyScope.php +++ /dev/null @@ -1,11 +0,0 @@ -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); + } + } +} \ No newline at end of file diff --git a/app/Http/Controllers/Backend/TaxonomyController.php b/app/Http/Controllers/Backend/TaxonomyController.php index 04760da..6e01894 100644 --- a/app/Http/Controllers/Backend/TaxonomyController.php +++ b/app/Http/Controllers/Backend/TaxonomyController.php @@ -92,14 +92,15 @@ public function edit(Taxonomy $taxonomy) public function update(Request $request, Taxonomy $taxonomy) { $data = $request->validate([ - 'code' => 'required', - 'name' => 'required', + 'code' => 'string|required', + 'name' => 'string|required', 'description' => 'nullable', + 'properties' => 'string' ]); try { $taxonomy->update($data); - $taxonomy->properties = $request->properties; + $taxonomy->properties = json_decode($request->properties); $taxonomy->updated_by = Auth::user()->id; $taxonomy->save(); return redirect()->route('dashboard.taxonomy.index')->with('Success', 'Taxonomy updated successfully'); diff --git a/app/Http/Controllers/Backend/TaxonomyTermController.php b/app/Http/Controllers/Backend/TaxonomyTermController.php index 7b7d7cd..fe23775 100644 --- a/app/Http/Controllers/Backend/TaxonomyTermController.php +++ b/app/Http/Controllers/Backend/TaxonomyTermController.php @@ -47,7 +47,7 @@ public function store(Request $request, Taxonomy $taxonomy, TaxonomyTerm $taxono ]); - foreach (json_decode($taxonomy->properties, true) as $property) { + foreach ($taxonomy->properties as $property) { $metadataKey = "metadata.{$property['code']}"; switch ($property['data_type']) { @@ -86,7 +86,7 @@ public function store(Request $request, Taxonomy $taxonomy, TaxonomyTerm $taxono $metadataArray = []; - foreach (json_decode($taxonomy->properties, true) as $property) { + foreach ($taxonomy->properties as $property) { $value = $request->input("metadata.{$property['code']}"); if ($property['data_type'] === 'boolean') { @@ -99,7 +99,7 @@ public function store(Request $request, Taxonomy $taxonomy, TaxonomyTerm $taxono } $taxonomyTerm = new TaxonomyTerm($validatedData); - $taxonomyTerm->metadata = json_encode($metadataArray); + $taxonomyTerm->metadata = $metadataArray; $taxonomyTerm->created_by = Auth::user()->id; $taxonomyTerm->save(); @@ -142,7 +142,7 @@ public function update(Request $request, Taxonomy $taxonomy, TaxonomyTerm $term) 'metadata' => 'array', ]); - foreach (json_decode($taxonomy->properties, true) as $property) { + foreach ($taxonomy->properties as $property) { $metadataKey = "metadata.{$property['code']}"; switch ($property['data_type']) { @@ -179,7 +179,7 @@ public function update(Request $request, Taxonomy $taxonomy, TaxonomyTerm $term) } $metadataArray = []; - foreach (json_decode($taxonomy->properties, true) as $property) { + foreach ($taxonomy->properties as $property) { $value = $request->input("metadata.{$property['code']}"); if ($property['data_type'] === 'boolean') { @@ -193,7 +193,7 @@ public function update(Request $request, Taxonomy $taxonomy, TaxonomyTerm $term) } $term->update($validatedData); - $term->metadata = json_encode($metadataArray); + $term->metadata = $metadataArray; $term->updated_by = Auth::user()->id; $term->save(); diff --git a/app/Http/Resources/TaxonomyListResource.php b/app/Http/Resources/TaxonomyListResource.php new file mode 100644 index 0000000..bff3dd1 --- /dev/null +++ b/app/Http/Resources/TaxonomyListResource.php @@ -0,0 +1,28 @@ + $this->code, + 'name' => $this->name, + 'description' => $this->description, + 'api' => config('app.url', '') . "/api/taxonomy/v1/" . $this->code, + ]; + } +} \ No newline at end of file diff --git a/app/Http/Resources/TaxonomyResource.php b/app/Http/Resources/TaxonomyResource.php new file mode 100644 index 0000000..b97d05b --- /dev/null +++ b/app/Http/Resources/TaxonomyResource.php @@ -0,0 +1,35 @@ + $this->code, + 'name' => $this->name, + 'description' => $this->description, + // 'properties' => $this->properties, + 'terms' => TaxonomyTermResource::collection($this->first_child_terms), + // 'created_by' => User::find($this->created_by)?->name, + // 'updated_by' => User::find($this->updated_by)?->name, + // 'created_at' => $this->created_at, + // 'updated_at' => $this->updated_at, + ]; + } +} \ No newline at end of file diff --git a/app/Http/Resources/TaxonomyTermResource.php b/app/Http/Resources/TaxonomyTermResource.php new file mode 100644 index 0000000..252214c --- /dev/null +++ b/app/Http/Resources/TaxonomyTermResource.php @@ -0,0 +1,38 @@ +getFormattedMetadataAttribute(); + + + return [ + 'code' => $this->code, + 'name' => $this->name, + 'terms' => $this->when( + sizeof($this->children) > 0, + TaxonomyTermResource::collection($this->children) + ), + 'metadata' => $this->when( + sizeof($metadata) > 0, + $metadata + ), + // 'created_by' => User::find($this->created_by)?->name, + // 'updated_by' => User::find($this->updated_by)?->name, + // 'created_at' => $this->created_at, + // 'updated_at' => $this->updated_at, + ]; + } +} \ No newline at end of file diff --git a/resources/views/backend/taxonomy/edit.blade.php b/resources/views/backend/taxonomy/edit.blade.php index 16a7fa8..145bc86 100644 --- a/resources/views/backend/taxonomy/edit.blade.php +++ b/resources/views/backend/taxonomy/edit.blade.php @@ -3,7 +3,7 @@ @section('title', __('Edit Taxonomy')) @section('content') -
-{{ json_encode($taxonomyData, JSON_PRETTY_PRINT) }} +{{ json_encode($taxonomyData['terms'], JSON_PRETTY_PRINT) }}