From d4628a5242c0295473d5164a204e035defa90007 Mon Sep 17 00:00:00 2001 From: alexPopaCode4 <156655986+alexPopaCode4@users.noreply.github.com> Date: Wed, 28 Feb 2024 18:18:57 +0200 Subject: [PATCH] Api DSU (#152) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Api DSU * Apply suggestions from code review * Fix code review * rename organization in organisation --------- Co-authored-by: Andrei Ioniță --- .../Controllers/OrganisationController.php | 38 +++++++++++++++++++ app/Http/Controllers/ResourceController.php | 28 ++++++++++++++ app/Http/Resources/IdAndNameResource.php | 22 +++++++++++ app/Http/Resources/OrganisationResource.php | 35 +++++++++++++++++ app/Http/Resources/ResourceResource.php | 31 +++++++++++++++ config/filament-breezy.php | 2 +- routes/api.php | 8 ++++ 7 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 app/Http/Controllers/OrganisationController.php create mode 100644 app/Http/Controllers/ResourceController.php create mode 100644 app/Http/Resources/IdAndNameResource.php create mode 100644 app/Http/Resources/OrganisationResource.php create mode 100644 app/Http/Resources/ResourceResource.php diff --git a/app/Http/Controllers/OrganisationController.php b/app/Http/Controllers/OrganisationController.php new file mode 100644 index 0000000..a4086bf --- /dev/null +++ b/app/Http/Controllers/OrganisationController.php @@ -0,0 +1,38 @@ +withoutEagerLoads(['city']) + ->select([ + 'id', + 'name', + 'county_id', + 'type', + 'status', + 'area', + 'created_at', + 'updated_at', + ]) + ->with([ + 'riskCategories', + 'activityCounties', + 'expertises', + 'resourceTypes', + 'county', + ]) + ->withCount('volunteers') + ->get() + ); + } +} diff --git a/app/Http/Controllers/ResourceController.php b/app/Http/Controllers/ResourceController.php new file mode 100644 index 0000000..e236558 --- /dev/null +++ b/app/Http/Controllers/ResourceController.php @@ -0,0 +1,28 @@ +with([ + 'county', + 'category', + 'subcategory', + 'types', + 'organisation' => fn ($query) => $query + ->withoutEagerLoads() + ->select('id', 'name'), + ]) + ->get() + ); + } +} diff --git a/app/Http/Resources/IdAndNameResource.php b/app/Http/Resources/IdAndNameResource.php new file mode 100644 index 0000000..cc80aa9 --- /dev/null +++ b/app/Http/Resources/IdAndNameResource.php @@ -0,0 +1,22 @@ + + */ + public function toArray(Request $request): array + { + return [ + 'id' => $this->id, + 'name' => $this->name + ]; + } +} diff --git a/app/Http/Resources/OrganisationResource.php b/app/Http/Resources/OrganisationResource.php new file mode 100644 index 0000000..aad9df3 --- /dev/null +++ b/app/Http/Resources/OrganisationResource.php @@ -0,0 +1,35 @@ + + */ + public function toArray(Request $request): array + { + return [ + 'id' => $this->id, + 'name' => $this->name, + 'type' => $this->type, + 'status' => $this->status, + 'expertises' => IdAndNameResource::collection($this->expertises), + 'risk_categories' => IdAndNameResource::collection($this->riskCategories), + 'resource_types' => IdAndNameResource::collection($this->resourceTypes), + 'area' => $this->area, + 'county' => $this->county->name, + 'activity_counties' => IdAndNameResource::collection($this->activityCounties), + 'created_at' => $this->created_at->format('Y-m-d H:i:s'), + 'updated_at' => $this->updated_at->format('Y-m-d H:i:s'), + 'volunteers_count' => $this->volunteers_count, + ]; + } +} diff --git a/app/Http/Resources/ResourceResource.php b/app/Http/Resources/ResourceResource.php new file mode 100644 index 0000000..521d509 --- /dev/null +++ b/app/Http/Resources/ResourceResource.php @@ -0,0 +1,31 @@ + + */ + public function toArray(Request $request): array + { + return [ + 'id' => $this->id, + 'name' => $this->name, + 'county' => $this->county->name, + 'organisation' => IdAndNameResource::make($this->organisation), + 'category' => IdAndNameResource::make($this->category), + 'subcategory' => IdAndNameResource::make($this->subcategory), + 'types' => IdAndNameResource::collection($this->types), + 'created_at' => $this->created_at->format('Y-m-d H:i:s'), + 'updated_at' => $this->updated_at->format('Y-m-d H:i:s'), + ]; + } +} diff --git a/config/filament-breezy.php b/config/filament-breezy.php index 382bf0d..2189153 100644 --- a/config/filament-breezy.php +++ b/config/filament-breezy.php @@ -112,7 +112,7 @@ |-------------------------------------------------------------------------- | Enable sanctum api token management. */ - 'enable_sanctum' => false, + 'enable_sanctum' => true, /* |-------------------------------------------------------------------------- | Sanctum permissions diff --git a/routes/api.php b/routes/api.php index 21e4de1..ba4fea6 100644 --- a/routes/api.php +++ b/routes/api.php @@ -2,6 +2,8 @@ declare(strict_types=1); +use App\Http\Controllers\OrganisationController; +use App\Http\Controllers\ResourceController; use Illuminate\Http\Request; use Illuminate\Support\Facades\Route; @@ -15,6 +17,12 @@ | is assigned the "api" middleware group. Enjoy building your API! | */ +Route::middleware('auth:sanctum') + ->prefix('/v1') + ->group(function () { + Route::get('/organisations', OrganisationController::class); + Route::get('/resources', ResourceController::class); + }); Route::middleware('auth:sanctum')->get('/user', function (Request $request) { return $request->user();