Skip to content

Commit

Permalink
feat: Add mission givers endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
octfx committed May 3, 2024
1 parent 7cab785 commit c5c2736
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 0 deletions.
91 changes: 91 additions & 0 deletions app/Http/Controllers/Api/V2/SC/MissionGiverController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace App\Http\Controllers\Api\V2\SC;

use App\Http\Controllers\Api\V2\AbstractApiV2Controller;
use App\Http\Resources\SC\Mission\GiverLinkResource;
use App\Http\Resources\SC\Mission\GiverResource;
use App\Models\SC\Mission\Giver;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Validator;
use OpenApi\Attributes as OA;
use Spatie\QueryBuilder\QueryBuilder;

class MissionGiverController extends AbstractApiV2Controller
{
#[OA\Get(
path: '/api/v2/missions-givers',
tags: ['In-Game', 'Missions'],
parameters: [
new OA\Parameter(ref: '#/components/parameters/page'),
new OA\Parameter(ref: '#/components/parameters/limit'),
],
responses: [
new OA\Response(
response: 200,
description: 'List of Mission Givers',
content: new OA\JsonContent(
type: 'array',
items: new OA\Items(ref: '#/components/schemas/mission_giver_link_v2')
)
),
]
)]
public function index(Request $request): AnonymousResourceCollection
{
$query = QueryBuilder::for(Giver::class, $request)
->paginate($this->limit)
->appends(request()->query());

return GiverLinkResource::collection($query);
}

#[OA\Get(
path: '/api/v2/mission-givers/{giver}',
tags: ['In-Game', 'Missions'],
parameters: [
new OA\Parameter(ref: '#/components/parameters/locale'),
new OA\Parameter(
name: 'giver',
in: 'path',
required: true,
schema: new OA\Schema(
description: 'Mission Giver UUID',
type: 'string',
),
),
],
responses: [
new OA\Response(
response: 200,
description: 'A Mission Giver',
content: new OA\JsonContent(
type: 'array',
items: new OA\Items(ref: '#/components/schemas/mission_giver_v2')
)
),
]
)]
public function show(Request $request): JsonResource
{
['giver' => $identifier] = Validator::validate(
[
'giver' => $request->giver,
],
[
'giver' => 'required|uuid',
]
);

$model = QueryBuilder::for(Giver::class, $request)
->where('uuid', $identifier)
->with([
'missions',
])
->firstOrFail();

return new GiverResource($model);
}
}
2 changes: 2 additions & 0 deletions app/Http/Resources/AbstractBaseResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ abstract class AbstractBaseResource extends JsonResource

public const MISSIONS_SHOW = 'missions/';

public const MISSION_GIVERS_SHOW = 'mission-givers/';

public const FACTIONS_SHOW = 'factions/';

public function __construct($resource)
Expand Down
34 changes: 34 additions & 0 deletions app/Http/Resources/SC/Mission/GiverLinkResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Http\Resources\SC\Mission;

use App\Http\Resources\AbstractTranslationResource;
use Illuminate\Http\Request;
use OpenApi\Attributes as OA;

#[OA\Schema(
schema: 'mission_giver_link_v2',
title: 'Mission Giver Link',
properties: [
new OA\Property(property: 'uuid', type: 'string', nullable: true),
new OA\Property(property: 'name', type: 'string', nullable: true),
new OA\Property(property: 'link', type: 'string', nullable: true),
],
type: 'object'
)]
class GiverLinkResource extends AbstractTranslationResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'uuid' => $this->uuid,
'name' => $this->name,
'link' => $this->makeApiUrl(self::MISSION_GIVERS_SHOW, $this->uuid),
];
}
}
8 changes: 8 additions & 0 deletions app/Http/Resources/SC/Mission/GiverResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
)]
class GiverResource extends AbstractTranslationResource
{
public static function validIncludes(): array
{
return [
'missions',
];
}

/**
* Transform the resource into an array.
*
Expand All @@ -50,6 +57,7 @@ public function toArray(Request $request): array
'medium_cooldown' => $this->medium_cooldown,
'long_cooldown' => $this->long_cooldown,
'description' => $this->getTranslation($this, $request),
'missions' => $this->whenLoaded('missions', fn () => MissionLinkResource::collection($this->missions)),
];
}
}
5 changes: 5 additions & 0 deletions app/Models/SC/Mission/Giver.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ public function translations(): HasMany
'uuid',
);
}

public function missions(): HasMany
{
return $this->hasMany(Mission::class, 'giver_id', 'id');
}
}
4 changes: 4 additions & 0 deletions routes/api/api_v2.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ static function () {
Route::get('missions/{mission}', 'MissionController@show')->name('missions.show')
->where('mission', '.*');

Route::get('mission-givers', 'MissionGiverController@index')->name('mission-givers.index');
Route::get('mission-givers/{giver}', 'MissionGiverController@show')->name('mission-givers.show')
->where('giver', '.*');

Route::get('factions', 'FactionController@index')->name('faction.index');
Route::get('factions/{faction}', 'FactionController@show')->name('faction.show')
->where('faction', '.*');
Expand Down

0 comments on commit c5c2736

Please sign in to comment.