diff --git a/app/Commands/CreateGenreCommand.php b/app/Commands/CreateGenreCommand.php new file mode 100644 index 00000000..23e7d2df --- /dev/null +++ b/app/Commands/CreateGenreCommand.php @@ -0,0 +1,75 @@ +. + */ + +namespace Poniverse\Ponyfm\Commands; + +use Gate; +use Illuminate\Support\Str; +use Poniverse\Ponyfm\Models\Genre; +use Validator; + +class CreateGenreCommand extends CommandBase +{ + /** @var Genre */ + private $_genreName; + + public function __construct($genreName) + { + $this->_genreName = $genreName; + } + + /** + * @return bool + */ + public function authorize() + { + return Gate::allows('create-genre'); + } + + /** + * @throws \Exception + * @return CommandResponse + */ + public function execute() + { + $slug = Str::slug($this->_genreName); + + $rules = [ + 'name' => 'required|unique:genres,name,NULL,id,deleted_at,NULL|max:50', + 'slug' => 'required|unique:genres,slug,NULL,id,deleted_at,NULL' + ]; + + $validator = Validator::make([ + 'name' => $this->_genreName, + 'slug' => $slug + ], $rules); + + if ($validator->fails()) { + return CommandResponse::fail($validator); + } + + Genre::create([ + 'name' => $this->_genreName, + 'slug' => $slug + ]); + + return CommandResponse::succeed(['message' => 'Genre created!']); + } +} diff --git a/app/Http/Controllers/Api/Web/GenresController.php b/app/Http/Controllers/Api/Web/GenresController.php index 1d00b956..0de4fb38 100644 --- a/app/Http/Controllers/Api/Web/GenresController.php +++ b/app/Http/Controllers/Api/Web/GenresController.php @@ -21,6 +21,7 @@ namespace Poniverse\Ponyfm\Http\Controllers\Api\Web; use Input; +use Poniverse\Ponyfm\Commands\CreateGenreCommand; use Poniverse\Ponyfm\Commands\DeleteGenreCommand; use Poniverse\Ponyfm\Commands\RenameGenreCommand; use Poniverse\Ponyfm\Models\Genre; @@ -45,6 +46,11 @@ class GenresController extends ApiControllerBase ], 200); } + public function postCreate() + { + $command = new CreateGenreCommand(Input::get('name')); + return $this->execute($command); + } public function putRename($genreId) { diff --git a/app/Http/routes.php b/app/Http/routes.php index 64842676..ec54763b 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -153,6 +153,7 @@ Route::group(['prefix' => 'api/web'], function() { Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'can:access-admin-area']], function() { Route::get('/genres', 'Api\Web\GenresController@getIndex'); + Route::post('/genres', 'Api\Web\GenresController@postCreate'); Route::put('/genres/{id}', 'Api\Web\GenresController@putRename')->where('id', '\d+'); Route::delete('/genres/{id}', 'Api\Web\GenresController@deleteGenre')->where('id', '\d+'); }); diff --git a/app/Models/Genre.php b/app/Models/Genre.php index 1d1f3914..967bf7b3 100644 --- a/app/Models/Genre.php +++ b/app/Models/Genre.php @@ -49,8 +49,6 @@ class Genre extends Model protected $appends = ['track_count', 'url']; protected $hidden = ['trackCountRelation']; - public $timestamps = false; - use SlugTrait, SoftDeletes, RevisionableTrait; public function tracks(){ diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php index 820d57e3..96b1b6c1 100644 --- a/app/Providers/AuthServiceProvider.php +++ b/app/Providers/AuthServiceProvider.php @@ -52,6 +52,10 @@ class AuthServiceProvider extends ServiceProvider return $user->hasRole('admin'); }); + $gate->define('create-genre', function(User $user) { + return $user->hasRole('admin'); + }); + $this->registerPolicies($gate); } } diff --git a/database/migrations/2016_01_06_123513_add_genre_timestamps.php b/database/migrations/2016_01_06_123513_add_genre_timestamps.php new file mode 100644 index 00000000..207e50b2 --- /dev/null +++ b/database/migrations/2016_01_06_123513_add_genre_timestamps.php @@ -0,0 +1,49 @@ +. + */ + +use Illuminate\Database\Schema\Blueprint; +use Illuminate\Database\Migrations\Migration; + +class AddGenreTimestamps extends Migration +{ + /** + * Run the migrations. + * + * @return void + */ + public function up() + { + Schema::table('genres', function(Blueprint $table) { + $table->nullableTimestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('genres', function(Blueprint $table) { + $table->dropTimestamps(); + }); + } +} diff --git a/public/templates/admin/genres.html b/public/templates/admin/genres.html index acb709e5..b307d26f 100644 --- a/public/templates/admin/genres.html +++ b/public/templates/admin/genres.html @@ -1,33 +1,55 @@
Genre | -- | # of tracks (including deleted) | -Actions | - -
---|---|---|---|
-
-
- {{ genre.errorMessage }}
-
- |
- - | {{ genre.track_count }} | -- - - - | -
Enter a genre name and press enter to create it!
+ + +Genre | ++ | # of tracks (including deleted) | +Actions | + +
---|---|---|---|
+
+
+ {{ genre.errorMessage }}
+
+ |
+ + | {{ genre.track_count }} | ++ + + + | +