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;
|
2020-09-06 18:21:39 +00:00
|
|
|
use App\Models\User;
|
2018-08-29 06:15:11 +00:00
|
|
|
use App\Repositories\PlaylistRepository;
|
2018-11-03 23:25:08 +00:00
|
|
|
use App\Services\SmartPlaylistService;
|
2020-09-06 18:21:39 +00:00
|
|
|
use Illuminate\Contracts\Auth\Authenticatable;
|
2015-12-14 13:22:39 +00:00
|
|
|
use Illuminate\Http\Request;
|
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
|
|
|
|
2020-09-06 18:21:39 +00:00
|
|
|
/** @var User */
|
|
|
|
private $currentUser;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
PlaylistRepository $playlistRepository,
|
|
|
|
SmartPlaylistService $smartPlaylistService,
|
2020-09-07 20:43:23 +00:00
|
|
|
?Authenticatable $currentUser
|
2020-09-06 21:20:42 +00:00
|
|
|
) {
|
2018-08-29 06:15:11 +00:00
|
|
|
$this->playlistRepository = $playlistRepository;
|
2018-11-03 23:25:08 +00:00
|
|
|
$this->smartPlaylistService = $smartPlaylistService;
|
2020-09-06 18:21:39 +00:00
|
|
|
$this->currentUser = $currentUser;
|
2018-08-29 06:15:11 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
public function store(PlaylistStoreRequest $request)
|
|
|
|
{
|
2018-11-25 21:21:46 +00:00
|
|
|
/** @var Playlist $playlist */
|
2020-09-06 18:21:39 +00:00
|
|
|
$playlist = $this->currentUser->playlists()->create([
|
2018-11-25 21:21:46 +00:00
|
|
|
'name' => $request->name,
|
|
|
|
'rules' => $request->rules,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$songs = (array) $request->songs;
|
|
|
|
|
|
|
|
if ($songs) {
|
|
|
|
$playlist->songs()->sync($songs);
|
|
|
|
}
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2020-12-22 23:01:49 +00:00
|
|
|
$playlistAsArray = $playlist->toArray();
|
|
|
|
$playlistAsArray['songs'] = $playlist->songs->pluck('id');
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2020-12-22 23:01:49 +00:00
|
|
|
return response()->json($playlistAsArray);
|
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
|
|
|
{
|
2020-09-07 20:43:23 +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);
|
|
|
|
}
|
|
|
|
|
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-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-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();
|
|
|
|
}
|
|
|
|
}
|