Add public API v1 with API key auth, rate limiting, and docs page
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
68d01448d2
commit
a583a13da1
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class UserApiController extends Controller
|
||||
{
|
||||
public function show(User $user): JsonResponse
|
||||
{
|
||||
$library = $user->library();
|
||||
|
||||
return response()->json([
|
||||
'data' => [
|
||||
'username' => $user->slug,
|
||||
'name' => $user->name,
|
||||
'avatar_url' => $user->avatar,
|
||||
'joined_at' => $user->created_at->toIso8601String(),
|
||||
'stats' => [
|
||||
'played' => (clone $library)->where('status', 'played')->count(),
|
||||
'playing' => (clone $library)->where('status', 'playing')->count(),
|
||||
'want_to_play' => (clone $library)->where('status', 'want_to_play')->count(),
|
||||
'total' => $user->library()->count(),
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function library(User $user, Request $request): JsonResponse
|
||||
{
|
||||
$paginator = $user->library()
|
||||
->with('game')
|
||||
->orderByDesc('updated_at')
|
||||
->paginate(20);
|
||||
|
||||
return response()->json([
|
||||
'data' => $paginator->map(fn ($ug) => [
|
||||
'game' => [
|
||||
'title' => $ug->game->title,
|
||||
'slug' => $ug->game->slug,
|
||||
'cover_url' => $ug->game->cover_url,
|
||||
],
|
||||
'status' => $ug->status,
|
||||
'rating' => $ug->rating,
|
||||
'updated_at' => $ug->updated_at->toIso8601String(),
|
||||
])->values(),
|
||||
'meta' => [
|
||||
'current_page' => $paginator->currentPage(),
|
||||
'last_page' => $paginator->lastPage(),
|
||||
'per_page' => $paginator->perPage(),
|
||||
'total' => $paginator->total(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user