Merge pull request #71 from Poniverse/feature/track-removals

Feature/track removals
This commit is contained in:
Adam Lavin 2016-03-20 21:56:47 +00:00
commit 547a8dc883
7 changed files with 99 additions and 4 deletions

View file

@ -0,0 +1,62 @@
<?php
/**
* Pony.fm - A community for pony fan music.
* Copyright (C) 2015 Peter Deltchev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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!']);
}
}

View file

@ -25,6 +25,7 @@ use Poniverse\Ponyfm\Commands\AddTrackToPlaylistCommand;
use Poniverse\Ponyfm\Commands\CreatePlaylistCommand; use Poniverse\Ponyfm\Commands\CreatePlaylistCommand;
use Poniverse\Ponyfm\Commands\DeletePlaylistCommand; use Poniverse\Ponyfm\Commands\DeletePlaylistCommand;
use Poniverse\Ponyfm\Commands\EditPlaylistCommand; use Poniverse\Ponyfm\Commands\EditPlaylistCommand;
use Poniverse\Ponyfm\Commands\RemoveTrackFromPlaylistCommand;
use Poniverse\Ponyfm\Http\Controllers\ApiControllerBase; use Poniverse\Ponyfm\Http\Controllers\ApiControllerBase;
use Poniverse\Ponyfm\Models\Image; use Poniverse\Ponyfm\Models\Image;
use Poniverse\Ponyfm\Models\Playlist; use Poniverse\Ponyfm\Models\Playlist;
@ -56,6 +57,11 @@ class PlaylistsController extends ApiControllerBase
return $this->execute(new AddTrackToPlaylistCommand($id, Input::get('track_id'))); 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() public function getIndex()
{ {
$page = 1; $page = 1;

View file

@ -117,6 +117,7 @@ Route::group(['prefix' => 'api/web'], function() {
Route::post('/playlists/delete/{id}', 'Api\Web\PlaylistsController@postDelete'); Route::post('/playlists/delete/{id}', 'Api\Web\PlaylistsController@postDelete');
Route::post('/playlists/edit/{id}', 'Api\Web\PlaylistsController@postEdit'); Route::post('/playlists/edit/{id}', 'Api\Web\PlaylistsController@postEdit');
Route::post('/playlists/{id}/add-track', 'Api\Web\PlaylistsController@postAddTrack'); 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+'); Route::post('/comments/{type}/{id}', 'Api\Web\CommentsController@postCreate')->where('id', '\d+');

View file

@ -12,6 +12,9 @@
<a pfm-eat-click class="icon-favourite" href="#" ng-click="toggleFavourite(track)" ng-if="::auth.isLogged"> <a pfm-eat-click class="icon-favourite" href="#" ng-click="toggleFavourite(track)" ng-if="::auth.isLogged">
<i class="icon-star-empty"></i> <i class="icon-star-empty"></i>
</a> </a>
<a pfm-eat-click href="#" ng-click="removeFromPlaylist(track)" ng-if="::canModifyPlaylist()">
<i class="icon-trash"></i>
</a>
</div> </div>
<a class="info" ng-href="{{::track.url}}"> <a class="info" ng-href="{{::track.url}}">
<span class="title">{{::track.title}}</span> <span class="title">{{::track.title}}</span>

View file

@ -57,7 +57,7 @@
</div> </div>
<h2>Tracks</h2> <h2>Tracks</h2>
<pfm-tracks-list tracks="::playlist.tracks"></pfm-tracks-list> <pfm-tracks-list tracks="::playlist.tracks" playlist="::playlist"></pfm-tracks-list>
<pfm-comments type="playlist" resource="::playlist"></pfm-comments> <pfm-comments type="playlist" resource="::playlist"></pfm-comments>
</div> </div>

View file

@ -19,14 +19,30 @@ module.exports = angular.module('ponyfm').directive 'pfmTracksList', () ->
templateUrl: '/templates/directives/tracks-list.html' templateUrl: '/templates/directives/tracks-list.html'
replace: true replace: true
scope: scope:
tracks: '=tracks', playlist: '='
tracks: '=tracks'
class: '@class' class: '@class'
controller: [ controller: [
'$scope', 'favourites', 'player', 'auth' '$dialog', '$scope', 'favourites', 'player', 'playlists', 'auth'
($scope, favourites, player, auth) -> ($dialog, $scope, favourites, player, playlists, auth) ->
$scope.auth = auth.data $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) -> $scope.toggleFavourite = (track) ->
favourites.toggle('track', track.id).done (res) -> favourites.toggle('track', track.id).done (res) ->
track.user_data.is_favourited = res.is_favourited track.user_data.is_favourited = res.is_favourited

View file

@ -68,6 +68,13 @@ module.exports = angular.module('ponyfm').factory('playlists', [
def 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: () -> refresh: () ->
if auth.data.isLogged if auth.data.isLogged
$.getJSON('/api/web/playlists/pinned') $.getJSON('/api/web/playlists/pinned')