From dfefb2ad81e09ad5ffc7984c5238f2331fced2ff Mon Sep 17 00:00:00 2001 From: Zeusking19 Date: Mon, 28 Dec 2015 14:31:28 +0000 Subject: [PATCH 1/5] 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') From 4a5c2418eb3fc9b6d8a4b13f08fe36199caa606c Mon Sep 17 00:00:00 2001 From: Mihail-K Date: Sun, 20 Mar 2016 16:05:57 -0400 Subject: [PATCH 2/5] Update references to models. --- app/Commands/RemoveTrackFromPlaylistCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Commands/RemoveTrackFromPlaylistCommand.php b/app/Commands/RemoveTrackFromPlaylistCommand.php index e1d25315..ceac11d1 100644 --- a/app/Commands/RemoveTrackFromPlaylistCommand.php +++ b/app/Commands/RemoveTrackFromPlaylistCommand.php @@ -20,8 +20,8 @@ namespace Poniverse\Ponyfm\Commands; -use Poniverse\Ponyfm\Playlist; -use Poniverse\Ponyfm\Track; +use Poniverse\Ponyfm\Models\Playlist; +use Poniverse\Ponyfm\Models\Track; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\DB; From fb2997c2e95bfff8ec5a6b9adb3fd89f04d08c2d Mon Sep 17 00:00:00 2001 From: Mihail-K Date: Sun, 20 Mar 2016 16:27:22 -0400 Subject: [PATCH 3/5] Add remove button for tracks in playlists. Resolves #3 --- public/templates/directives/tracks-list.html | 3 +++ public/templates/playlists/show.html | 2 +- .../scripts/app/directives/tracks-list.coffee | 15 ++++++++++++--- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/public/templates/directives/tracks-list.html b/public/templates/directives/tracks-list.html index dcb4416b..34a63b60 100644 --- a/public/templates/directives/tracks-list.html +++ b/public/templates/directives/tracks-list.html @@ -12,6 +12,9 @@ + + + {{::track.title}} diff --git a/public/templates/playlists/show.html b/public/templates/playlists/show.html index 84df2af8..23f59895 100644 --- a/public/templates/playlists/show.html +++ b/public/templates/playlists/show.html @@ -57,7 +57,7 @@

Tracks

- + diff --git a/resources/assets/scripts/app/directives/tracks-list.coffee b/resources/assets/scripts/app/directives/tracks-list.coffee index d0d665aa..b32e357e 100644 --- a/resources/assets/scripts/app/directives/tracks-list.coffee +++ b/resources/assets/scripts/app/directives/tracks-list.coffee @@ -19,14 +19,23 @@ module.exports = angular.module('ponyfm').directive 'pfmTracksList', () -> templateUrl: '/templates/directives/tracks-list.html' replace: true scope: - tracks: '=tracks', + playlist: '=' + tracks: '=tracks' class: '@class' controller: [ - '$scope', 'favourites', 'player', 'auth' - ($scope, favourites, player, auth) -> + '$scope', 'favourites', 'player', 'playlists', 'auth' + ($scope, favourites, player, playlists, auth) -> $scope.auth = auth.data + $scope.canModifyPlaylist = -> + $scope.playlist and $scope.auth.isLogged and $scope.playlist.user.id == $scope.auth.user.id + + $scope.removeFromPlaylist = (track) -> + playlists.removeTrackFromPlaylist $scope.playlist?.id, track.id + .done -> + $scope.tracks = _.reject $scope.tracks, (t) -> t.id == track.id + $scope.toggleFavourite = (track) -> favourites.toggle('track', track.id).done (res) -> track.user_data.is_favourited = res.is_favourited From e7ed44d7de8fdbfed2ae512dcb81777bda7ce742 Mon Sep 17 00:00:00 2001 From: Mihail-K Date: Sun, 20 Mar 2016 16:42:58 -0400 Subject: [PATCH 4/5] Clean up loose code. --- app/Commands/RemoveTrackFromPlaylistCommand.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/Commands/RemoveTrackFromPlaylistCommand.php b/app/Commands/RemoveTrackFromPlaylistCommand.php index ceac11d1..bf77af64 100644 --- a/app/Commands/RemoveTrackFromPlaylistCommand.php +++ b/app/Commands/RemoveTrackFromPlaylistCommand.php @@ -30,7 +30,7 @@ class RemoveTrackFromPlaylistCommand extends CommandBase private $_track; private $_playlist; - function __construct($playlistId, $trackId) + public function __construct($playlistId, $trackId) { $this->_playlist = Playlist::find($playlistId); $this->_track = Track::find($trackId); @@ -52,7 +52,6 @@ class RemoveTrackFromPlaylistCommand extends CommandBase */ 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 . ')') From d8cb98b912df61d608e7765b1fcc5fe490f3f714 Mon Sep 17 00:00:00 2001 From: Mihail-K Date: Sun, 20 Mar 2016 17:41:54 -0400 Subject: [PATCH 5/5] Add confirmation dialog to track removal. --- .../scripts/app/directives/tracks-list.coffee | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/resources/assets/scripts/app/directives/tracks-list.coffee b/resources/assets/scripts/app/directives/tracks-list.coffee index b32e357e..64b04551 100644 --- a/resources/assets/scripts/app/directives/tracks-list.coffee +++ b/resources/assets/scripts/app/directives/tracks-list.coffee @@ -24,17 +24,24 @@ module.exports = angular.module('ponyfm').directive 'pfmTracksList', () -> class: '@class' controller: [ - '$scope', 'favourites', 'player', 'playlists', 'auth' - ($scope, favourites, player, playlists, auth) -> + '$dialog', '$scope', 'favourites', 'player', 'playlists', 'auth' + ($dialog, $scope, favourites, player, playlists, auth) -> $scope.auth = auth.data $scope.canModifyPlaylist = -> $scope.playlist and $scope.auth.isLogged and $scope.playlist.user.id == $scope.auth.user.id $scope.removeFromPlaylist = (track) -> - playlists.removeTrackFromPlaylist $scope.playlist?.id, track.id - .done -> - $scope.tracks = _.reject $scope.tracks, (t) -> t.id == track.id + $dialog.messageBox "Remove #{track.title} from playlist", + "Are you sure you want to delete \"#{track.title}\"?", [ + { result: 'ok', label: 'Yes', cssClass: 'btn-danger' }, + { result: 'cancel', label: 'No', cssClass: 'btn-primary' } + ] + .open().then (res) -> + return if res is 'cancel' + playlists.removeTrackFromPlaylist $scope.playlist?.id, track.id + .done -> + $scope.tracks = _.reject $scope.tracks, (t) -> t.id == track.id $scope.toggleFavourite = (track) -> favourites.toggle('track', track.id).done (res) ->