73 lines
1.6 KiB
PHP
73 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Features\Games\Repositories;
|
|
|
|
use App\Features\Games\Models\Game;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
class GameRepository
|
|
{
|
|
public function __construct(
|
|
private Game $model
|
|
) {}
|
|
|
|
public function getByConsole(int $consoleId, int $perPage = 24): LengthAwarePaginator
|
|
{
|
|
return $this->model
|
|
->where('console_id', $consoleId)
|
|
->with('romFiles')
|
|
->orderBy('title')
|
|
->paginate($perPage);
|
|
}
|
|
|
|
public function getBySlug(string $slug): ?Game
|
|
{
|
|
return $this->model
|
|
->with(['console', 'romFiles'])
|
|
->where('slug', $slug)
|
|
->first();
|
|
}
|
|
|
|
public function getRecent(int $limit = 10): Collection
|
|
{
|
|
return $this->model
|
|
->with('console')
|
|
->latest()
|
|
->take($limit)
|
|
->get();
|
|
}
|
|
|
|
public function search(string $query): Collection
|
|
{
|
|
return $this->model
|
|
->with('console')
|
|
->where('title', 'like', "%{$query}%")
|
|
->orderBy('title')
|
|
->get();
|
|
}
|
|
|
|
public function getAll()
|
|
{
|
|
return $this->model
|
|
->with('console')
|
|
->orderBy('title')
|
|
->paginate(24);
|
|
}
|
|
|
|
public function create(array $data): Game
|
|
{
|
|
return $this->model->create($data);
|
|
}
|
|
|
|
public function firstOrCreate(array $conditions, array $values = []): Game
|
|
{
|
|
return $this->model->firstOrCreate($conditions, $values);
|
|
}
|
|
|
|
public function count(): int
|
|
{
|
|
return $this->model->count();
|
|
}
|
|
}
|