#20: Implemented the genre renaming tool.

This commit is contained in:
Peter Deltchev 2015-11-24 02:49:47 -08:00
parent 395a894bdd
commit 4c660fcb71
14 changed files with 366 additions and 11 deletions

View file

@ -0,0 +1,77 @@
<?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 Gate;
use Illuminate\Support\Str;
use Poniverse\Ponyfm\Genre;
use Validator;
class RenameGenreCommand extends CommandBase
{
/** @var Genre */
private $_genre;
private $_newName;
public function __construct($genreId, $newName)
{
$this->_genre = Genre::find($genreId);;
$this->_newName = $newName;
}
/**
* @return bool
*/
public function authorize()
{
return Gate::allows('rename', $this->_genre);
}
/**
* @throws \Exception
* @return CommandResponse
*/
public function execute()
{
$slug = Str::slug($this->_newName);
$rules = [
'name' => 'required|unique:genres,name,'.$this->_genre->id.'|max:50',
'slug' => 'required|unique:genres,slug,'.$this->_genre->id
];
$validator = Validator::make([
'name' => $this->_newName,
'slug' => $slug
], $rules);
if ($validator->fails()) {
return CommandResponse::fail($validator);
}
$this->_genre->name = $this->_newName;
$this->_genre->slug = $slug;
$this->_genre->save();
return CommandResponse::succeed(['message' => 'Genre renamed!']);
}
}

View file

@ -20,6 +20,8 @@
namespace Poniverse\Ponyfm; namespace Poniverse\Ponyfm;
use DB;
use Illuminate\Database\Eloquent\Relations\Relation;
use Poniverse\Ponyfm\Traits\SlugTrait; use Poniverse\Ponyfm\Traits\SlugTrait;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
@ -27,7 +29,37 @@ class Genre extends Model
{ {
protected $table = 'genres'; protected $table = 'genres';
protected $fillable = ['name', 'slug']; protected $fillable = ['name', 'slug'];
protected $appends = ['track_count'];
protected $hidden = ['trackCountRelation'];
public $timestamps = false; public $timestamps = false;
use SlugTrait; use SlugTrait;
public function tracks(){
return $this->hasMany(Track::class, 'genre_id');
}
/**
* "Dummy" relation to facilitate eager-loading of a genre's track count.
* This relationship should not be used directly.
*
* Inspired by: http://laravel.io/forum/05-03-2014-eloquent-get-count-relation?page=1#reply-6226
*
* @return Relation
*/
public function trackCountRelation() {
return $this->hasOne(Track::class)
->select(['genre_id', DB::raw('count(*) as track_count')])
->groupBy('genre_id');
}
/**
* Returns the number of tracks in this genre.
*
* @return int
*/
public function getTrackCountAttribute() {
return $this->trackCountRelation()->count();
}
} }

View file

@ -0,0 +1,51 @@
<?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\Http\Controllers\Api\Web;
use Input;
use Poniverse\Ponyfm\Commands\RenameGenreCommand;
use Poniverse\Ponyfm\Genre;
use Poniverse\Ponyfm\Http\Controllers\ApiControllerBase;
use Response;
class GenresController extends ApiControllerBase
{
public function getIndex()
{
$this->authorize('access-admin-area');
$genres = Genre::with(['trackCountRelation' => function($query) {
$query->withTrashed();
}])->get();
return Response::json([
'genres' => $genres->toArray()
], 200);
}
public function putRename($genreId)
{
$command = new RenameGenreCommand($genreId, Input::get('name'));
return $this->execute($command);
}
}

View file

@ -20,11 +20,12 @@
namespace Poniverse\Ponyfm\Http\Controllers; namespace Poniverse\Ponyfm\Http\Controllers;
use Poniverse\Ponyfm\Commands\CommandBase;
use Response; use Response;
abstract class ApiControllerBase extends Controller abstract class ApiControllerBase extends Controller
{ {
protected function execute($command) protected function execute(CommandBase $command)
{ {
if (!$command->authorize()) { if (!$command->authorize()) {
return $this->notAuthorized(); return $this->notAuthorized();

View file

@ -142,6 +142,11 @@ Route::group(['prefix' => 'api/web'], function() {
Route::get('/favourites/playlists', 'Api\Web\FavouritesController@getPlaylists'); Route::get('/favourites/playlists', 'Api\Web\FavouritesController@getPlaylists');
}); });
Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'can:access-admin-area']], function() {
Route::get('/genres', 'Api\Web\GenresController@getIndex');
Route::put('/genres/{id}', 'Api\Web\GenresController@putRename')->where('id', '\d+');
});
Route::post('/auth/logout', 'Api\Web\AuthController@postLogout'); Route::post('/auth/logout', 'Api\Web\AuthController@postLogout');
}); });

