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

61 lines
1.8 KiB
PHP
Raw Normal View History

2022-06-10 10:47:46 +00:00
<?php
namespace App\Http\Controllers\V6\API;
2022-07-29 06:47:10 +00:00
use App\Http\Controllers\Controller;
use App\Http\Requests\V6\API\AddSongsToPlaylistRequest;
use App\Http\Requests\V6\API\RemoveSongsFromPlaylistRequest;
2022-06-10 10:47:46 +00:00
use App\Http\Resources\SongResource;
use App\Models\Playlist;
use App\Models\User;
use App\Repositories\SongRepository;
2022-07-04 10:39:02 +00:00
use App\Services\PlaylistService;
2022-06-10 10:47:46 +00:00
use App\Services\SmartPlaylistService;
use Illuminate\Contracts\Auth\Authenticatable;
2022-07-04 10:39:02 +00:00
use Illuminate\Http\Response;
2022-06-10 10:47:46 +00:00
class PlaylistSongController extends Controller
{
/** @param User $user */
public function __construct(
private SongRepository $songRepository,
2022-07-04 10:39:02 +00:00
private PlaylistService $playlistService,
2022-06-10 10:47:46 +00:00
private SmartPlaylistService $smartPlaylistService,
private ?Authenticatable $user
) {
}
public function index(Playlist $playlist)
{
$this->authorize('own', $playlist);
2022-06-10 10:47:46 +00:00
return SongResource::collection(
$playlist->is_smart
? $this->smartPlaylistService->getSongs($playlist, $this->user)
2022-06-10 10:47:46 +00:00
: $this->songRepository->getByStandardPlaylist($playlist, $this->user)
);
}
2022-07-04 10:39:02 +00:00
public function store(Playlist $playlist, AddSongsToPlaylistRequest $request)
2022-07-04 10:39:02 +00:00
{
$this->authorize('own', $playlist);
2022-07-04 10:39:02 +00:00
abort_if($playlist->is_smart, Response::HTTP_FORBIDDEN);
$this->playlistService->addSongsToPlaylist($playlist, $request->songs);
2022-07-27 08:49:33 +00:00
return response()->noContent();
2022-07-04 10:39:02 +00:00
}
public function destroy(Playlist $playlist, RemoveSongsFromPlaylistRequest $request)
2022-07-04 10:39:02 +00:00
{
$this->authorize('own', $playlist);
2022-07-04 10:39:02 +00:00
abort_if($playlist->is_smart, Response::HTTP_FORBIDDEN);
$this->playlistService->removeSongsFromPlaylist($playlist, $request->songs);
2022-07-27 08:49:33 +00:00
return response()->noContent();
2022-07-04 10:39:02 +00:00
}
2022-06-10 10:47:46 +00:00
}