feat: add paginatin to music

This commit is contained in:
2026-06-12 15:25:12 +03:30
parent 934a3884ad
commit 3a2ed286b6
+15 -9
View File
@@ -32,23 +32,29 @@ public function getAllMusic()
return response()->json($music);
}
public function index()
public function index(Request $request)
{
$userId = auth()->id();
// Paginated so clients don't pull the whole library at once.
$perPage = (int) $request->input('per_page', 20);
$perPage = max(1, min($perPage, 100));
// (public) OR (private AND owned by me) — grouped so it stays correct.
$music = Music::with(['image', 'playlists'])
->where('type', 'public')
->orWhere(function($query) use ($userId) {
$query->where('type', 'private')
->where(function ($query) use ($userId) {
$query->where('type', 'public')
->orWhere(function ($q) use ($userId) {
$q->where('type', 'private')
->where('user_id', $userId);
});
})
->orderBy('created_at', 'desc')
->get();
->paginate($perPage);
return response()->json([
'data' => $music,
'total' => $music->count()
]);
// Laravel's paginator JSON keeps `data` and `total`, and adds
// current_page / last_page / per_page for the client.
return response()->json($music);
}
public function getMusicByPlaylist($playlistId)