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;
|
2023-06-05 21:46:41 +00:00
|
|
|
use App\Http\Requests\API\AddSongsToPlaylistRequest;
|
|
|
|
use App\Http\Requests\API\RemoveSongsFromPlaylistRequest;
|
|
|
|
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;
|
2022-11-27 15:29:29 +00:00
|
|
|
use Illuminate\Http\Response;
|
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(
|
2023-06-05 21:46:41 +00:00
|
|
|
private SongRepository $songRepository,
|
2022-08-01 11:03:58 +00:00
|
|
|
private PlaylistService $playlistService,
|
2023-06-05 21:46:41 +00:00
|
|
|
private SmartPlaylistService $smartPlaylistService,
|
2022-08-10 14:56:01 +00:00
|
|
|
private ?Authenticatable $user
|
2022-07-29 06:47:10 +00:00
|
|
|
) {
|
2021-10-11 11:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function index(Playlist $playlist)
|
|
|
|
{
|
2022-08-10 14:56:01 +00:00
|
|
|
$this->authorize('own', $playlist);
|
2021-10-11 11:30:27 +00:00
|
|
|
|
2023-06-05 21:46:41 +00:00
|
|
|
return SongResource::collection(
|
2021-10-11 11:30:27 +00:00
|
|
|
$playlist->is_smart
|
2023-06-05 21:46:41 +00:00
|
|
|
? $this->smartPlaylistService->getSongs($playlist, $this->user)
|
|
|
|
: $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)
|
|
|
|
{
|
|
|
|
$this->authorize('own', $playlist);
|
|
|
|
|
2024-01-09 11:53:35 +00:00
|
|
|
$this->songRepository->getMany(ids: $request->songs, scopedUser: $this->user)
|
|
|
|
->each(fn ($song) => $this->authorize('access', $song));
|
|
|
|
|
2023-06-05 21:46:41 +00:00
|
|
|
abort_if($playlist->is_smart, Response::HTTP_FORBIDDEN);
|
|
|
|
|
|
|
|
$this->playlistService->addSongsToPlaylist($playlist, $request->songs);
|
|
|
|
|
|
|
|
return response()->noContent();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function destroy(Playlist $playlist, RemoveSongsFromPlaylistRequest $request)
|
2021-10-11 11:30:27 +00:00
|
|
|
{
|
2022-08-10 14:56:01 +00:00
|
|
|
$this->authorize('own', $playlist);
|
2021-10-11 11:30:27 +00:00
|
|
|
|
2023-06-05 21:46:41 +00:00
|
|
|
abort_if($playlist->is_smart, Response::HTTP_FORBIDDEN);
|
2021-10-11 11:30:27 +00:00
|
|
|
|
2023-06-05 21:46:41 +00:00
|
|
|
$this->playlistService->removeSongsFromPlaylist($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
|
|
|
}
|
|
|
|
}
|