2021-10-11 13:30:27 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\API;
|
|
|
|
|
2024-01-18 12:13:05 +01:00
|
|
|
use App\Facades\License;
|
2022-07-29 08:47:10 +02:00
|
|
|
use App\Http\Controllers\Controller;
|
2023-06-05 23:46:41 +02:00
|
|
|
use App\Http\Requests\API\AddSongsToPlaylistRequest;
|
|
|
|
use App\Http\Requests\API\RemoveSongsFromPlaylistRequest;
|
2024-01-18 12:13:05 +01:00
|
|
|
use App\Http\Resources\CollaborativeSongResource;
|
2023-06-05 23:46:41 +02:00
|
|
|
use App\Http\Resources\SongResource;
|
2021-10-11 13:30:27 +02:00
|
|
|
use App\Models\Playlist;
|
2022-08-01 13:03:58 +02:00
|
|
|
use App\Models\User;
|
2023-06-05 23:46:41 +02:00
|
|
|
use App\Repositories\SongRepository;
|
2022-07-29 08:47:10 +02:00
|
|
|
use App\Services\PlaylistService;
|
2021-10-11 13:30:27 +02:00
|
|
|
use App\Services\SmartPlaylistService;
|
2022-08-01 13:03:58 +02:00
|
|
|
use Illuminate\Contracts\Auth\Authenticatable;
|
2024-01-25 17:21:26 +01:00
|
|
|
use Illuminate\Http\Resources\Json\ResourceCollection;
|
2022-11-27 16:29:29 +01:00
|
|
|
use Illuminate\Http\Response;
|
2024-01-25 17:21:26 +01:00
|
|
|
use Illuminate\Support\Collection;
|
2021-10-11 13:30:27 +02:00
|
|
|
|
|
|
|
class PlaylistSongController extends Controller
|
|
|
|
{
|
2022-08-01 13:03:58 +02:00
|
|
|
/** @param User $user */
|
2022-07-29 08:47:10 +02:00
|
|
|
public function __construct(
|
2023-06-05 23:46:41 +02:00
|
|
|
private SongRepository $songRepository,
|
2022-08-01 13:03:58 +02:00
|
|
|
private PlaylistService $playlistService,
|
2023-06-05 23:46:41 +02:00
|
|
|
private SmartPlaylistService $smartPlaylistService,
|
2022-08-10 16:56:01 +02:00
|
|
|
private ?Authenticatable $user
|
2022-07-29 08:47:10 +02:00
|
|
|
) {
|
2021-10-11 13:30:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function index(Playlist $playlist)
|
|
|
|
{
|
2024-01-18 12:13:05 +01:00
|
|
|
if ($playlist->is_smart) {
|
|
|
|
$this->authorize('own', $playlist);
|
|
|
|
return SongResource::collection($this->smartPlaylistService->getSongs($playlist, $this->user));
|
|
|
|
}
|
2021-10-11 13:30:27 +02:00
|
|
|
|
2024-01-18 12:13:05 +01:00
|
|
|
$this->authorize('collaborate', $playlist);
|
|
|
|
|
2024-01-25 17:21:26 +01:00
|
|
|
return self::createResourceCollection($this->songRepository->getByStandardPlaylist($playlist, $this->user));
|
2021-10-11 13:30:27 +02:00
|
|
|
}
|
|
|
|
|
2023-06-05 23:46:41 +02:00
|
|
|
public function store(Playlist $playlist, AddSongsToPlaylistRequest $request)
|
|
|
|
{
|
2024-01-18 12:13:05 +01:00
|
|
|
abort_if($playlist->is_smart, Response::HTTP_FORBIDDEN, 'Smart playlist content is automatically generated');
|
2023-06-05 23:46:41 +02:00
|
|
|
|
2024-01-18 12:13:05 +01:00
|
|
|
$this->authorize('collaborate', $playlist);
|
2024-01-09 12:53:35 +01:00
|
|
|
|
2024-01-18 12:13:05 +01:00
|
|
|
$songs = $this->songRepository->getMany(ids: $request->songs, scopedUser: $this->user);
|
2023-06-05 23:46:41 +02:00
|
|
|
|
2024-01-25 17:21:26 +01:00
|
|
|
return self::createResourceCollection($this->playlistService->addSongsToPlaylist($playlist, $songs, $this->user));
|
|
|
|
}
|
2023-06-05 23:46:41 +02:00
|
|
|
|
2024-01-25 17:21:26 +01:00
|
|
|
private static function createResourceCollection(Collection $songs): ResourceCollection
|
|
|
|
{
|
|
|
|
return License::isPlus()
|
|
|
|
? CollaborativeSongResource::collection($songs)
|
|
|
|
: SongResource::collection($songs);
|
2023-06-05 23:46:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function destroy(Playlist $playlist, RemoveSongsFromPlaylistRequest $request)
|
2021-10-11 13:30:27 +02:00
|
|
|
{
|
2024-01-18 12:13:05 +01:00
|
|
|
abort_if($playlist->is_smart, Response::HTTP_FORBIDDEN, 'Smart playlist content is automatically generated');
|
2024-01-25 17:21:26 +01:00
|
|
|
|
2024-01-18 12:13:05 +01:00
|
|
|
$this->authorize('collaborate', $playlist);
|
2023-06-05 23:46:41 +02:00
|
|
|
$this->playlistService->removeSongsFromPlaylist($playlist, $request->songs);
|
2021-10-11 13:30:27 +02:00
|
|
|
|
2022-07-29 08:47:10 +02:00
|
|
|
return response()->noContent();
|
2021-10-11 13:30:27 +02:00
|
|
|
}
|
|
|
|
}
|