2015-12-13 04:42:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\API;
|
|
|
|
|
|
|
|
use App\Http\Requests\API\PlaylistStoreRequest;
|
2015-12-14 13:22:39 +00:00
|
|
|
use App\Models\Playlist;
|
|
|
|
use Illuminate\Http\Request;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
class PlaylistController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* 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($request->input('songs'));
|
|
|
|
|
2015-12-29 06:26:09 +00:00
|
|
|
$playlist->songs = $playlist->songs->pluck('id');
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
return response()->json($playlist);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rename a playlist.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
2015-12-15 10:35:46 +00:00
|
|
|
* @param Playlist $playlist
|
2015-12-13 04:42:28 +00:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
2015-12-14 16:27:26 +00:00
|
|
|
public function update(Request $request, Playlist $playlist)
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2015-12-14 16:27:26 +00:00
|
|
|
$this->authorize('owner', $playlist);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2015-12-14 16:27:26 +00:00
|
|
|
$playlist->update($request->only('name'));
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
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
|
2015-12-15 10:35:46 +00:00
|
|
|
* @param Playlist $playlist
|
2015-12-13 04:42:28 +00:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
2015-12-14 16:27:26 +00:00
|
|
|
public function sync(Request $request, Playlist $playlist)
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2015-12-14 16:27:26 +00:00
|
|
|
$this->authorize('owner', $playlist);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
$playlist->songs()->sync($request->input('songs'));
|
|
|
|
|
|
|
|
return response()->json();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a playlist.
|
|
|
|
*
|
2015-12-15 10:35:46 +00:00
|
|
|
* @param Playlist $playlist
|
2015-12-13 04:42:28 +00:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
2015-12-14 16:27:26 +00:00
|
|
|
public function destroy(Playlist $playlist)
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2015-12-14 16:27:26 +00:00
|
|
|
$this->authorize('owner', $playlist);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2015-12-14 16:27:26 +00:00
|
|
|
$playlist->delete();
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
return response()->json();
|
|
|
|
}
|
|
|
|
}
|