2015-12-13 04:42:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\API;
|
|
|
|
|
|
|
|
use App\Http\Requests\API\PlaylistStoreRequest;
|
2021-10-10 18:05:51 +00:00
|
|
|
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;
|
2021-10-08 16:23:45 +00:00
|
|
|
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;
|
2021-10-08 16:23:45 +00:00
|
|
|
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,
|
2021-10-08 16:23:45 +00:00
|
|
|
PlaylistService $playlistService,
|
2020-09-07 20:43:23 +00:00
|
|
|
?Authenticatable $currentUser
|
2020-09-06 21:20:42 +00:00
|
|
|
) {
|
2018-08-29 06:15:11 +00:00
|
|
|
$this->playlistRepository = $playlistRepository;
|
2021-10-08 16:23:45 +00:00
|
|
|
$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)
|
|
|
|
{
|
2021-10-08 16:23:45 +00:00
|
|
|
$playlist = $this->playlistService->createPlaylist(
|
|
|
|
$request->name,
|
|
|
|
$this->currentUser,
|
|
|
|
(array) $request->songs,
|
|
|
|
$request->rules
|
|
|
|
);
|
2018-11-25 21:21:46 +00:00
|
|
|
|
2021-10-08 16:23:45 +00:00
|
|
|
$playlist->songs = $playlist->songs->pluck('id')->toArray();
|
2018-11-25 21:21:46 +00:00
|
|
|
|
2021-10-08 16:23:45 +00:00
|
|
|
return response()->json($playlist);
|
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
|
|
|
{
|
2020-09-07 20:43:23 +00:00
|
|
|
$this->authorize('owner', $playlist);
|
2015-12-13 04:42:28 +00:00
|
|
|
|
2019-04-13 20:38:34 +00:00
|
|
|
$playlist->update($request->only('name', 'rules'));
|
2015-12-13 04:42:28 +00:00
|
|
|
|
|
|
|
return response()->json($playlist);
|
|
|
|
}
|
|
|
|
|
2015-12-14 16:27:26 +00:00
|
|
|
public function destroy(Playlist $playlist)
|
2015-12-13 04:42:28 +00:00
|
|
|
{
|
2015-12-14 16:27:26 +00:00
|
|
|
$this->authorize('owner', $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
|
|
|
|
|
|
|
return response()->json();
|
|
|
|
}
|
|
|
|
}
|