diff options
author | Camil Staps | 2015-05-07 14:16:13 +0300 |
---|---|---|
committer | Camil Staps | 2015-05-07 14:16:13 +0300 |
commit | 085161fbc3451f90f991a0731fbaf0931cb07678 (patch) | |
tree | 32f13ab154a687453b35c1787dbc92ee87bd96db /src | |
parent | Validate register input (diff) |
Validation exception; user/login endpoint
Diffstat (limited to 'src')
-rw-r--r-- | src/CamilStaps/BotleaguesApi/BotleaguesApiServiceProvider.php | 11 | ||||
-rw-r--r-- | src/CamilStaps/BotleaguesApi/Exception/ValidationException.php | 18 | ||||
-rw-r--r-- | src/controllers/UserController.php | 9 | ||||
-rw-r--r-- | src/routes.php | 47 |
4 files changed, 57 insertions, 28 deletions
diff --git a/src/CamilStaps/BotleaguesApi/BotleaguesApiServiceProvider.php b/src/CamilStaps/BotleaguesApi/BotleaguesApiServiceProvider.php index 4d6c93c..6351614 100644 --- a/src/CamilStaps/BotleaguesApi/BotleaguesApiServiceProvider.php +++ b/src/CamilStaps/BotleaguesApi/BotleaguesApiServiceProvider.php @@ -1,7 +1,6 @@ <?php namespace CamilStaps\BotleaguesApi; -use \Illuminate\Database\Eloquent\ModelNotFoundException; use \Illuminate\Support\ServiceProvider; use \Illuminate\Support\Facades\App; use \Illuminate\Support\Facades\Config; @@ -37,7 +36,7 @@ class BotleaguesApiServiceProvider extends ServiceProvider { 500); }); - API::error(function(ModelNotFoundException $e) { + API::error(function(\Illuminate\Database\Eloquent\ModelNotFoundException $e) { return Response::make( ['error' => 'Resource not found'], 404); @@ -47,6 +46,14 @@ class BotleaguesApiServiceProvider extends ServiceProvider { ['error' => !Config::get('app.debug') || empty($e->getMessage()) ? 'Access denied' : $e->getMessage()], 404); }); + API::error(function(Exception\ValidationException $e) { + return Response::make( + [ + 'error' => $e->getMessage(), + 'errors' => $e->errors + ], + 500); + }); API::error(function(\Exception $e) { return Response::make( ['error' => Config::get('app.debug') ? $e->getMessage() : "Internal error"], diff --git a/src/CamilStaps/BotleaguesApi/Exception/ValidationException.php b/src/CamilStaps/BotleaguesApi/Exception/ValidationException.php new file mode 100644 index 0000000..c59ae6f --- /dev/null +++ b/src/CamilStaps/BotleaguesApi/Exception/ValidationException.php @@ -0,0 +1,18 @@ +<?php +namespace CamilStaps\BotleaguesApi\Exception; + +use Symfony\Component\HttpKernel\Exception\HttpException; + +class ValidationException extends HttpException { + + public function __construct($message = null, $errors = null, Exception $previous = null, $headers = [], $code = 0) { + if (is_null($errors)) { + $this->errors = new MessageBag; + } else { + $this->errors = is_array($errors) ? new MessageBag($errors) : $errors; + } + + parent::__construct(422, $message, $previous, $headers, $code); + } + +}
\ No newline at end of file diff --git a/src/controllers/UserController.php b/src/controllers/UserController.php index e52d8c3..5c247fb 100644 --- a/src/controllers/UserController.php +++ b/src/controllers/UserController.php @@ -24,6 +24,10 @@ class UserController extends BaseController { return $this->user->findOrFail($id); } + public function login() { + return Auth::user(); + } + public function update($id) { $s = $this->user->find($id); @@ -39,14 +43,13 @@ class UserController extends BaseController { public function store() { $rules = [ - 'email' => ['required', 'email'], + 'email' => ['required', 'email', 'unique:users'], 'password' => ['required', 'min:7'] ]; $payload = Input::only('email', 'password'); $validator = Validator::make($payload, $rules); - if ($validator->fails()) { - throw new \Dingo\Api\Exception\StoreResourceFailedException('Could not create new user.', $validator->errors()); + throw new Exception\ValidationException('Could not create new user.', $validator->errors()); } $this->user->email = Input::get('email'); diff --git a/src/routes.php b/src/routes.php index 4e7aa9d..9b3266b 100644 --- a/src/routes.php +++ b/src/routes.php @@ -1,35 +1,14 @@ <?php Route::group(array('https'), function() { - Route::api(['version' => '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', ['except' => ['index', 'show', 'create','edit']]); - + Route::resource('user', 'CamilStaps\BotleaguesApi\UserController', ['except' => ['index', 'show', 'create','edit','store']]); + Route::get('user/login', 'CamilStaps\BotleaguesApi\UserController@login'); Route::group(array('before' => 'administrator'), function() { @@ -46,4 +25,26 @@ Route::group(array('https'), function() { }); + Route::api(['version' => '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']]); + + }); + });
\ No newline at end of file |