Skip to content

Commit

Permalink
Api DSU (#152)
Browse files Browse the repository at this point in the history
* Api DSU

* Apply suggestions from code review

* Fix code review

* rename organization in organisation

---------

Co-authored-by: Andrei Ioniță <[email protected]>
  • Loading branch information
alexPopaCode4 and andreiio authored Feb 28, 2024
1 parent a2831ce commit d4628a5
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 1 deletion.
38 changes: 38 additions & 0 deletions app/Http/Controllers/OrganisationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers;

use App\Http\Resources\OrganisationResource;
use App\Models\Organisation;

class OrganisationController extends Controller
{
public function __invoke()
{
return OrganisationResource::collection(
Organisation::query()
->withoutEagerLoads(['city'])
->select([
'id',
'name',
'county_id',
'type',
'status',
'area',
'created_at',
'updated_at',
])
->with([
'riskCategories',
'activityCounties',
'expertises',
'resourceTypes',
'county',
])
->withCount('volunteers')
->get()
);
}
}
28 changes: 28 additions & 0 deletions app/Http/Controllers/ResourceController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers;

use App\Http\Resources\ResourceResource;
use App\Models\Resource;

class ResourceController extends Controller
{
public function __invoke()
{
return ResourceResource::collection(
Resource::query()
->with([
'county',
'category',
'subcategory',
'types',
'organisation' => fn ($query) => $query
->withoutEagerLoads()
->select('id', 'name'),
])
->get()
);
}
}
22 changes: 22 additions & 0 deletions app/Http/Resources/IdAndNameResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class IdAndNameResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'name' => $this->name
];
}
}
35 changes: 35 additions & 0 deletions app/Http/Resources/OrganisationResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class OrganisationResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
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,
];
}
}
31 changes: 31 additions & 0 deletions app/Http/Resources/ResourceResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class ResourceResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
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'),
];
}
}
2 changes: 1 addition & 1 deletion config/filament-breezy.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
|--------------------------------------------------------------------------
| Enable sanctum api token management.
*/
'enable_sanctum' => false,
'enable_sanctum' => true,
/*
|--------------------------------------------------------------------------
| Sanctum permissions
Expand Down
8 changes: 8 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand Down

0 comments on commit d4628a5

Please sign in to comment.