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

61 lines
1.5 KiB
PHP
Raw Normal View History

2015-12-13 04:42:28 +00:00
<?php
namespace App\Http\Controllers\API;
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;
2015-12-14 13:22:39 +00:00
use App\Models\Playlist;
2020-09-06 18:21:39 +00:00
use App\Models\User;
2018-08-29 06:15:11 +00:00
use App\Repositories\PlaylistRepository;
use App\Services\PlaylistService;
2020-09-06 18:21:39 +00:00
use Illuminate\Contracts\Auth\Authenticatable;
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 PlaylistRepository $playlistRepository,
private PlaylistService $playlistService,
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()
{
2018-08-29 06:15:11 +00:00
return response()->json($this->playlistRepository->getAllByCurrentUser());
2016-01-26 06:32:29 +00:00
}
2015-12-13 04:42:28 +00:00
public function store(PlaylistStoreRequest $request)
{
$playlist = $this->playlistService->createPlaylist(
$request->name,
2022-07-29 06:47:10 +00:00
$this->user,
(array) $request->songs,
$request->rules
);
2018-11-25 21:21:46 +00:00
$playlist->songs = $playlist->songs->pluck('id')->toArray();
2018-11-25 21:21:46 +00:00
return response()->json($playlist);
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('owner', $playlist);
2015-12-13 04:42:28 +00:00
$playlist->update($request->only('name', 'rules'));
2015-12-13 04:42:28 +00:00
return response()->json($playlist);
}
public function destroy(Playlist $playlist)
2015-12-13 04:42:28 +00:00
{
$this->authorize('owner', $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
}
}