2021-10-11 11:30:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\API;
|
|
|
|
|
2024-01-18 11:13:05 +00:00
|
|
|
use App\Facades\License;
|
2022-07-29 06:47:10 +00:00
|
|
|
use App\Http\Controllers\Controller;
|
2023-06-05 21:46:41 +00:00
|
|
|
use App\Http\Requests\API\AddSongsToPlaylistRequest;
|
|
|
|
use App\Http\Requests\API\RemoveSongsFromPlaylistRequest;
|
2024-01-18 11:13:05 +00:00
|
|
|
use App\Http\Resources\CollaborativeSongResource;
|
2023-06-05 21:46:41 +00:00
|
|
|
use App\Http\Resources\SongResource;
|
2021-10-11 11:30:27 +00:00
|
|
|
use App\Models\Playlist;
|
2022-08-01 11:03:58 +00:00
|
|
|
use App\Models\User;
|
2023-06-05 21:46:41 +00:00
|
|
|
use App\Repositories\SongRepository;
|
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;
|
2024-01-25 16:21:26 +00:00
|
|
|
use Illuminate\Http\Resources\Json\ResourceCollection;
|
2022-11-27 15:29:29 +00:00
|
|
|
use Illuminate\Http\Response;
|
2024-01-25 16:21:26 +00:00
|
|
|
use Illuminate\Support\Collection;
|
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(
|
2024-04-18 14:36:28 +00:00
|
|
|
private readonly SongRepository $songRepository,
|
|
|
|
private readonly PlaylistService $playlistService,
|
|
|
|
private readonly SmartPlaylistService $smartPlaylistService,
|
|
|
|
private readonly ?Authenticatable $user
|
2022-07-29 06:47:10 +00:00
|
|
|
) {
|
2021-10-11 11:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function index(Playlist $playlist)
|
|
|
|
{
|
2024-01-18 11:13:05 +00:00
|
|
|
if ($playlist->is_smart) {
|
|
|
|
$this->authorize('own', $playlist);
|
|
|
|
return SongResource::collection($this->smartPlaylistService->getSongs($playlist, $this->user));
|
|
|
|
}
|
2021-10-11 11:30:27 +00:00
|
|
|
|
2024-01-18 11:13:05 +00:00
|
|
|
$this->authorize('collaborate', $playlist);
|
|
|
|
|
2024-01-25 16:21:26 +00:00
|
|
|
return self::createResourceCollection($this->songRepository->getByStandardPlaylist($playlist, $this->user));
|
2021-10-11 11:30:27 +00:00
|
|
|
}
|
|
|
|
|
2023-06-05 21:46:41 +00:00
|
|
|
public function store(Playlist $playlist, AddSongsToPlaylistRequest $request)
|
|
|
|
{
|
2024-01-18 11:13:05 +00:00
|
|
|
abort_if($playlist->is_smart, Response::HTTP_FORBIDDEN, 'Smart playlist content is automatically generated');
|
2023-06-05 21:46:41 +00:00
|
|
|
|
2024-01-18 11:13:05 +00:00
|
|
|
$this->authorize('collaborate', $playlist);
|
2024-01-09 11:53:35 +00:00
|
|
|
|
2024-06-04 09:44:33 +00:00
|
|
|
$playables = $this->songRepository->getMany(ids: $request->songs, scopedUser: $this->user);
|
2023-06-05 21:46:41 +00:00
|
|
|
|
2024-04-18 14:36:28 +00:00
|
|
|
return self::createResourceCollection(
|
2024-06-04 09:44:33 +00:00
|
|
|
$this->playlistService->addPlayablesToPlaylist($playlist, $playables, $this->user)
|
2024-04-18 14:36:28 +00:00
|
|
|
);
|
2024-01-25 16:21:26 +00:00
|
|
|
}
|
2023-06-05 21:46:41 +00:00
|
|
|
|
2024-01-25 16:21:26 +00:00
|
|
|
private static function createResourceCollection(Collection $songs): ResourceCollection
|
|
|
|
{
|
|
|
|
return License::isPlus()
|
|
|
|
? CollaborativeSongResource::collection($songs)
|
|
|
|
: SongResource::collection($songs);
|
2023-06-05 21:46:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function destroy(Playlist $playlist, RemoveSongsFromPlaylistRequest $request)
|
2021-10-11 11:30:27 +00:00
|
|
|
{
|
2024-01-18 11:13:05 +00:00
|
|
|
abort_if($playlist->is_smart, Response::HTTP_FORBIDDEN, 'Smart playlist content is automatically generated');
|
2024-01-25 16:21:26 +00:00
|
|
|
|
2024-01-18 11:13:05 +00:00
|
|
|
$this->authorize('collaborate', $playlist);
|
2024-05-31 05:40:34 +00:00
|
|
|
$this->playlistService->removePlayablesFromPlaylist($playlist, $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
|
|
|
}
|
|
|
|
}
|