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

68 lines
1.7 KiB
PHP
Raw Normal View History

2015-12-13 04:42:28 +00:00
<?php
namespace App\Http\Controllers\API;
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
{
2021-06-05 10:47:56 +00:00
private PlaylistRepository $playlistRepository;
private PlaylistService $playlistService;
2018-08-29 06:15:11 +00:00
2020-09-06 18:21:39 +00:00
/** @var User */
2021-06-05 10:47:56 +00:00
private ?Authenticatable $currentUser;
2020-09-06 18:21:39 +00:00
public function __construct(
PlaylistRepository $playlistRepository,
PlaylistService $playlistService,
?Authenticatable $currentUser
2020-09-06 21:20:42 +00:00
) {
2018-08-29 06:15:11 +00:00
$this->playlistRepository = $playlistRepository;
$this->playlistService = $playlistService;
2020-09-06 18:21:39 +00:00
$this->currentUser = $currentUser;
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,
$this->currentUser,
(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
return response()->json();
}
}