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

64 lines
1.9 KiB
PHP
Raw Normal View History

<?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;
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\Response;
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)
{
$this->authorize('own', $playlist);
2023-06-05 21:46:41 +00:00
return SongResource::collection(
$playlist->is_smart
2023-06-05 21:46:41 +00:00
? $this->smartPlaylistService->getSongs($playlist, $this->user)
: $this->songRepository->getByStandardPlaylist($playlist, $this->user)
);
}
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)
{
$this->authorize('own', $playlist);
2023-06-05 21:46:41 +00:00
abort_if($playlist->is_smart, Response::HTTP_FORBIDDEN);
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();
}
}