2015-12-13 04:42:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\API;
|
|
|
|
|
|
|
|
use App\Http\Requests\API\PlaylistStoreRequest;
|
2017-12-09 18:34:27 +00:00
|
|
|
use App\Http\Requests\API\PlaylistSyncRequest;
|
2015-12-14 13:22:39 +00:00
|
|
|
use App\Models\Playlist;
|
2018-08-29 06:15:11 +00:00
|
|
|
use App\Repositories\PlaylistRepository;
|
2018-11-03 23:25:08 +00:00
|
|
|
use App\Services\SmartPlaylistService;
|
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
|
|
|
|
2018-12-09 21:24:43 +00:00
|
|
|
/**
|
|
|
|
* @group 4. Playlist management
|
|
|
|
*/
|
2015-12-13 04:42:28 +00:00
|
|
|
class PlaylistController extends Controller
|
|
|
|
{
|
2018-08-29 06:15:11 +00:00
|
|
|
private $playlistRepository;
|
2018-11-03 23:25:08 +00:00
|
|
|
private $smartPlaylistService;
|
2018-08-29 06:15:11 +00:00
|
|
|
|
2018-11-03 23:25:08 +00:00
|
|
|
public function __construct(PlaylistRepository $playlistRepository, SmartPlaylistService $smartPlaylistService)
|
2018-08-29 06:15:11 +00:00
|
|
|
{
|
|
|
|
$this->playlistRepository = $playlistRepository;
|
2018-11-03 23:25:08 +00:00
|
|
|
$this->smartPlaylistService = $smartPlaylistService;
|
2018-08-29 06:15:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 06:32:29 +00:00
|
|
|
/**
|
2018-12-09 21:24:43 +00:00
|
|
|
* Get current user's playlists.
|
|
|
|
*
|
|
|
|
* @responseFile responses/playlist.index.json
|
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()
|
|
|
|
{
|
2018-08-29 06:15:11 +00:00
|
|
|
return response()->json($this->playlistRepository->getAllByCurrentUser());
|
2016-01-26 06:32:29 +00:00
|
|
|
}
|
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
/**
|
|
|
|
* Create a new playlist.
|
|
|
|
*
|
2018-12-09 21:24:43 +00:00
|
|
|
* @bodyParam name string required Name of the playlist. Example: Sleepy Songs
|
|
|
|
* @bodyParam rules array An array of rules if creating a "smart playlist."
|
|
|
|
* @responseFile responses/playlist.store.json
|
|
|
|
*
|
2017-06-04 01:30:45 +00:00
|
|
|
* @return JsonResponse
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
|
|
|
public function store(PlaylistStoreRequest $request)
|
|
|
|
{
|
2018-11-25 21:21:46 +00:00
|
|
|
/** @var Playlist $playlist */
|
|
|
|
$playlist = $request->user()->playlists()->create([
|
|
|
|
'name' => $request->name,
|
|
|
|
'rules' => $request->rules,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$songs = (array) $request->songs;
|
|
|
|
|
|
|
|
if ($songs) {
|
|
|
|
$playlist->songs()->sync($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.
|
|
|
|
*
|
2018-12-09 21:24:43 +00:00
|
|
|
* @bodyParam name string required New name of the playlist. Example: Catchy Songs
|
|
|
|
* @responseFile responses/playlist.update.json
|
|
|
|
*
|
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
|
|
|
|
2019-04-13 20:38:34 +00:00
|
|
|
$playlist->update($request->only('name', 'rules'));
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
return response()->json($playlist);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-12-09 21:24:43 +00:00
|
|
|
* Replace a playlist's content.
|
|
|
|
*
|
|
|
|
* Instead of adding or removing songs individually, a playlist's content is replaced entirely with an array of song IDs.
|
|
|
|
*
|
|
|
|
* @bodyParam songs array required An array of song IDs.
|
|
|
|
* @response []
|
2015-12-13 04:42:28 +00:00
|
|
|
*
|
2017-12-09 18:34:27 +00:00
|
|
|
* @throws AuthorizationException
|
2017-12-09 20:10:55 +00:00
|
|
|
*
|
|
|
|
* @return JsonResponse
|
2015-12-13 04:42:28 +00:00
|
|
|
*/
|
2017-12-09 18:34:27 +00:00
|
|
|
public function sync(PlaylistSyncRequest $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
|
|
|
|
2018-11-03 23:25:08 +00:00
|
|
|
abort_if($playlist->is_smart, 403, 'A smart playlist\'s content cannot be updated manually.');
|
|
|
|
|
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
|
|
|
/**
|
2018-12-09 21:24:43 +00:00
|
|
|
* Get a playlist's songs.
|
|
|
|
*
|
|
|
|
* @response ["0146d01afb742b01f28ab8b556f9a75d", "c741133cb8d1982a5c60b1ce2a1e6e47", "..."]
|
2017-12-02 16:05:40 +00:00
|
|
|
*
|
2017-12-09 18:34:27 +00:00
|
|
|
* @throws AuthorizationException
|
2017-12-09 20:10:55 +00:00
|
|
|
*
|
|
|
|
* @return JsonResponse
|
2017-12-02 16:05:40 +00:00
|
|
|
*/
|
2017-12-09 18:34:27 +00:00
|
|
|
public function getSongs(Playlist $playlist)
|
2017-12-02 16:05:40 +00:00
|
|
|
{
|
|
|
|
$this->authorize('owner', $playlist);
|
|
|
|
|
2018-11-03 23:25:08 +00:00
|
|
|
return response()->json(
|
|
|
|
$playlist->is_smart
|
|
|
|
? $this->smartPlaylistService->getSongs($playlist)->pluck('id')
|
|
|
|
: $playlist->songs->pluck('id')
|
|
|
|
);
|
2017-12-02 16:05:40 +00:00
|
|
|
}
|
|
|
|
|
2015-12-13 04:42:28 +00:00
|
|
|
/**
|
|
|
|
* Delete a playlist.
|
|
|
|
*
|
2018-12-09 21:24:43 +00:00
|
|
|
* @response []
|
|
|
|
*
|
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();
|
|
|
|
}
|
|
|
|
}
|