Skip to content

Commit

Permalink
refactor: Scope items to current game version
Browse files Browse the repository at this point in the history
Fixes #98
  • Loading branch information
octfx committed Dec 31, 2023
1 parent cd422eb commit f286ea5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 25 deletions.
10 changes: 5 additions & 5 deletions app/Http/Controllers/Api/V2/AbstractApiV2Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,13 @@ protected function processRequestParams(): void
*/
private function processLimit(): void
{
if ($this->request->has(self::LIMIT) && null !== $this->request->get(self::LIMIT)) {
$itemLimit = (int)$this->request->get(self::LIMIT);
if ($this->request->input(self::LIMIT) !== null) {
$itemLimit = (int) $this->request->get(self::LIMIT);

if ($itemLimit > 0) {
if ($itemLimit > 500) {
$this->limit = 500;
} elseif ($itemLimit >= 0) {
$this->limit = $itemLimit;
} elseif (0 === $itemLimit) {
$this->limit = 0;
}
}
}
Expand Down
8 changes: 0 additions & 8 deletions app/Http/Controllers/Api/V2/SC/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ class ItemController extends AbstractApiV2Controller
public function index(Request $request): AnonymousResourceCollection
{
$query = QueryBuilder::for(Item::class, $request)
->where(
'version',
'LIKE',
$request->get('version', config('api.sc_data_version')) . '%'
)
->allowedFilters([
'type',
'sub_type',
Expand Down Expand Up @@ -106,13 +101,11 @@ public function show(Request $request)

try {
$item = QueryBuilder::for(Item::class, $request)
->where('version', 'LIKE', $request->get('version', config('api.sc_data_version')) . '%')
->where('class_name', 'NOT LIKE', '%test_%')
->where(function (Builder $query) use ($identifier) {
$query->where('uuid', $identifier)
->orWhere('name', $identifier);
})
->orderByDesc('version')
->allowedIncludes(ItemResource::validIncludes())
->with([
'dimensions',
Expand Down Expand Up @@ -200,7 +193,6 @@ public function search(ItemSearchRequest $request): JsonResource
}

$items = $items
->where('version', 'LIKE', $request->get('version', config('api.sc_data_version')) . '%')
->where(function (Builder $query) use ($toSearch) {
$query->where('name', 'like', "%{$toSearch}%")
->orWhere('uuid', $toSearch)
Expand Down
12 changes: 0 additions & 12 deletions app/Http/Controllers/Api/V2/SC/Vehicle/VehicleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,6 @@ public function show(Request $request): AbstractBaseResource

try {
$vehicleModel = QueryBuilder::for(UnpackedVehicle::class)
->whereRelation(
'item',
'version',
'LIKE',
$request->get('version', config('api.sc_data_version')) . '%'
)
->where(function (Builder $query) use ($identifier, $underscored) {
$query->where('name', $identifier)
->orWhere('class_name', $underscored)
Expand Down Expand Up @@ -211,12 +205,6 @@ public function search(Request $request): AnonymousResourceCollection
$underscored = str_replace(' ', '_', $identifier);

$queryBuilder = QueryBuilder::for(UnpackedVehicle::class)
->whereRelation(
'item',
'version',
'LIKE',
$request->get('version', config('api.sc_data_version')) . '%'
)
->where(function (Builder $query) use ($identifier, $underscored) {
$query->Where('class_name', 'LIKE', "%{$underscored}")
->orWhere('class_name', $identifier)
Expand Down
13 changes: 13 additions & 0 deletions app/Models/SC/Item/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use App\Models\System\Translation\AbstractHasTranslations as HasTranslations;
use App\Traits\HasDescriptionDataTrait;
use App\Traits\HasModelChangelogTrait as ModelChangelog;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
Expand Down Expand Up @@ -87,6 +88,18 @@ public function getRouteKey()
'requiredTags',
];

protected static function boot(): void
{
parent::boot();

static::addGlobalScope(
'version',
static function (Builder $builder) {
$builder->where('version', config('api.sc_data_version'));
}
);
}

public function translations(): HasMany
{
return $this->hasMany(
Expand Down

0 comments on commit f286ea5

Please sign in to comment.