2015-12-13 12:42:28 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\API;
|
|
|
|
|
|
|
|
use App\Http\Requests\API\PlaylistStoreRequest;
|
2017-12-09 19:34:27 +01:00
|
|
|
use App\Http\Requests\API\PlaylistSyncRequest;
|
2015-12-14 08:22:39 -05:00
|
|
|
use App\Models\Playlist;
|
2020-09-06 20:21:39 +02:00
|
|
|
use App\Models\User;
|
2018-08-29 13:15:11 +07:00
|
|
|
use App\Repositories\PlaylistRepository;
|
2018-11-04 00:25:08 +01:00
|
|
|
use App\Services\SmartPlaylistService;
|
2017-06-04 02:30:45 +01:00
|
|
|
use Exception;
|
|
|
|
use Illuminate\Auth\Access\AuthorizationException;
|
2020-09-06 20:21:39 +02:00
|
|
|
use Illuminate\Contracts\Auth\Authenticatable;
|
2017-06-04 02:30:45 +01:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2015-12-14 08:22:39 -05:00
|
|
|
use Illuminate\Http\Request;
|
2015-12-13 12:42:28 +08:00
|
|
|
|
|
|
|
class PlaylistController extends Controller
|
|
|
|
{
|
2018-08-29 13:15:11 +07:00
|
|
|
private $playlistRepository;
|
2018-11-04 00:25:08 +01:00
|
|
|
private $smartPlaylistService;
|
2018-08-29 13:15:11 +07:00
|
|
|
|
2020-09-06 20:21:39 +02:00
|
|
|
/** @var User */
|
|
|
|
private $currentUser;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
PlaylistRepository $playlistRepository,
|
|
|
|
SmartPlaylistService $smartPlaylistService,
|
2020-09-07 22:43:23 +02:00
|
|
|
?Authenticatable $currentUser
|
2020-09-06 23:20:42 +02:00
|
|
|
) {
|
2018-08-29 13:15:11 +07:00
|
|
|
$this->playlistRepository = $playlistRepository;
|
2018-11-04 00:25:08 +01:00
|
|
|
$this->smartPlaylistService = $smartPlaylistService;
|
2020-09-06 20:21:39 +02:00
|
|
|
$this->currentUser = $currentUser;
|
2018-08-29 13:15:11 +07:00
|
|
|
}
|
|
|
|
|
2016-01-26 14:32:29 +08:00
|
|
|
public function index()
|
|
|
|
{
|
2018-08-29 13:15:11 +07:00
|
|
|
return response()->json($this->playlistRepository->getAllByCurrentUser());
|
2016-01-26 14:32:29 +08:00
|
|
|
}
|
|
|
|
|
2015-12-13 12:42:28 +08:00
|
|
|
public function store(PlaylistStoreRequest $request)
|
|
|
|
{
|
2018-11-25 22:21:46 +01:00
|
|
|
/** @var Playlist $playlist */
|
2020-09-06 20:21:39 +02:00
|
|
|
$playlist = $this->currentUser->playlists()->create([
|
2018-11-25 22:21:46 +01:00
|
|
|
'name' => $request->name,
|
|
|
|
'rules' => $request->rules,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$songs = (array) $request->songs;
|
|
|
|
|
|
|
|
if ($songs) {
|
|
|
|
$playlist->songs()->sync($songs);
|
|
|
|
}
|
2015-12-13 12:42:28 +08:00
|
|
|
|
2015-12-29 08:35:22 +07:00
|
|
|
$playlist->songs = $playlist->songs->pluck('id');
|
2015-12-13 12:42:28 +08:00
|
|
|
|
|
|
|
return response()->json($playlist);
|
|
|
|
}
|
|
|
|
|
2015-12-14 14:27:26 -02:00
|
|
|
public function update(Request $request, Playlist $playlist)
|
2015-12-13 12:42:28 +08:00
|
|
|
{
|
2020-09-07 22:43:23 +02:00
|
|
|
$this->authorize('owner', $playlist);
|
2015-12-13 12:42:28 +08:00
|
|
|
|
2019-04-13 22:38:34 +02:00
|
|
|
$playlist->update($request->only('name', 'rules'));
|
2015-12-13 12:42:28 +08:00
|
|
|
|
|
|
|
return response()->json($playlist);
|
|
|
|
}
|
|
|
|
|
2017-12-09 19:34:27 +01:00
|
|
|
public function sync(PlaylistSyncRequest $request, Playlist $playlist)
|
2015-12-13 12:42:28 +08:00
|
|
|
{
|
2015-12-14 14:27:26 -02:00
|
|
|
$this->authorize('owner', $playlist);
|
2015-12-13 12:42:28 +08:00
|
|
|
|
2018-11-04 00:25:08 +01:00
|
|
|
abort_if($playlist->is_smart, 403, 'A smart playlist\'s content cannot be updated manually.');
|
|
|
|
|
2016-11-18 13:38:25 +08:00
|
|
|
$playlist->songs()->sync((array) $request->songs);
|
2015-12-13 12:42:28 +08:00
|
|
|
|
|
|
|
return response()->json();
|
|
|
|
}
|
|
|
|
|
2017-12-09 19:34:27 +01:00
|
|
|
public function getSongs(Playlist $playlist)
|
2017-12-02 17:05:40 +01:00
|
|
|
{
|
|
|
|
$this->authorize('owner', $playlist);
|
|
|
|
|
2018-11-04 00:25:08 +01:00
|
|
|
return response()->json(
|
|
|
|
$playlist->is_smart
|
|
|
|
? $this->smartPlaylistService->getSongs($playlist)->pluck('id')
|
|
|
|
: $playlist->songs->pluck('id')
|
|
|
|
);
|
2017-12-02 17:05:40 +01:00
|
|
|
}
|
|
|
|
|
2015-12-13 12:42:28 +08:00
|
|
|
/**
|
2020-04-12 10:18:17 +02:00
|
|
|
* Delete a playlist
|
2015-12-13 12:42:28 +08:00
|
|
|
*
|
2018-12-09 22:24:43 +01:00
|
|
|
* @response []
|
|
|
|
*
|
2017-06-04 02:30:45 +01:00
|
|
|
* @throws Exception
|
|
|
|
* @throws AuthorizationException
|
2016-08-03 18:42:39 +08:00
|
|
|
*
|
2017-06-04 02:30:45 +01:00
|
|
|
* @return JsonResponse
|
2015-12-13 12:42:28 +08:00
|
|
|
*/
|
2015-12-14 14:27:26 -02:00
|
|
|
public function destroy(Playlist $playlist)
|
2015-12-13 12:42:28 +08:00
|
|
|
{
|
2015-12-14 14:27:26 -02:00
|
|
|
$this->authorize('owner', $playlist);
|
2015-12-13 12:42:28 +08:00
|
|
|
|
2015-12-14 14:27:26 -02:00
|
|
|
$playlist->delete();
|
2015-12-13 12:42:28 +08:00
|
|
|
|
|
|
|
return response()->json();
|
|
|
|
}
|
|
|
|
}
|