2021-10-11 11:30:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\API;
|
|
|
|
|
2022-07-29 06:47:10 +00:00
|
|
|
use App\Http\Controllers\Controller;
|
2021-10-11 11:30:27 +00:00
|
|
|
use App\Http\Requests\API\PlaylistSongUpdateRequest;
|
|
|
|
use App\Models\Playlist;
|
2022-08-01 11:03:58 +00:00
|
|
|
use App\Models\User;
|
2022-07-29 06:47:10 +00:00
|
|
|
use App\Services\PlaylistService;
|
2021-10-11 11:30:27 +00:00
|
|
|
use App\Services\SmartPlaylistService;
|
2022-08-01 11:03:58 +00:00
|
|
|
use Illuminate\Contracts\Auth\Authenticatable;
|
2021-10-11 11:30:27 +00:00
|
|
|
|
|
|
|
class PlaylistSongController extends Controller
|
|
|
|
{
|
2022-08-01 11:03:58 +00:00
|
|
|
/** @param User $user */
|
2022-07-29 06:47:10 +00:00
|
|
|
public function __construct(
|
|
|
|
private SmartPlaylistService $smartPlaylistService,
|
2022-08-01 11:03:58 +00:00
|
|
|
private PlaylistService $playlistService,
|
|
|
|
private Authenticatable $user
|
2022-07-29 06:47:10 +00:00
|
|
|
) {
|
2021-10-11 11:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function index(Playlist $playlist)
|
|
|
|
{
|
|
|
|
$this->authorize('owner', $playlist);
|
|
|
|
|
|
|
|
return response()->json(
|
|
|
|
$playlist->is_smart
|
2022-08-01 11:03:58 +00:00
|
|
|
? $this->smartPlaylistService->getSongs($playlist, $this->user)->pluck('id')
|
2021-10-11 11:30:27 +00:00
|
|
|
: $playlist->songs->pluck('id')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-29 06:47:10 +00:00
|
|
|
/** @deprecated */
|
2021-10-11 11:30:27 +00:00
|
|
|
public function update(PlaylistSongUpdateRequest $request, Playlist $playlist)
|
|
|
|
{
|
|
|
|
$this->authorize('owner', $playlist);
|
|
|
|
|
2022-07-29 06:47:10 +00:00
|
|
|
abort_if($playlist->is_smart, 403, 'A smart playlist cannot be populated manually.');
|
2021-10-11 11:30:27 +00:00
|
|
|
|
2022-07-29 06:47:10 +00:00
|
|
|
$this->playlistService->populatePlaylist($playlist, (array) $request->songs);
|
2021-10-11 11:30:27 +00:00
|
|
|
|
2022-07-29 06:47:10 +00:00
|
|
|
return response()->noContent();
|
2021-10-11 11:30:27 +00:00
|
|
|
}
|
|
|
|
}
|