From 11a20c5be971d97f4a4f575f91b706791c1893a9 Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Sun, 26 Apr 2015 23:29:17 +0200 Subject: Competitions; CompetitionTypes; Participants; better routing; administrators; ... --- src/CamilStaps/BotleaguesApi/Bot.php | 3 +- .../BotleaguesApi/BotleaguesApiServiceProvider.php | 19 +++++++-- src/CamilStaps/BotleaguesApi/Competition.php | 12 ++++++ src/CamilStaps/BotleaguesApi/CompetitionType.php | 13 +++++++ src/CamilStaps/BotleaguesApi/Game.php | 11 ++++++ src/CamilStaps/BotleaguesApi/Participant.php | 12 ++++++ src/CamilStaps/BotleaguesApi/User.php | 4 ++ src/controllers/BotController.php | 18 +++++++-- src/controllers/CompetitionController.php | 36 +++++++++++++++++ src/controllers/CompetitionTypeController.php | 36 +++++++++++++++++ src/controllers/GameController.php | 36 +++++++++++++++++ src/controllers/ParticipantController.php | 36 +++++++++++++++++ src/controllers/UserController.php | 9 ++--- src/filters.php | 15 ++++++++ ...015_02_23_184402_botleaguesapi-create_users.php | 1 + ...2015_botleaguesapi-create_competition_types.php | 34 ++++++++++++++++ ...26_192025_botleaguesapi-create_competitions.php | 41 ++++++++++++++++++++ ...26_192035_botleaguesapi-create_participants.php | 36 +++++++++++++++++ src/routes.php | 45 +++++++++++++++++++++- 19 files changed, 401 insertions(+), 16 deletions(-) create mode 100644 src/CamilStaps/BotleaguesApi/Competition.php create mode 100644 src/CamilStaps/BotleaguesApi/CompetitionType.php create mode 100644 src/CamilStaps/BotleaguesApi/Game.php create mode 100644 src/CamilStaps/BotleaguesApi/Participant.php create mode 100644 src/controllers/CompetitionController.php create mode 100644 src/controllers/CompetitionTypeController.php create mode 100644 src/controllers/GameController.php create mode 100644 src/controllers/ParticipantController.php create mode 100644 src/migrations/2015_04_26_192015_botleaguesapi-create_competition_types.php create mode 100644 src/migrations/2015_04_26_192025_botleaguesapi-create_competitions.php create mode 100644 src/migrations/2015_04_26_192035_botleaguesapi-create_participants.php (limited to 'src') diff --git a/src/CamilStaps/BotleaguesApi/Bot.php b/src/CamilStaps/BotleaguesApi/Bot.php index f45ada7..04fa2c7 100644 --- a/src/CamilStaps/BotleaguesApi/Bot.php +++ b/src/CamilStaps/BotleaguesApi/Bot.php @@ -5,6 +5,7 @@ use Illuminate\Database\Eloquent\Model; class Bot extends Model { - + protected $table = 'bots'; + protected $fillable = ['userId', 'gameId', 'title']; } \ No newline at end of file diff --git a/src/CamilStaps/BotleaguesApi/BotleaguesApiServiceProvider.php b/src/CamilStaps/BotleaguesApi/BotleaguesApiServiceProvider.php index 012b5cf..22ec44c 100644 --- a/src/CamilStaps/BotleaguesApi/BotleaguesApiServiceProvider.php +++ b/src/CamilStaps/BotleaguesApi/BotleaguesApiServiceProvider.php @@ -4,6 +4,7 @@ namespace CamilStaps\BotleaguesApi; use \Illuminate\Database\Eloquent\ModelNotFoundException; use \Illuminate\Support\ServiceProvider; use \Illuminate\Support\Facades\App; +use \Illuminate\Support\Facades\Config; use \Dingo\Api\Facade\API; use Response; @@ -27,15 +28,27 @@ class BotleaguesApiServiceProvider extends ServiceProvider { include __DIR__ . '/../../filters.php'; include __DIR__ . '/../../routes.php'; + + App::fatal(function($e) { + return Response::make( + ['error' => Config::get('app.debug') ? $e->getMessage() : "Internal error"], + 500); + }); API::error(function(ModelNotFoundException $e) { - return Response::make(['error' => 'Resource not found'], 404); + return Response::make( + ['error' => 'Resource not found'], + 404); }); API::error(function(\Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException $e) { - return Response::make(['error' => $e->getMessage() == null ? 'Access denied' : $e->getMessage()], 404); + return Response::make( + ['error' => !Config::get('app.debug') || empty($e->getMessage()) ? 'Access denied' : $e->getMessage()], + 404); }); API::error(function(\Exception $e) { - return Response::make(['error' => $e->getMessage()], 500); + return Response::make( + ['error' => Config::get('app.debug') ? $e->getMessage() : "Internal error"], + 500); }); } diff --git a/src/CamilStaps/BotleaguesApi/Competition.php b/src/CamilStaps/BotleaguesApi/Competition.php new file mode 100644 index 0000000..a8da665 --- /dev/null +++ b/src/CamilStaps/BotleaguesApi/Competition.php @@ -0,0 +1,12 @@ +isAdministrator; + } + } \ No newline at end of file diff --git a/src/controllers/BotController.php b/src/controllers/BotController.php index c304d1b..9349ea8 100644 --- a/src/controllers/BotController.php +++ b/src/controllers/BotController.php @@ -3,16 +3,26 @@ namespace CamilStaps\BotleaguesApi; class BotController extends BaseController { + protected $bot; + + public function __construct(Bot $bot) { + $this->bot = $bot; + } + public function index() { - return Bot::all(); + return $this->bot->all(); } public function show($id) { - return Bot::findOrFail($id); + return $this->bot->findOrFail($id); + } + + public function update($id) { + throw new \Exception("Not implemented yet."); } - public function edit($id) { - return $this->response->noContent(); + public function store() { + throw new \Exception("Not implemented yet."); } } \ No newline at end of file diff --git a/src/controllers/CompetitionController.php b/src/controllers/CompetitionController.php new file mode 100644 index 0000000..6529433 --- /dev/null +++ b/src/controllers/CompetitionController.php @@ -0,0 +1,36 @@ +competition = $competition; + } + + public function index() { + return $this->competition->all(); + } + + public function show($id) { + return $this->competition->findOrFail($id); + } + + public function update($id) { + throw new \Exception("Not implemented yet."); + } + + public function store() { + $this->competition->fill(Input::all()); + + if ($this->competition->save()) { + return $this->response->created(); + } else { + throw new Dingo\Api\Exception\StoreResourceFailedException; + } + } + +} \ No newline at end of file diff --git a/src/controllers/CompetitionTypeController.php b/src/controllers/CompetitionTypeController.php new file mode 100644 index 0000000..430f7b5 --- /dev/null +++ b/src/controllers/CompetitionTypeController.php @@ -0,0 +1,36 @@ +competitionType = $competitionType; + } + + public function index() { + return $this->competitionType->all(); + } + + public function show($id) { + return $this->competitionType->findOrFail($id); + } + + public function update($id) { + throw new \Exception("Not implemented yet."); + } + + public function store() { + $this->competitionType->fill(Input::all()); + + if ($this->competitionType->save()) { + return $this->response->created(); + } else { + throw new Dingo\Api\Exception\StoreResourceFailedException; + } + } + +} \ No newline at end of file diff --git a/src/controllers/GameController.php b/src/controllers/GameController.php new file mode 100644 index 0000000..3ef9c37 --- /dev/null +++ b/src/controllers/GameController.php @@ -0,0 +1,36 @@ +game = $game; + } + + public function index() { + return $this->game->all(); + } + + public function show($id) { + return $this->game->findOrFail($id); + } + + public function update($id) { + throw new \Exception("Not implemented yet."); + } + + public function store() { + $this->game->fill(Input::all()); + + if ($this->game->save()) { + return $this->response->created(); + } else { + throw new Dingo\Api\Exception\StoreResourceFailedException; + } + } + +} \ No newline at end of file diff --git a/src/controllers/ParticipantController.php b/src/controllers/ParticipantController.php new file mode 100644 index 0000000..2ca28fb --- /dev/null +++ b/src/controllers/ParticipantController.php @@ -0,0 +1,36 @@ +participant = $participant; + } + + public function index() { + return $this->participant->all(); + } + + public function show($id) { + return $this->participant->findOrFail($id); + } + + public function update($id) { + throw new \Exception("Not implemented yet."); + } + + public function store() { + $this->participant->fill(Input::all()); + + if ($this->participant->save()) { + return $this->response->created(); + } else { + throw new Dingo\Api\Exception\StoreResourceFailedException; + } + } + +} \ No newline at end of file diff --git a/src/controllers/UserController.php b/src/controllers/UserController.php index 02400e9..03bebbc 100644 --- a/src/controllers/UserController.php +++ b/src/controllers/UserController.php @@ -4,7 +4,6 @@ namespace CamilStaps\BotleaguesApi; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Input; -use Illuminate\Support\Facades\Redirect; class UserController extends BaseController { @@ -12,6 +11,8 @@ class UserController extends BaseController { public function __construct(User $user) { $this->user = $user; + + $this->beforeFilter('current_user', array('only' => ['update', 'destroy'])); } public function index() { @@ -23,10 +24,6 @@ class UserController extends BaseController { } public function update($id) { - if ($id != Auth::user()->id) { - throw new \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException(); - } - $s = $this->user->find($id); if (!empty(Input::get('password'))) @@ -41,7 +38,7 @@ class UserController extends BaseController { public function store() { $this->user->email = Input::get('email'); - $this->user->password = Hash::make(Input::get('password')); + $this->user->password = empty(Input::get('password')) ? null : Hash::make(Input::get('password')); if ($this->user->save()) { return $this->response->created(); diff --git a/src/filters.php b/src/filters.php index b3d9bbc..11f0096 100644 --- a/src/filters.php +++ b/src/filters.php @@ -1 +1,16 @@ isAdministrator) { + throw new \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException(); + } +}); + +Route::filter('current_user', function(){ + Auth::basic(); + + if (empty(Auth::user()) || Route::input('user') != Auth::user()->id) { + throw new \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException(); + } +}); \ No newline at end of file diff --git a/src/migrations/2015_02_23_184402_botleaguesapi-create_users.php b/src/migrations/2015_02_23_184402_botleaguesapi-create_users.php index b768180..b054a43 100644 --- a/src/migrations/2015_02_23_184402_botleaguesapi-create_users.php +++ b/src/migrations/2015_02_23_184402_botleaguesapi-create_users.php @@ -18,6 +18,7 @@ class BotleaguesapiCreateUsers extends Migration { $table->string('email', 127)->unique(); $table->string('password', 60); $table->rememberToken(); + $table->boolean('isAdministrator')->default(false); $table->timestamps(); }); } diff --git a/src/migrations/2015_04_26_192015_botleaguesapi-create_competition_types.php b/src/migrations/2015_04_26_192015_botleaguesapi-create_competition_types.php new file mode 100644 index 0000000..280bb6d --- /dev/null +++ b/src/migrations/2015_04_26_192015_botleaguesapi-create_competition_types.php @@ -0,0 +1,34 @@ +increments('type'); + $table->string('description'); + $table->mediumText('explanation'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('competition_types'); + } + +} diff --git a/src/migrations/2015_04_26_192025_botleaguesapi-create_competitions.php b/src/migrations/2015_04_26_192025_botleaguesapi-create_competitions.php new file mode 100644 index 0000000..8652086 --- /dev/null +++ b/src/migrations/2015_04_26_192025_botleaguesapi-create_competitions.php @@ -0,0 +1,41 @@ +increments('id')->unsigned(); + $table->integer('gameId')->unsigned(); + $table->foreign('gameId')->references('id')->on('games'); + $table->string('title', 127)->unique(); + $table->mediumText('description')->nullable(); + $table->integer('competition_type')->unsigned(); + $table->foreign('competition_type')->references('type')->on('competition_types'); + $table->dateTime('start'); + $table->dateTime('end'); + $table->mediumInteger('max_players')->unsigned(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('competitions'); + } + +} diff --git a/src/migrations/2015_04_26_192035_botleaguesapi-create_participants.php b/src/migrations/2015_04_26_192035_botleaguesapi-create_participants.php new file mode 100644 index 0000000..9b76316 --- /dev/null +++ b/src/migrations/2015_04_26_192035_botleaguesapi-create_participants.php @@ -0,0 +1,36 @@ +increments('id'); + $table->integer('botId')->unsigned(); + $table->foreign('botId')->references('id')->on('bots'); + $table->integer('competitionId')->unsigned(); + $table->foreign('competitionId')->references('id')->on('competitions'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('participants'); + } + +} diff --git a/src/routes.php b/src/routes.php index 707bf06..5d7b8b6 100644 --- a/src/routes.php +++ b/src/routes.php @@ -1,9 +1,50 @@ 'v1', 'protected' => false], function () { + + Route::resource('bot', 'CamilStaps\BotleaguesApi\BotController', + ['only' => ['index','show']]); + + Route::resource('competition', 'CamilStaps\BotleaguesApi\CompetitionController', + ['only' => ['index','show']]); + + Route::resource('competition_type', 'CamilStaps\BotleaguesApi\CompetitionTypeController', + ['only' => ['index','show']]); + + Route::resource('game', 'CamilStaps\BotleaguesApi\GameController', + ['only' => ['index','show']]); + + Route::resource('participant', 'CamilStaps\BotleaguesApi\ParticipantController', + ['only' => ['index','show']]); + + Route::resource('user', 'CamilStaps\BotleaguesApi\UserController', + ['only' => ['index','show','store']]); + + }); + Route::api(['version' => 'v1', 'protected' => true, 'providers' => 'basic'], function () { + Route::resource('bot', 'CamilStaps\BotleaguesApi\BotController', - array('except' => ['create','edit'])); + ['except' => ['index', 'show', 'create','edit']]); + Route::resource('user', 'CamilStaps\BotleaguesApi\UserController', - array('except' => ['create','edit'])); + ['except' => ['index', 'show', 'create','edit','store']]); + //'before' => 'current_user']); + + Route::group(array('before' => 'administrator'), function() { + + Route::resource('competition', 'CamilStaps\BotleaguesApi\CompetitionController', + ['except' => ['index', 'show', 'create', 'edit']]); + + Route::resource('competition_type', 'CamilStaps\BotleaguesApi\CompetitionTypeController', + ['except' => ['index', 'show', 'create', 'edit']]); + + Route::resource('game', 'CamilStaps\BotleaguesApi\GameController', + ['except' => ['index', 'show', 'create','edit']]); + + }); + }); + }); \ No newline at end of file -- cgit v1.2.3