feat: add paginatin to music
This commit is contained in:
@@ -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('user_id', $userId);
|
||||
->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)
|
||||
|
||||
Reference in New Issue
Block a user