Skip to content

Commit

Permalink
feat: Allow to filter out hardpoints
Browse files Browse the repository at this point in the history
Removes / Includes hardpoints based on item types

Examples:
- Remove Doors: `?filter[hardpoints]=!Doors`
- Remove Doors and Seats: `?filter[hardpoints]=!Door,!Seat`
  • Loading branch information
octfx committed May 12, 2024
1 parent 2070837 commit 5b523a7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
5 changes: 5 additions & 0 deletions app/Http/Controllers/Api/V2/SC/Vehicle/VehicleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
39 changes: 38 additions & 1 deletion app/Http/Resources/SC/Vehicle/VehicleResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down
11 changes: 4 additions & 7 deletions app/Http/Resources/StarCitizen/Vehicle/VehicleResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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,
Expand Down

0 comments on commit 5b523a7

Please sign in to comment.