2015-12-13 04:42:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\API;
|
|
|
|
|
2022-11-27 15:29:29 +00:00
|
|
|
use App\Exceptions\PlaylistBothSongsAndRulesProvidedException;
|
2024-01-12 14:41:02 +00:00
|
|
|
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;
|
2021-10-10 18:05:51 +00:00
|
|
|
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;
|
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;
|
2021-10-08 16:23:45 +00:00
|
|
|
use App\Services\PlaylistService;
|
2022-11-27 15:29:29 +00:00
|
|
|
use App\Values\SmartPlaylistRuleGroupCollection;
|
2020-09-06 18:21:39 +00:00
|
|
|
use Illuminate\Contracts\Auth\Authenticatable;
|
2022-11-27 15:29:29 +00:00
|
|
|
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(
|
2024-04-18 14:36:28 +00:00
|
|
|
private readonly PlaylistService $playlistService,
|
|
|
|
private readonly PlaylistRepository $playlistRepository,
|
|
|
|
private readonly PlaylistFolderRepository $folderRepository,
|
|
|
|
private readonly ?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) {
|
2024-01-01 20:38:41 +00:00
|
|
|
$folder = $this->folderRepository->getOne($request->folder_id);
|
2023-06-05 21:46:41 +00:00
|
|
|
$this->authorize('own', $folder);
|
|
|
|
}
|
|
|
|
|
2022-11-27 15:29:29 +00:00
|
|
|
try {
|
|
|
|
$playlist = $this->playlistService->createPlaylist(
|
|
|
|
$request->name,
|
|
|
|
$this->user,
|
2023-06-05 21:46:41 +00:00
|
|
|
$folder,
|
2022-11-27 15:29:29 +00:00
|
|
|
Arr::wrap($request->songs),
|
2024-01-12 14:41:02 +00:00
|
|
|
$request->rules ? SmartPlaylistRuleGroupCollection::create(Arr::wrap($request->rules)) : null,
|
|
|
|
$request->own_songs_only && $request->rules && License::isPlus()
|
2022-11-27 15:29:29 +00:00
|
|
|
);
|
2018-11-25 21:21:46 +00:00
|
|
|
|
2023-06-05 21:46:41 +00:00
|
|
|
return PlaylistResource::make($playlist);
|
2022-11-27 15:29:29 +00:00
|
|
|
} catch (PlaylistBothSongsAndRulesProvidedException $e) {
|
|
|
|
throw ValidationException::withMessages(['songs' => [$e->getMessage()]]);
|
|
|
|
}
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
2021-10-10 18:05:51 +00:00
|
|
|
public function update(PlaylistUpdateRequest $request, Playlist $playlist)
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2022-08-10 14:56:01 +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) {
|
2024-01-01 20:38:41 +00:00
|
|
|
$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,
|
2024-01-12 14:41:02 +00:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
2015-12-14 16:27:26 +00:00
|
|
|
public function destroy(Playlist $playlist)
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2022-08-10 14:56:01 +00:00
|
|
|
$this->authorize('own', $playlist);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2015-12-14 16:27:26 +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
|
|
|
}
|
|
|
|
}
|