build: initialize RetroHub with Laravel, Inertia, Vue, and Horizon
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Games\Controllers;
|
||||
|
||||
use App\Features\Consoles\Models\Console;
|
||||
use App\Features\Games\Data\GameData;
|
||||
use App\Features\Games\Repositories\GameRepository;
|
||||
use App\Features\Games\Repositories\RomFileRepository;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
|
||||
class GameController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private GameRepository $gameRepository,
|
||||
private RomFileRepository $romFileRepository
|
||||
) {}
|
||||
|
||||
public function show(string $slug): Response
|
||||
{
|
||||
$game = $this->gameRepository->getBySlug($slug);
|
||||
abort_unless($game, 404);
|
||||
|
||||
return Inertia::render('Games/Show', [
|
||||
'game' => GameData::fromModel($game),
|
||||
]);
|
||||
}
|
||||
|
||||
public function edit(string $slug): Response
|
||||
{
|
||||
$game = $this->gameRepository->getBySlug($slug);
|
||||
abort_unless($game, 404);
|
||||
|
||||
return Inertia::render('Games/Edit', [
|
||||
'game' => GameData::fromModel($game),
|
||||
'consoles' => Console::orderBy('name')->get(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(Request $request, string $slug): RedirectResponse
|
||||
{
|
||||
$game = $this->gameRepository->getBySlug($slug);
|
||||
abort_unless($game, 404);
|
||||
|
||||
$validated = $request->validate([
|
||||
'title' => 'required|string|max:255',
|
||||
'description' => 'nullable|string',
|
||||
'release_year' => 'nullable|integer|min:1970|max:2030',
|
||||
'developer' => 'nullable|string|max:255',
|
||||
'publisher' => 'nullable|string|max:255',
|
||||
'genre' => 'nullable|string|max:255',
|
||||
'rating' => 'nullable|numeric|min:0|max:10',
|
||||
]);
|
||||
|
||||
$game->update($validated);
|
||||
|
||||
return redirect()->route('games.show', $game)
|
||||
->with('success', 'Game updated successfully.');
|
||||
}
|
||||
|
||||
public function destroy(string $slug): RedirectResponse
|
||||
{
|
||||
$game = $this->gameRepository->getBySlug($slug);
|
||||
abort_unless($game, 404);
|
||||
|
||||
$romFiles = $this->romFileRepository->getByGame($game->id);
|
||||
foreach ($romFiles as $romFile) {
|
||||
Storage::disk('local')->delete($romFile->file_path);
|
||||
$romFile->delete();
|
||||
}
|
||||
|
||||
if ($game->cover_path) {
|
||||
Storage::disk('local')->delete($game->cover_path);
|
||||
}
|
||||
|
||||
$console = $game->console;
|
||||
$game->delete();
|
||||
|
||||
return redirect()->route('consoles.show', $console->slug)
|
||||
->with('success', 'Game deleted successfully.');
|
||||
}
|
||||
|
||||
public function download(string $slug, int $romFileId): StreamedResponse
|
||||
{
|
||||
$game = $this->gameRepository->getBySlug($slug);
|
||||
abort_unless($game, 404);
|
||||
|
||||
$romFile = $this->romFileRepository->getByGame($game->id)->where('id', $romFileId)->first();
|
||||
abort_unless($romFile, 404);
|
||||
|
||||
return Storage::disk('local')->download(
|
||||
$romFile->file_path,
|
||||
$romFile->file_name
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Games\Data;
|
||||
|
||||
use Spatie\LaravelData\Attributes\MapInputName;
|
||||
use Spatie\LaravelData\Data;
|
||||
use Spatie\LaravelData\Mappers\SnakeCaseMapper;
|
||||
|
||||
#[MapInputName(SnakeCaseMapper::class)]
|
||||
class DashboardStatsData extends Data
|
||||
{
|
||||
public function __construct(
|
||||
public int $totalGames,
|
||||
public int $totalConsoles,
|
||||
public int $totalRoms,
|
||||
public int $userSaves,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Games\Data;
|
||||
|
||||
use App\Features\Consoles\Data\ConsoleData;
|
||||
use App\Features\Games\Models\Game;
|
||||
use Spatie\LaravelData\Attributes\MapInputName;
|
||||
use Spatie\LaravelData\Data;
|
||||
use Spatie\LaravelData\Mappers\SnakeCaseMapper;
|
||||
|
||||
#[MapInputName(SnakeCaseMapper::class)]
|
||||
class GameData extends Data
|
||||
{
|
||||
public function __construct(
|
||||
public int $id,
|
||||
public int $consoleId,
|
||||
public string $title,
|
||||
public string $slug,
|
||||
public ?int $igdbId,
|
||||
public ?int $tgdbId,
|
||||
public ?string $description,
|
||||
public ?int $releaseYear,
|
||||
public ?string $developer,
|
||||
public ?string $publisher,
|
||||
public ?string $genre,
|
||||
public ?float $rating,
|
||||
public ?string $coverPath,
|
||||
public ?string $thumbnailPath,
|
||||
public ?ConsoleData $console,
|
||||
public ?array $romFiles,
|
||||
) {}
|
||||
|
||||
public static function fromModel(Game $game): self
|
||||
{
|
||||
return new self(
|
||||
id: $game->id,
|
||||
consoleId: $game->console_id,
|
||||
title: $game->title,
|
||||
slug: $game->slug,
|
||||
igdbId: $game->igdb_id,
|
||||
tgdbId: $game->tgdb_id,
|
||||
description: $game->description,
|
||||
releaseYear: $game->release_year,
|
||||
developer: $game->developer,
|
||||
publisher: $game->publisher,
|
||||
genre: $game->genre,
|
||||
rating: $game->rating ? (float) $game->rating : null,
|
||||
coverPath: $game->cover_path,
|
||||
thumbnailPath: $game->thumbnail_path,
|
||||
console: $game->relationLoaded('console') && $game->console
|
||||
? ConsoleData::fromModel($game->console)
|
||||
: null,
|
||||
romFiles: $game->relationLoaded('romFiles')
|
||||
? $game->romFiles->map(fn ($rom) => RomFileData::fromModel($rom))->toArray()
|
||||
: null,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Games\Data;
|
||||
|
||||
use App\Features\Games\Models\RomFile;
|
||||
use Spatie\LaravelData\Attributes\MapInputName;
|
||||
use Spatie\LaravelData\Data;
|
||||
use Spatie\LaravelData\Mappers\SnakeCaseMapper;
|
||||
|
||||
#[MapInputName(SnakeCaseMapper::class)]
|
||||
class RomFileData extends Data
|
||||
{
|
||||
public function __construct(
|
||||
public int $id,
|
||||
public int $gameId,
|
||||
public string $filePath,
|
||||
public string $fileName,
|
||||
public int $fileSize,
|
||||
public string $fileHash,
|
||||
public ?string $region,
|
||||
public ?string $language,
|
||||
) {}
|
||||
|
||||
public static function fromModel(RomFile $romFile): self
|
||||
{
|
||||
return new self(
|
||||
id: $romFile->id,
|
||||
gameId: $romFile->game_id,
|
||||
filePath: $romFile->file_path,
|
||||
fileName: $romFile->file_name,
|
||||
fileSize: $romFile->file_size,
|
||||
fileHash: $romFile->file_hash,
|
||||
region: $romFile->region,
|
||||
language: $romFile->language,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Games\Models;
|
||||
|
||||
use App\Features\Consoles\Models\Console;
|
||||
use App\Features\Settings\Models\SaveFile;
|
||||
use Database\Factories\GameFactory;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class Game extends Model
|
||||
{
|
||||
/** @use HasFactory<GameFactory> */
|
||||
use HasFactory;
|
||||
|
||||
protected static ?string $factory = GameFactory::class;
|
||||
|
||||
protected $fillable = [
|
||||
'console_id',
|
||||
'title',
|
||||
'slug',
|
||||
'igdb_id',
|
||||
'tgdb_id',
|
||||
'description',
|
||||
'release_year',
|
||||
'developer',
|
||||
'publisher',
|
||||
'genre',
|
||||
'rating',
|
||||
'cover_path',
|
||||
'thumbnail_path',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'rating' => 'decimal:1',
|
||||
];
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::creating(function (Game $game) {
|
||||
if (empty($game->slug)) {
|
||||
$game->slug = Str::slug($game->title);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function console(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Console::class);
|
||||
}
|
||||
|
||||
public function romFiles(): HasMany
|
||||
{
|
||||
return $this->hasMany(RomFile::class);
|
||||
}
|
||||
|
||||
public function saveFiles(): HasMany
|
||||
{
|
||||
return $this->hasMany(SaveFile::class);
|
||||
}
|
||||
|
||||
public function getRouteKeyName(): string
|
||||
{
|
||||
return 'slug';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Games\Models;
|
||||
|
||||
use Database\Factories\RomFileFactory;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class RomFile extends Model
|
||||
{
|
||||
/** @use HasFactory<RomFileFactory> */
|
||||
use HasFactory;
|
||||
|
||||
protected static ?string $factory = RomFileFactory::class;
|
||||
|
||||
protected $fillable = [
|
||||
'game_id',
|
||||
'file_path',
|
||||
'file_name',
|
||||
'file_size',
|
||||
'file_hash',
|
||||
'region',
|
||||
'language',
|
||||
];
|
||||
|
||||
public function game(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Game::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Features\Games\Repositories;
|
||||
|
||||
use App\Features\Games\Models\RomFile;
|
||||
|
||||
class RomFileRepository
|
||||
{
|
||||
public function __construct(
|
||||
private RomFile $model
|
||||
) {}
|
||||
|
||||
public function count(): int
|
||||
{
|
||||
return $this->model->count();
|
||||
}
|
||||
|
||||
public function findByHash(string $hash): ?RomFile
|
||||
{
|
||||
return $this->model->where('file_hash', $hash)->first();
|
||||
}
|
||||
|
||||
public function create(array $data): RomFile
|
||||
{
|
||||
return $this->model->create($data);
|
||||
}
|
||||
|
||||
public function getByGame(int $gameId)
|
||||
{
|
||||
return $this->model->where('game_id', $gameId)->get();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user