Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Api DSU #152

Merged
merged 4 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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