koel/app/Http/Controllers/API/PlaylistController.php

97 lines
2.5 KiB
PHP
Raw Normal View History

2015-12-13 04:42:28 +00:00
<?php
namespace App\Http\Controllers\API;
use App\Http\Requests\API\PlaylistStoreRequest;
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;
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;
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,
?Authenticatable $currentUser
2020-09-06 21:20:42 +00:00
) {
2018-08-29 06:15:11 +00:00
$this->playlistRepository = $playlistRepository;
$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
}
public function update(Request $request, Playlist $playlist)
2015-12-13 04:42:28 +00:00
{
$this->authorize('owner', $playlist);
2015-12-13 04:42:28 +00:00
$playlist->update($request->only('name', 'rules'));
2015-12-13 04:42:28 +00:00
return response()->json($playlist);
}
public function sync(PlaylistSyncRequest $request, Playlist $playlist)
2015-12-13 04:42:28 +00:00
{
$this->authorize('owner', $playlist);
2015-12-13 04:42:28 +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();
}
public function getSongs(Playlist $playlist)
{
$this->authorize('owner', $playlist);
return response()->json(
$playlist->is_smart
? $this->smartPlaylistService->getSongs($playlist)->pluck('id')
: $playlist->songs->pluck('id')
);
}
public function destroy(Playlist $playlist)
2015-12-13 04:42:28 +00:00
{
$this->authorize('owner', $playlist);
2015-12-13 04:42:28 +00:00
$playlist->delete();
2015-12-13 04:42:28 +00:00
return response()->json();
}
}