mirror of
https://github.com/koel/koel
synced 2024-12-03 01:09:23 +00:00
95 lines
2.3 KiB
PHP
95 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\API;
|
|
|
|
use App\Http\Requests\API\PlaylistStoreRequest;
|
|
use App\Models\Playlist;
|
|
use Illuminate\Http\Request;
|
|
|
|
class PlaylistController extends Controller
|
|
{
|
|
/**
|
|
* Gets all playlists by the current user.
|
|
*
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function index()
|
|
{
|
|
return response()->json(Playlist::byCurrentUser()->orderBy('name')->with('songs')->get());
|
|
}
|
|
|
|
/**
|
|
* Create a new playlist.
|
|
*
|
|
* @param PlaylistStoreRequest $request
|
|
*
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function store(PlaylistStoreRequest $request)
|
|
{
|
|
$playlist = auth()->user()->playlists()->create($request->only('name'));
|
|
$playlist->songs()->sync((array) $request->songs);
|
|
|
|
$playlist->songs = $playlist->songs->pluck('id');
|
|
|
|
return response()->json($playlist);
|
|
}
|
|
|
|
/**
|
|
* Rename a playlist.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param Playlist $playlist
|
|
*
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
*
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function update(Request $request, Playlist $playlist)
|
|
{
|
|
$this->authorize('owner', $playlist);
|
|
|
|
$playlist->update($request->only('name'));
|
|
|
|
return response()->json($playlist);
|
|
}
|
|
|
|
/**
|
|
* Sync a playlist with songs.
|
|
* Any songs that are not populated here will be removed from the playlist.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param Playlist $playlist
|
|
*
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
*
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function sync(Request $request, Playlist $playlist)
|
|
{
|
|
$this->authorize('owner', $playlist);
|
|
|
|
$playlist->songs()->sync((array) $request->songs);
|
|
|
|
return response()->json();
|
|
}
|
|
|
|
/**
|
|
* Delete a playlist.
|
|
*
|
|
* @param Playlist $playlist
|
|
*
|
|
* @throws \Exception
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
|
*
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function destroy(Playlist $playlist)
|
|
{
|
|
$this->authorize('owner', $playlist);
|
|
|
|
$playlist->delete();
|
|
|
|
return response()->json();
|
|
}
|
|
}
|