diff --git a/app/Commands/RemoveTrackFromPlaylistCommand.php b/app/Commands/RemoveTrackFromPlaylistCommand.php new file mode 100644 index 00000000..bf77af64 --- /dev/null +++ b/app/Commands/RemoveTrackFromPlaylistCommand.php @@ -0,0 +1,62 @@ +. + */ + +namespace Poniverse\Ponyfm\Commands; + +use Poniverse\Ponyfm\Models\Playlist; +use Poniverse\Ponyfm\Models\Track; +use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\DB; + +class RemoveTrackFromPlaylistCommand extends CommandBase +{ + private $_track; + private $_playlist; + + public 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() + { + $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 113162c3..b86f288e 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\Models\Image; use Poniverse\Ponyfm\Models\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 cff6880d..fb907ea2 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -117,6 +117,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/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..64b04551 100644 --- a/resources/assets/scripts/app/directives/tracks-list.coffee +++ b/resources/assets/scripts/app/directives/tracks-list.coffee @@ -19,14 +19,30 @@ 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) -> + '$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) -> + $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) -> track.user_data.is_favourited = res.is_favourited diff --git a/resources/assets/scripts/app/services/playlists.coffee b/resources/assets/scripts/app/services/playlists.coffee index b1d137cb..b4114235 100644 --- a/resources/assets/scripts/app/services/playlists.coffee +++ b/resources/assets/scripts/app/services/playlists.coffee @@ -68,6 +68,13 @@ module.exports = 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')