102 lines
3.1 KiB
PHP
102 lines
3.1 KiB
PHP
<?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
|
|
);
|
|
}
|
|
}
|