-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
15 changed files
with
246 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 0 additions & 11 deletions
11
app/Domains/Taxonomy/Models/Traits/Scope/TaxonomyScope.php
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use App\Domains\Taxonomy\Models\Taxonomy; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class TaxonomyListResource extends JsonResource | ||
{ | ||
public $collects = Taxonomy::class; | ||
|
||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable | ||
*/ | ||
public function toArray($request) | ||
{ | ||
|
||
return [ | ||
'code' => $this->code, | ||
'name' => $this->name, | ||
'description' => $this->description, | ||
'api' => config('app.url', '') . "/api/taxonomy/v1/" . $this->code, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
// use App\Domains\Auth\Models\User; | ||
use App\Domains\Taxonomy\Models\Taxonomy; | ||
use App\Http\Resources\TaxonomyTermResource; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class TaxonomyResource extends JsonResource | ||
{ | ||
public $collects = Taxonomy::class; | ||
|
||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable | ||
*/ | ||
public function toArray($request) | ||
{ | ||
|
||
return [ | ||
'code' => $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, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
// use App\Domains\Auth\Models\User; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class TaxonomyTermResource extends JsonResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable | ||
*/ | ||
public function toArray($request) | ||
{ | ||
$metadata = $this->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, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.