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;
|
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
|
|
|
|
{
|
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)
|
|
|
|
{
|
2021-10-08 16:23:45 +00:00
|
|
|
$playlist = $this->playlistService->createPlaylist(
|
|
|
|
$request->name,
|
2022-07-29 06:47:10 +00:00
|
|
|
$this->user,
|
2021-10-08 16:23:45 +00:00
|
|
|
(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
|
|
|
|
2022-07-29 06:47:10 +00:00
|
|
|
return response()->noContent();
|
2015-12-13 04:42:28 +00:00
|
|
|
}
|
|
|
|
}
|