diff --git a/app/Commands/CreateAnnouncementCommand.php b/app/Commands/CreateAnnouncementCommand.php new file mode 100644 index 00000000..8ae614ab --- /dev/null +++ b/app/Commands/CreateAnnouncementCommand.php @@ -0,0 +1,70 @@ +. + */ + +namespace Poniverse\Ponyfm\Commands; + +use Gate; +use Poniverse\Ponyfm\Models\Announcement; +use Validator; + +class CreateAnnouncementCommand extends CommandBase +{ + /** @var Announcement */ + private $_announcementName; + + public function __construct($announcementName) + { + $this->_announcementName = $announcementName; + } + + /** + * @return bool + */ + public function authorize() + { + return Gate::allows('create-announcement'); + } + + /** + * @throws \Exception + * @return CommandResponse + */ + public function execute() + { + + $rules = [ + 'name' => 'required|max:50', + ]; + + $validator = Validator::make([ + 'name' => $this->_announcementName, + ], $rules); + + if ($validator->fails()) { + return CommandResponse::fail($validator); + } + + Announcement::create([ + 'title' => $this->_announcementName, + ]); + + return CommandResponse::succeed(['message' => 'Announcement created!']); + } +} diff --git a/app/Http/Controllers/AdminController.php b/app/Http/Controllers/AdminController.php index 821cfef4..c3c70505 100644 --- a/app/Http/Controllers/AdminController.php +++ b/app/Http/Controllers/AdminController.php @@ -53,4 +53,9 @@ class AdminController extends Controller { return View::make('shared.null'); } + + public function getAnnouncements() + { + return View::make('shared.null'); + } } diff --git a/app/Http/Controllers/Api/Web/AnnouncementsController.php b/app/Http/Controllers/Api/Web/AnnouncementsController.php index caf974f4..0ab43b9e 100644 --- a/app/Http/Controllers/Api/Web/AnnouncementsController.php +++ b/app/Http/Controllers/Api/Web/AnnouncementsController.php @@ -21,8 +21,10 @@ namespace Poniverse\Ponyfm\Http\Controllers\Api\Web; use Carbon\Carbon; +use Poniverse\Ponyfm\Commands\CreateAnnouncementCommand; use Poniverse\Ponyfm\Http\Controllers\Controller; use Poniverse\Ponyfm\Models\Announcement; +use Request; use Response; class AnnouncementsController extends Controller { @@ -42,4 +44,34 @@ class AnnouncementsController extends Controller { 200 ); } + + public function getAdminIndex() { + $this->authorize('access-admin-area'); + + $announcements = Announcement::orderBy('start_time', 'desc') + ->get(); + + return Response::json([ + 'announcements' => $announcements->toArray() + ], 200); + } + + public function getItemById($genreId) { + $this->authorize('access-admin-area'); + + $query = Announcement::where('id', '=', $genreId) + ->orderBy('start_time', 'desc'); + + $announcement = $query->first(); + + return Response::json( + ["announcement" => $announcement], + 200 + ); + } + + public function postCreate() { + $command = new CreateAnnouncementCommand(Request::get('name')); + return $this->execute($command); + } } diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php index 3b4b7835..aceaf21b 100644 --- a/app/Providers/AuthServiceProvider.php +++ b/app/Providers/AuthServiceProvider.php @@ -72,6 +72,10 @@ class AuthServiceProvider extends ServiceProvider return $user->hasRole('admin'); }); + Gate::define('create-announcement', function (User $user) { + return $user->hasRole('admin'); + }); + $this->registerPolicies(); } } diff --git a/public/templates/admin/_layout.html b/public/templates/admin/_layout.html index f94c5b84..3ee9d07f 100644 --- a/public/templates/admin/_layout.html +++ b/public/templates/admin/_layout.html @@ -4,6 +4,7 @@
  • Genres
  • Show Songs
  • Users
  • +
  • Announcements
  • diff --git a/public/templates/admin/announcements.html b/public/templates/admin/announcements.html new file mode 100644 index 00000000..abd03e80 --- /dev/null +++ b/public/templates/admin/announcements.html @@ -0,0 +1,5 @@ +
    +
    + {{ item.id }}: {{ item.title }} +
    +
    diff --git a/resources/assets/scripts/app/app.coffee b/resources/assets/scripts/app/app.coffee index 2ddf4f7e..f828ac90 100644 --- a/resources/assets/scripts/app/app.coffee +++ b/resources/assets/scripts/app/app.coffee @@ -293,6 +293,16 @@ ponyfm.config [ controller: 'admin-users' templateUrl: '/templates/admin/users.html' + state.state 'admin.announcements', + url: '/announcements' + controller: 'admin-announcements' + templateUrl: '/templates/admin/announcements.html' + + state.state 'admin.announcement', + url: '/announcements/{id:[^\-]+}-{slug}' + templateUrl: '/templates/admin/announcement-show.html' + controller: 'admin-announcement-edit' + # Homepage if window.pfm.auth.isLogged diff --git a/resources/assets/scripts/app/controllers/admin-announcements.coffee b/resources/assets/scripts/app/controllers/admin-announcements.coffee new file mode 100644 index 00000000..bd068a5a --- /dev/null +++ b/resources/assets/scripts/app/controllers/admin-announcements.coffee @@ -0,0 +1,23 @@ +# Pony.fm - A community for pony fan music. +# Copyright (C) 2016 Josef Citrine +# +# 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 . + +module.exports = angular.module('ponyfm').controller "admin-announcements", [ + '$scope', 'announcements' + ($scope, announcements) -> + announcements.getAdminList().done (ann) -> + $scope.announcements = ann + console.log $scope.announcements +] diff --git a/resources/assets/scripts/app/services/announcements.coffee b/resources/assets/scripts/app/services/announcements.coffee index 0a1d4b18..7bd2a3e6 100644 --- a/resources/assets/scripts/app/services/announcements.coffee +++ b/resources/assets/scripts/app/services/announcements.coffee @@ -28,5 +28,11 @@ module.exports = angular.module('ponyfm').factory('announcements', [ def.resolve(announcementResponse.announcement) def.promise() + getAdminList: () -> + def = new $.Deferred() + $http.get('/api/web/admin/announcements').success (announcementResponse) -> + def.resolve(announcementResponse.announcements) + def.promise() + self ]) diff --git a/routes/web.php b/routes/web.php index d56eed27..973653ed 100644 --- a/routes/web.php +++ b/routes/web.php @@ -193,6 +193,12 @@ Route::group(['prefix' => 'api/web'], function () { Route::get('/tracks', 'Api\Web\TracksController@getAllTracks'); Route::get('/tracks/unclassified', 'Api\Web\TracksController@getClassifierQueue'); + + Route::get('/announcements', 'Api\Web\AnnouncementsController@getAdminIndex'); + Route::get('/announcements/{id}', 'Api\Web\AnnouncementsController@getItemById')->where('id', '\d+'); + Route::post('/announcements', 'Api\Web\AnnouncementsController@postCreate'); + Route::put('/announcements/{id}', 'Api\Web\AnnouncementsController@putUpdate')->where('id', '\d+'); + Route::delete('/announcements/{id}', 'Api\Web\AnnouncementsController@deleteItem')->where('id', '\d+'); }); Route::post('/auth/logout', 'Api\Web\AuthController@postLogout'); @@ -205,6 +211,7 @@ Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'can:access-admin-ar Route::get('/tracks/unclassified', 'AdminController@getClassifierQueue'); Route::get('/show-songs', 'AdminController@getShowSongs'); Route::get('/users', 'AdminController@getUsers'); + Route::get('/announcements', 'AdminController@getAnnouncements'); Route::get('/', 'AdminController@getIndex'); });