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

72 lines
2.5 KiB
PHP
Raw Normal View History

<?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;
use App\Models\Playlist;
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;
use App\Services\SmartPlaylistService;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Http\Response;
use Illuminate\Support\Collection;
class PlaylistSongController extends Controller
{
/** @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,
private PlaylistService $playlistService,
2023-06-05 21:46:41 +00:00
private SmartPlaylistService $smartPlaylistService,
private ?Authenticatable $user
2022-07-29 06:47:10 +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));
}
2024-01-18 11:13:05 +00:00
$this->authorize('collaborate', $playlist);
return self::createResourceCollection($this->songRepository->getByStandardPlaylist($playlist, $this->user));
}
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-01-18 11:13:05 +00:00
$songs = $this->songRepository->getMany(ids: $request->songs, scopedUser: $this->user);
2023-06-05 21:46:41 +00:00
return self::createResourceCollection($this->playlistService->addSongsToPlaylist($playlist, $songs, $this->user));
}
2023-06-05 21:46:41 +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)
{
2024-01-18 11:13:05 +00:00
abort_if($playlist->is_smart, Response::HTTP_FORBIDDEN, 'Smart playlist content is automatically generated');
2024-01-18 11:13:05 +00:00
$this->authorize('collaborate', $playlist);
2023-06-05 21:46:41 +00:00
$this->playlistService->removeSongsFromPlaylist($playlist, $request->songs);
2022-07-29 06:47:10 +00:00
return response()->noContent();
}
}