Skip to content

Commit

Permalink
Api DSU
Browse files Browse the repository at this point in the history
  • Loading branch information
alexPopaCode4 committed Feb 27, 2024
1 parent a2831ce commit ae29f10
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 1 deletion.
22 changes: 22 additions & 0 deletions app/Http/Controllers/OrganizationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Http\Controllers;

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

class OrganizationController extends Controller
{
public function __invoke()
{
return OrganizationResource::collection(
Organisation::query()
->with('riskCategories')
->with('activityCounties')
->with('expertises')
->with('resourceTypes')
->with('volunteers')
->get()
);
}
}
23 changes: 23 additions & 0 deletions app/Http/Controllers/ResourceController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

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')
->with('organisation')
->with('category')
->with('subcategory')
->with('types')
->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
];
}
}
31 changes: 31 additions & 0 deletions app/Http/Resources/OrganizationResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Http\Resources;

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

class OrganizationResource 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_area' => IdAndNameResource::collection($this->expertises),
'risc_category' => IdAndNameResource::collection($this->riskCategories),
'action_type' => IdAndNameResource::collection($this->resourceTypes),
'activity_area' => $this->area,
'county' => 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(),
];
}
}
29 changes: 29 additions & 0 deletions app/Http/Resources/ResourceResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

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' => IdAndNameResource::make($this->county),
'organization' => 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\OrganizationController;
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('/organizations', OrganizationController::class);
Route::get('/resources', ResourceController::class);
});

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
Expand Down

0 comments on commit ae29f10

Please sign in to comment.