91 lines
2.2 KiB
PHP
91 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Game;
|
|
use Illuminate\Http\Request;
|
|
use App\Http\Requests\StoreGameRequest;
|
|
|
|
class GameController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$query = Game::query()->with(['winner']);
|
|
|
|
if ($request->has('type') && in_array($request->type, ['3', '9'])) {
|
|
$query->where('type', $request->type);
|
|
}
|
|
|
|
if ($request->has('status') && in_array($request->status, ['Pending', 'Playing', 'Ended', 'Interrupted'])) {
|
|
$query->where('status', $request->status);
|
|
}
|
|
|
|
// Sorting
|
|
$sortField = $request->input('sort_by', 'began_at');
|
|
$sortDirection = $request->input('sort_direction', 'desc');
|
|
|
|
$allowedSortFields = [
|
|
'began_at',
|
|
'ended_at',
|
|
'total_time',
|
|
'type',
|
|
'status'
|
|
];
|
|
|
|
if (in_array($sortField, $allowedSortFields)) {
|
|
$query->orderBy($sortField, $sortDirection === 'asc' ? 'asc' : 'desc');
|
|
}
|
|
|
|
// Pagination
|
|
$perPage = $request->input('per_page', 15);
|
|
$games = $query->paginate($perPage);
|
|
|
|
return response()->json([
|
|
'data' => $games->items(),
|
|
'meta' => [
|
|
'current_page' => $games->currentPage(),
|
|
'last_page' => $games->lastPage(),
|
|
'per_page' => $games->perPage(),
|
|
'total' => $games->total()
|
|
]
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(StoreGameRequest $request)
|
|
{
|
|
$game = Game::create($request->validated());
|
|
return response()->json($game, 201);
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(Game $game)
|
|
{
|
|
return $game;
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, Game $game)
|
|
{
|
|
$game->update($request->validated());
|
|
return response()->json($game);
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(Game $game)
|
|
{
|
|
//
|
|
}
|
|
}
|