From dfefb2ad81e09ad5ffc7984c5238f2331fced2ff Mon Sep 17 00:00:00 2001 From: Zeusking19 Date: Mon, 28 Dec 2015 14:31:28 +0000 Subject: [PATCH] Implement server-side track removal from playlist --- .../RemoveTrackFromPlaylistCommand.php | 63 +++++++++++++++++++ .../Api/Web/PlaylistsController.php | 6 ++ app/Http/routes.php | 1 + .../scripts/app/services/playlists.coffee | 7 +++ 4 files changed, 77 insertions(+) create mode 100644 app/Commands/RemoveTrackFromPlaylistCommand.php diff --git a/app/Commands/RemoveTrackFromPlaylistCommand.php b/app/Commands/RemoveTrackFromPlaylistCommand.php new file mode 100644 index 00000000..e1d25315 --- /dev/null +++ b/app/Commands/RemoveTrackFromPlaylistCommand.php @@ -0,0 +1,63 @@ +. + */ + +namespace Poniverse\Ponyfm\Commands; + +use Poniverse\Ponyfm\Playlist; +use Poniverse\Ponyfm\Track; +use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\DB; + +class RemoveTrackFromPlaylistCommand extends CommandBase +{ + private $_track; + private $_playlist; + + function __construct($playlistId, $trackId) + { + $this->_playlist = Playlist::find($playlistId); + $this->_track = Track::find($trackId); + } + + /** + * @return bool + */ + public function authorize() + { + $user = Auth::user(); + + return $user != null && $this->_playlist && $this->_track && $this->_playlist->user_id == $user->id; + } + + /** + * @throws \Exception + * @return CommandResponse + */ + public function execute() + { +// $songIndex = $this->_playlist->tracks()->count() - 1; + $this->_playlist->tracks()->detach($this->_track); + Playlist::whereId($this->_playlist->id)->update([ + 'track_count' => DB::raw('(SELECT COUNT(id) FROM playlist_track WHERE playlist_id = ' . $this->_playlist->id . ')') + ]); + + return CommandResponse::succeed(['message' => 'Track removed!']); + } +} diff --git a/app/Http/Controllers/Api/Web/PlaylistsController.php b/app/Http/Controllers/Api/Web/PlaylistsController.php index aac920b3..baffd566 100644 --- a/app/Http/Controllers/Api/Web/PlaylistsController.php +++ b/app/Http/Controllers/Api/Web/PlaylistsController.php @@ -25,6 +25,7 @@ use Poniverse\Ponyfm\Commands\AddTrackToPlaylistCommand; use Poniverse\Ponyfm\Commands\CreatePlaylistCommand; use Poniverse\Ponyfm\Commands\DeletePlaylistCommand; use Poniverse\Ponyfm\Commands\EditPlaylistCommand; +use Poniverse\Ponyfm\Commands\RemoveTrackFromPlaylistCommand; use Poniverse\Ponyfm\Http\Controllers\ApiControllerBase; use Poniverse\Ponyfm\Image; use Poniverse\Ponyfm\Playlist; @@ -56,6 +57,11 @@ class PlaylistsController extends ApiControllerBase return $this->execute(new AddTrackToPlaylistCommand($id, Input::get('track_id'))); } + public function postRemoveTrack($id) + { + return $this->execute(new RemoveTrackFromPlaylistCommand($id, Input::get('track_id'))); + } + public function getIndex() { $page = 1; diff --git a/app/Http/routes.php b/app/Http/routes.php index 9289f759..0ab6299d 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -119,6 +119,7 @@ Route::group(['prefix' => 'api/web'], function() { Route::post('/playlists/delete/{id}', 'Api\Web\PlaylistsController@postDelete'); Route::post('/playlists/edit/{id}', 'Api\Web\PlaylistsController@postEdit'); Route::post('/playlists/{id}/add-track', 'Api\Web\PlaylistsController@postAddTrack'); + Route::post('/playlists/{id}/remove-track', 'Api\Web\PlaylistsController@postRemoveTrack'); Route::post('/comments/{type}/{id}', 'Api\Web\CommentsController@postCreate')->where('id', '\d+'); diff --git a/resources/assets/scripts/app/services/playlists.coffee b/resources/assets/scripts/app/services/playlists.coffee index afbd14ab..151dbacf 100644 --- a/resources/assets/scripts/app/services/playlists.coffee +++ b/resources/assets/scripts/app/services/playlists.coffee @@ -68,6 +68,13 @@ angular.module('ponyfm').factory('playlists', [ def + removeTrackFromPlaylist: (playlistId, trackId) -> + def = new $.Deferred() + $http.post('/api/web/playlists/' + playlistId + '/remove-track', {track_id: trackId}).success (res) -> + def.resolve(res) + + def + refresh: () -> if auth.data.isLogged $.getJSON('/api/web/playlists/pinned')