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;
|
2017-06-04 01:30:45 +00:00
|
|
|
use Exception;
|
|
|
|
use Illuminate\Auth\Access\AuthorizationException;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
2015-12-14 13:22:39 +00:00
|
|
|
use Illuminate\Http\Request;
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
class PlaylistController extends Controller
|
|
|
|
{
|
2016-01-26 06:32:29 +00:00
|
|
|
/**
|
|
|
|
* Gets all playlists by the current user.
|
2016-03-06 04:11:28 +00:00
|
|
|
*
|
2017-06-04 01:30:45 +00:00
|
|
|
* @return JsonResponse
|
2016-01-26 06:32:29 +00:00
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
return response()->json(Playlist::byCurrentUser()->orderBy('name')->with('songs')->get());
|
|
|
|
}
|
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
/**
|
|
|
|
* Create a new playlist.
|
|
|
|
*
|
|
|
|
* @param PlaylistStoreRequest $request
|
|
|
|
*
|
2017-06-04 01:30:45 +00:00
|
|
|
* @return JsonResponse
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
|
|
|
public function store(PlaylistStoreRequest $request)
|
|
|
|
{
|
2017-05-02 03:23:10 +00:00
|
|
|
$playlist = $request->user()->playlists()->create($request->only('name'));
|
2017-04-29 02:55:41 +00:00
|
|
|
$playlist->songs()->sync((array) $request->songs);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2015-12-29 01:35:22 +00:00
|
|
|
$playlist->songs = $playlist->songs->pluck('id');
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
return response()->json($playlist);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rename a playlist.
|
|
|
|
*
|
2017-06-04 01:31:59 +00:00
|
|
|
* @param Request $request
|
|
|
|
* @param Playlist $playlist
|
2015-12-13 04:42:28 +00:00
|
|
|
*
|
2017-06-04 01:30:45 +00:00
|
|
|
* @throws AuthorizationException
|
2016-08-03 10:42:39 +00:00
|
|
|
*
|
2017-06-04 01:30:45 +00:00
|
|
|
* @return JsonResponse
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
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.
|
|
|
|
*
|
2017-06-04 01:31:59 +00:00
|
|
|
* @param Request $request
|
|
|
|
* @param Playlist $playlist
|
2015-12-13 04:42:28 +00:00
|
|
|
*
|
2017-06-04 01:30:45 +00:00
|
|
|
* @throws AuthorizationException
|
2016-08-03 10:42:39 +00:00
|
|
|
*
|
2017-06-04 01:30:45 +00:00
|
|
|
* @return JsonResponse
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
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
|
|
|
|
2016-11-18 05:38:25 +00:00
|
|
|
$playlist->songs()->sync((array) $request->songs);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
return response()->json();
|
|
|
|
}
|
|
|
|
|
2017-12-02 16:05:40 +00:00
|
|
|
/**
|
|
|
|
* Get a playlist's all songs.
|
|
|
|
*
|
2017-12-02 16:07:53 +00:00
|
|
|
* @param Request $request
|
|
|
|
* @param Playlist $playlist
|
|
|
|
*
|
2017-12-02 16:05:40 +00:00
|
|
|
* @return JsonResponse
|
|
|
|
*/
|
|
|
|
public function getSongs(Request $request, Playlist $playlist)
|
|
|
|
{
|
|
|
|
$this->authorize('owner', $playlist);
|
|
|
|
|
|
|
|
return response()->json($playlist->songs->pluck('id'));
|
|
|
|
}
|
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
/**
|
|
|
|
* Delete a playlist.
|
|
|
|
*
|
2015-12-15 10:35:46 +00:00
|
|
|
* @param Playlist $playlist
|
2015-12-13 04:42:28 +00:00
|
|
|
*
|
2017-06-04 01:30:45 +00:00
|
|
|
* @throws Exception
|
|
|
|
* @throws AuthorizationException
|
2016-08-03 10:42:39 +00:00
|
|
|
*
|
2017-06-04 01:30:45 +00:00
|
|
|
* @return JsonResponse
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
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();
|
|
|
|
}
|
|
|
|
}
|