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); return response()->json($music);
} }
public function index() public function index(Request $request)
{ {
$userId = auth()->id(); $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']) $music = Music::with(['image', 'playlists'])
->where('type', 'public') ->where(function ($query) use ($userId) {
->orWhere(function($query) use ($userId) { $query->where('type', 'public')
$query->where('type', 'private') ->orWhere(function ($q) use ($userId) {
$q->where('type', 'private')
->where('user_id', $userId); ->where('user_id', $userId);
});
}) })
->orderBy('created_at', 'desc') ->orderBy('created_at', 'desc')
->get(); ->paginate($perPage);
return response()->json([ // Laravel's paginator JSON keeps `data` and `total`, and adds
'data' => $music, // current_page / last_page / per_page for the client.
'total' => $music->count() return response()->json($music);
]);
} }
public function getMusicByPlaylist($playlistId) public function getMusicByPlaylist($playlistId)