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

96 lines
3 KiB
PHP
Raw Normal View History

2015-12-13 04:42:28 +00:00
<?php
namespace App\Http\Controllers\API;
use App\Exceptions\PlaylistBothSongsAndRulesProvidedException;
use App\Facades\License;
2022-07-29 06:47:10 +00:00
use App\Http\Controllers\Controller;
2015-12-13 04:42:28 +00:00
use App\Http\Requests\API\PlaylistStoreRequest;
use App\Http\Requests\API\PlaylistUpdateRequest;
2023-06-05 21:46:41 +00:00
use App\Http\Resources\PlaylistResource;
2015-12-14 13:22:39 +00:00
use App\Models\Playlist;
2023-06-05 21:46:41 +00:00
use App\Models\PlaylistFolder;
2020-09-06 18:21:39 +00:00
use App\Models\User;
2023-06-05 21:46:41 +00:00
use App\Repositories\PlaylistFolderRepository;
2024-01-18 11:13:05 +00:00
use App\Repositories\PlaylistRepository;
use App\Services\PlaylistService;
use App\Values\SmartPlaylistRuleGroupCollection;
2020-09-06 18:21:39 +00:00
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Support\Arr;
use Illuminate\Validation\ValidationException;
2015-12-13 04:42:28 +00:00
class PlaylistController extends Controller
{
2022-07-29 06:47:10 +00:00
/** @param User $user */
2020-09-06 18:21:39 +00:00
public function __construct(
2022-07-29 06:47:10 +00:00
private PlaylistService $playlistService,
2024-01-18 11:13:05 +00:00
private PlaylistRepository $playlistRepository,
2023-06-05 21:46:41 +00:00
private PlaylistFolderRepository $folderRepository,
2022-07-29 06:47:10 +00:00
private ?Authenticatable $user
2020-09-06 21:20:42 +00:00
) {
2018-08-29 06:15:11 +00:00
}
2016-01-26 06:32:29 +00:00
public function index()
{
2024-01-18 11:13:05 +00:00
return PlaylistResource::collection($this->playlistRepository->getAllAccessibleByUser($this->user));
2016-01-26 06:32:29 +00:00
}
2015-12-13 04:42:28 +00:00
public function store(PlaylistStoreRequest $request)
{
2023-06-05 21:46:41 +00:00
$folder = null;
if ($request->folder_id) {
/** @var PlaylistFolder $folder */
$folder = $this->folderRepository->getOne($request->folder_id);
2023-06-05 21:46:41 +00:00
$this->authorize('own', $folder);
}
try {
$playlist = $this->playlistService->createPlaylist(
$request->name,
$this->user,
2023-06-05 21:46:41 +00:00
$folder,
Arr::wrap($request->songs),
$request->rules ? SmartPlaylistRuleGroupCollection::create(Arr::wrap($request->rules)) : null,
$request->own_songs_only && $request->rules && License::isPlus()
);
2018-11-25 21:21:46 +00:00
2023-06-05 21:46:41 +00:00
return PlaylistResource::make($playlist);
} catch (PlaylistBothSongsAndRulesProvidedException $e) {
throw ValidationException::withMessages(['songs' => [$e->getMessage()]]);
}
2015-12-13 04:42:28 +00:00
}
public function update(PlaylistUpdateRequest $request, Playlist $playlist)
2015-12-13 04:42:28 +00:00
{
$this->authorize('own', $playlist);
2015-12-13 04:42:28 +00:00
2023-06-05 21:46:41 +00:00
$folder = null;
2015-12-13 04:42:28 +00:00
2023-06-05 21:46:41 +00:00
if ($request->folder_id) {
/** @var PlaylistFolder $folder */
$folder = $this->folderRepository->getOne($request->folder_id);
2023-06-05 21:46:41 +00:00
$this->authorize('own', $folder);
}
return PlaylistResource::make(
$this->playlistService->updatePlaylist(
$playlist,
$request->name,
$folder,
$request->rules ? SmartPlaylistRuleGroupCollection::create(Arr::wrap($request->rules)) : null,
$request->own_songs_only && $request->rules && License::isPlus()
2023-06-05 21:46:41 +00:00
)
);
2015-12-13 04:42:28 +00:00
}
public function destroy(Playlist $playlist)
2015-12-13 04:42:28 +00:00
{
$this->authorize('own', $playlist);
2015-12-13 04:42:28 +00:00
$playlist->delete();
2015-12-13 04:42:28 +00:00
2022-07-29 06:47:10 +00:00
return response()->noContent();
2015-12-13 04:42:28 +00:00
}
}