View file

@ -20,15 +20,12 @@
namespace Poniverse\Ponyfm\Policies; namespace Poniverse\Ponyfm\Policies;
use Poniverse\Ponyfm\Genre;
use Poniverse\Ponyfm\User;
class GenrePolicy class GenrePolicy
{ {
/** public function rename(User $user, Genre $genre) {
* Create a new policy instance. return $user->hasRole('admin');
*
* @return void
*/
public function __construct()
{
// stub class
} }
} }

View file

@ -1,3 +1,31 @@
<h1>Genre Editor</h1> <h1>Genre Editor</h1>
<p>This is a stub page!</p> <section class="genre-list stretch-to-bottom">
<table class="table">
<thead>
<th>Genre</th>
<th class="-status"></th>
<th># of tracks (including deleted)</th>
<th>Actions</th>
</thead>
<tr ng-repeat="genre in genres">
<td>
<input
type="text"
class="x-large"
ng-class="{'x-saving': genre.isSaving, 'x-error': genre.isError}"
ng-model="genre.name"
pfm-on-enter="renameGenre(genre)"
/>
<div class="alert alert-error" ng-show="genre.isError">
{{ genre.errorMessage }}
</div>
</td>
<td><i ng-show="genre.isSaving" class="icon-cog icon-spin icon-large"></i></td>
<td>{{ genre.track_count }}</td>
<td>
<button class="btn btn-warning" disabled>Merge&hellip;</button>
</td>
</tr>
</table>
</section>

View file

@ -232,7 +232,8 @@ module.config [
templateUrl: '/templates/admin/_layout.html' templateUrl: '/templates/admin/_layout.html'
state.state 'admin.genres', state.state 'admin.genres',
url: '/genres', url: '/genres'
controller: 'admin-genres'
templateUrl: '/templates/admin/genres.html' templateUrl: '/templates/admin/genres.html'
# Homepage # Homepage

View file

@ -0,0 +1,48 @@
# 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/>.
angular.module('ponyfm').controller 'admin-genres', [
'$scope', '$state', 'admin-genres'
($scope, $state, genres) ->
$scope.genres = {}
setGenres = (genres) ->
for genre in genres
genre.isSaving = false
genre.isError = false
$scope.genres[genre.id] = genre
genres.fetch().done setGenres
# Renames the given genre
$scope.renameGenre = (genre) ->
genre.isSaving = true
genres.rename(genre.id, genre.name)
.done (response)->
genre.isError = false
.fail (response)->
genre.errorMessage = response
genre.isError = true
.always (response)->
genre.isSaving = false
# Merges genre1 into genre2
mergeGenre = (genre1, genre2) ->
# stub method
]

View file

@ -0,0 +1,28 @@
# 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/>.
# This directive calls the given function when Enter is pressed in a
# standalone input field.
angular.module('ponyfm').directive 'pfmOnEnter', () ->
(scope, element, attrs) ->
element.bind("keyup", (event) ->
if (event.which is 13)
scope.$apply(()->
scope.$eval(attrs.pfmOnEnter)
)
event.preventDefault()
)

View file

@ -0,0 +1,45 @@
# 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/>.
angular.module('ponyfm').factory('admin-genres', [
'$rootScope', '$http'
($rootScope, $http) ->
def = null
genres = []
self =
fetch: () ->
url = '/api/web/admin/genres'
def = new $.Deferred()
$http.get(url).success (genres) ->
def.resolve(genres['genres'])
def.promise()
rename: (genre_id, new_name) ->
url = "/api/web/admin/genres/#{genre_id}"
def = new $.Deferred()
$http.put(url, {name: new_name})
.success (response)->
def.resolve(response)
.error (response)->
def.reject(response)
def.promise()
self
])

27
resources/assets/styles/admin.less vendored Normal file
View file

@ -0,0 +1,27 @@
/**
* 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/>.
*/
@import 'base/bootstrap/bootstrap';
@import 'mixins';
.genre-list {
.-status {
width: 30px;
}
}

View file

@ -23,6 +23,7 @@
@import 'mixins'; @import 'mixins';
@import 'layout'; @import 'layout';
@import 'account-content'; @import 'account-content';
@import 'admin';
@import 'components'; @import 'components';
@import 'forms'; @import 'forms';
@import 'animations'; @import 'animations';

View file

@ -68,6 +68,20 @@ input[type="text"], input[type="password"], input[type="date"], input[type="numb
&:focus { &:focus {
.box-shadow(none); .box-shadow(none);
} }
&.x-large {
font-size: 12pt;
padding: 0.5em;
height: 2em;
}
&.x-saving {
background: @pfm-light-grey;
}
&.x-error {
background: @btnDangerBackground;
}
} }
select { select {