From 5b523a77f43a4da4e1feaac26a27229a17bcde37 Mon Sep 17 00:00:00 2001 From: "H. C. Kruse" Date: Sun, 12 May 2024 12:45:59 +0200 Subject: [PATCH] feat: Allow to filter out hardpoints Removes / Includes hardpoints based on item types Examples: - Remove Doors: `?filter[hardpoints]=!Doors` - Remove Doors and Seats: `?filter[hardpoints]=!Door,!Seat` --- .../Api/V2/SC/Vehicle/VehicleController.php | 5 +++ .../Resources/SC/Vehicle/VehicleResource.php | 39 ++++++++++++++++++- .../StarCitizen/Vehicle/VehicleResource.php | 11 ++---- 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/Api/V2/SC/Vehicle/VehicleController.php b/app/Http/Controllers/Api/V2/SC/Vehicle/VehicleController.php index 5dbf670d..6f222518 100644 --- a/app/Http/Controllers/Api/V2/SC/Vehicle/VehicleController.php +++ b/app/Http/Controllers/Api/V2/SC/Vehicle/VehicleController.php @@ -66,6 +66,11 @@ public function index(Request $request): AnonymousResourceCollection tags: ['Vehicles', 'RSI-Website', 'In-Game'], parameters: [ new OA\Parameter(ref: '#/components/parameters/locale'), + new OA\Parameter( + name: 'filter[hardpoint]', + description: 'Filter hardpoint types, prefix with "!" to remove these hardpoints.', + in: 'query', schema: new OA\Schema(type: 'string') + ), new OA\Parameter( name: 'include', in: 'query', diff --git a/app/Http/Resources/SC/Vehicle/VehicleResource.php b/app/Http/Resources/SC/Vehicle/VehicleResource.php index 603830bb..e13ffeb1 100644 --- a/app/Http/Resources/SC/Vehicle/VehicleResource.php +++ b/app/Http/Resources/SC/Vehicle/VehicleResource.php @@ -9,6 +9,8 @@ use App\Http\Resources\SC\Shop\ShopResource; use App\Http\Resources\TranslationResourceFactory; use App\Models\System\Language; +use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Http\Request; use Illuminate\Support\Str; use OpenApi\Attributes as OA; @@ -269,6 +271,41 @@ public function toArray(Request $request): array ->map('strtolower') ->toArray(); + $hardpoints = []; + + if (in_array('hardpoints', $includes, true)) { + if ($request->has('filter') && isset($request->get('filter')['hardpoints'])) { + /** @var HasMany $hardpoints */ + $hardpoints = $this->hardpointsWithoutParent(); + + $filters = collect(explode(',', $request->get('filter')['hardpoints'])) + ->map(fn (string $filter) => trim($filter)); + + $remove = $filters + ->filter(fn (string $filter) => str_starts_with($filter, '!')) + ->map(fn (string $filter) => ltrim($filter, '!')) + ->toArray(); + + $include = $filters + ->filter(fn (string $filter) => ! str_starts_with($filter, '!')) + ->toArray(); + + if (! empty($remove)) { + $hardpoints->whereRelation('item', function (Builder $query) use ($remove) { + $query->whereNotIn('type', $remove); + }); + } elseif (! empty($include)) { + $hardpoints->whereRelation('item', function (Builder $query) use ($include) { + $query->whereIn('type', $include); + }); + } + + $hardpoints = $hardpoints->get(); + } else { + $hardpoints = $this->hardpointsWithoutParent; + } + } + $manufacturer = $this->item->manufacturer->name; if ($manufacturer === 'Unknown Manufacturer') { $manufacturer = $this->description_manufacturer; @@ -371,7 +408,7 @@ public function toArray(Request $request): array 'expedite_cost' => $this->expedite_cost, ], $this->mergeWhen(in_array('hardpoints', $includes, true), [ - 'hardpoints' => HardpointResource::collection($this->hardpointsWithoutParent), + 'hardpoints' => HardpointResource::collection($hardpoints), ]), $this->mergeWhen(in_array('shops', $includes, true), [ 'shops' => ShopResource::collection($this->item->shops), diff --git a/app/Http/Resources/StarCitizen/Vehicle/VehicleResource.php b/app/Http/Resources/StarCitizen/Vehicle/VehicleResource.php index bd21afd8..174580f1 100644 --- a/app/Http/Resources/StarCitizen/Vehicle/VehicleResource.php +++ b/app/Http/Resources/StarCitizen/Vehicle/VehicleResource.php @@ -134,11 +134,8 @@ public static function validIncludes(): array /** * Transform the resource into an array. - * - * @param Request $request - * @return array */ - public function toArray($request): array + public function toArray(Request $request): array { $includes = collect(explode(',', $request->get('include', ''))) ->map('trim') @@ -151,9 +148,9 @@ public function toArray($request): array 'name' => $this->name, 'slug' => $this->slug, 'sizes' => [ - 'length' => (double)$this->length, - 'beam' => (double)$this->width, - 'height' => (double)$this->height, + 'length' => (float) $this->length, + 'beam' => (float) $this->width, + 'height' => (float) $this->height, ], 'mass' => $this->mass, 'cargo_capacity' => $this->cargo_capacity,