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

116 lines
2.6 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\PlaylistSyncRequest;
2015-12-14 13:22:39 +00:00
use App\Models\Playlist;
2017-06-04 01:30:45 +00:00
use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Http\JsonResponse;
2015-12-14 13:22:39 +00:00
use Illuminate\Http\Request;
2015-12-13 04:42:28 +00:00
class PlaylistController extends Controller
{
2016-01-26 06:32:29 +00:00
/**
* Gets all playlists by the current user.
2016-03-06 04:11:28 +00:00
*
2017-06-04 01:30:45 +00:00
* @return JsonResponse
2016-01-26 06:32:29 +00:00
*/
public function index()
{
return response()->json(Playlist::byCurrentUser()->orderBy('name')->with('songs')->get());
}
2015-12-13 04:42:28 +00:00
/**
* Create a new playlist.
*
* @param PlaylistStoreRequest $request
*
2017-06-04 01:30:45 +00:00
* @return JsonResponse
2015-12-13 04:42:28 +00:00
*/
public function store(PlaylistStoreRequest $request)
{
2017-05-02 03:23:10 +00:00
$playlist = $request->user()->playlists()->create($request->only('name'));
2017-04-29 02:55:41 +00:00
$playlist->songs()->sync((array) $request->songs);
2015-12-13 04:42:28 +00:00
2015-12-29 01:35:22 +00:00
$playlist->songs = $playlist->songs->pluck('id');
2015-12-13 04:42:28 +00:00
return response()->json($playlist);
}
/**
* Rename a playlist.
*
2017-06-04 01:31:59 +00:00
* @param Request $request
* @param Playlist $playlist
2015-12-13 04:42:28 +00:00
*
2017-06-04 01:30:45 +00:00
* @throws AuthorizationException
2016-08-03 10:42:39 +00:00
*
2017-06-04 01:30:45 +00:00
* @return JsonResponse
2015-12-13 04:42:28 +00:00
*/
public function update(Request $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'));
2015-12-13 04:42:28 +00:00
return response()->json($playlist);
}
/**
* Sync a playlist with songs.
* Any songs that are not populated here will be removed from the playlist.
*
* @param PlaylistSyncRequest $request
* @param Playlist $playlist
2016-08-03 10:42:39 +00:00
*
* @throws AuthorizationException
2017-12-09 20:10:55 +00:00
*
* @return JsonResponse
2015-12-13 04:42:28 +00:00
*/
public function sync(PlaylistSyncRequest $request, Playlist $playlist)
2015-12-13 04:42:28 +00:00
{
$this->authorize('owner', $playlist);
2015-12-13 04:42:28 +00:00
2016-11-18 05:38:25 +00:00
$playlist->songs()->sync((array) $request->songs);
2015-12-13 04:42:28 +00:00
return response()->json();
}
/**
* Get a playlist's all songs.
*
2017-12-02 16:07:53 +00:00
* @param Playlist $playlist
*
* @throws AuthorizationException
2017-12-09 20:10:55 +00:00
*
* @return JsonResponse
*/
public function getSongs(Playlist $playlist)
{
$this->authorize('owner', $playlist);
return response()->json($playlist->songs->pluck('id'));
}
2015-12-13 04:42:28 +00:00
/**
* Delete a playlist.
*
2015-12-15 10:35:46 +00:00
* @param Playlist $playlist
2015-12-13 04:42:28 +00:00
*
2017-06-04 01:30:45 +00:00
* @throws Exception
* @throws AuthorizationException
2016-08-03 10:42:39 +00:00
*
2017-06-04 01:30:45 +00:00
* @return JsonResponse
2015-12-13 04:42:28 +00:00
*/
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();
}
}