diff options
Diffstat (limited to 'src/controllers')
-rw-r--r-- | src/controllers/.gitkeep | 0 | ||||
-rw-r--r-- | src/controllers/BaseController.php | 10 | ||||
-rw-r--r-- | src/controllers/BotController.php | 30 | ||||
-rw-r--r-- | src/controllers/CompetitionController.php | 37 | ||||
-rw-r--r-- | src/controllers/CompetitionTypeController.php | 37 | ||||
-rw-r--r-- | src/controllers/GameController.php | 37 | ||||
-rw-r--r-- | src/controllers/ParticipantController.php | 37 | ||||
-rw-r--r-- | src/controllers/PasswordReminderController.php | 47 | ||||
-rw-r--r-- | src/controllers/UserController.php | 63 | ||||
-rw-r--r-- | src/controllers/UserTokenController.php | 51 |
10 files changed, 349 insertions, 0 deletions
diff --git a/src/controllers/.gitkeep b/src/controllers/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/controllers/.gitkeep diff --git a/src/controllers/BaseController.php b/src/controllers/BaseController.php new file mode 100644 index 0000000..78bda5f --- /dev/null +++ b/src/controllers/BaseController.php @@ -0,0 +1,10 @@ +<?php +namespace CamilStaps\BotleaguesApi\Controllers; + +use \Dingo\Api\Routing\Helpers; + +class BaseController extends \Illuminate\Routing\Controller { + + use Helpers; + +}
\ No newline at end of file diff --git a/src/controllers/BotController.php b/src/controllers/BotController.php new file mode 100644 index 0000000..89aef8c --- /dev/null +++ b/src/controllers/BotController.php @@ -0,0 +1,30 @@ +<?php +namespace CamilStaps\BotleaguesApi\Controllers; + +use CamilStaps\BotleaguesApi\Database\Bot; + +class BotController extends BaseController { + + protected $bot; + + public function __construct(Bot $bot) { + $this->bot = $bot; + } + + public function index() { + return $this->bot->all(); + } + + public function show($id) { + return $this->bot->findOrFail($id); + } + + public function update($id) { + throw new \Exception("Not implemented yet."); + } + + 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..f54c11d --- /dev/null +++ b/src/controllers/CompetitionController.php @@ -0,0 +1,37 @@ +<?php +namespace CamilStaps\BotleaguesApi\Controllers; + +use CamilStaps\BotleaguesApi\Database\Competition; +use Illuminate\Support\Facades\Input; + +class CompetitionController extends BaseController { + + protected $competition; + + public function __construct(Competition $competition) { + $this->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..993973c --- /dev/null +++ b/src/controllers/CompetitionTypeController.php @@ -0,0 +1,37 @@ +<?php +namespace CamilStaps\BotleaguesApi\Controllers; + +use CamilStaps\BotleaguesApi\Database\CompetitionType; +use Illuminate\Support\Facades\Input; + +class CompetitionTypeController extends BaseController { + + protected $competitionType; + + public function __construct(CompetitionType $competitionType) { + $this->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..0fbcc1b --- /dev/null +++ b/src/controllers/GameController.php @@ -0,0 +1,37 @@ +<?php +namespace CamilStaps\BotleaguesApi\Controllers; + +use CamilStaps\BotleaguesApi\Database\Game; +use Illuminate\Support\Facades\Input; + +class GameController extends BaseController { + + protected $game; + + public function __construct(Game $game) { + $this->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..11d7dd9 --- /dev/null +++ b/src/controllers/ParticipantController.php @@ -0,0 +1,37 @@ +<?php +namespace CamilStaps\BotleaguesApi\Controllers; + +use CamilStaps\BotleaguesApi\Database\Participant; +use Illuminate\Support\Facades\Input; + +class ParticipantController extends BaseController { + + protected $participant; + + public function __construct(Participant $participant) { + $this->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/PasswordReminderController.php b/src/controllers/PasswordReminderController.php new file mode 100644 index 0000000..bf9d959 --- /dev/null +++ b/src/controllers/PasswordReminderController.php @@ -0,0 +1,47 @@ +<?php +/** + * Created by PhpStorm. + * User: camilstaps + * Date: 13-5-15 + * Time: 13:13 + */ + +namespace CamilStaps\BotleaguesApi\Controllers; + +use CamilStaps\BotleaguesApi\Database\PasswordReminder; +use CamilStaps\BotleaguesApi\Exception\ValidationException; +use Illuminate\Support\Facades\Input; +use Illuminate\Support\Facades\Validator; + +class PasswordReminderController extends BaseController { + + protected $passwordReminder; + + public function __construct(PasswordReminder $passwordReminder) { + $this->passwordReminder = $passwordReminder; + } + + /** + * Set the userId and create a random token + */ + public function store() { + $rules = [ + 'user_id' => ['required'] + ]; + $payload = Input::only('user_id'); + $validator = Validator::make($payload, $rules); + if ($validator->fails()) { + throw new ValidationException('Could not find user.', $validator->errors()); + } + + $this->passwordReminder->userId = Input::get('user_id'); + $this->passwordReminder->token = base64_encode(openssl_random_pseudo_bytes(64)); + + if ($this->passwordReminder->save()) { + return $this->passwordReminder; + } 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 new file mode 100644 index 0000000..6af3fe8 --- /dev/null +++ b/src/controllers/UserController.php @@ -0,0 +1,63 @@ +<?php +namespace CamilStaps\BotleaguesApi\Controllers; + +use CamilStaps\BotleaguesApi\Database\User; +use CamilStaps\BotleaguesApi\Exception\ValidationException; +use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Hash; +use Illuminate\Support\Facades\Input; +use Illuminate\Support\Facades\Validator; + +class UserController extends BaseController { + + protected $user; + + public function __construct(User $user) { + $this->user = $user; + + $this->beforeFilter('current_user', array('only' => ['update', 'destroy'])); + } + + public function index() { + return $this->user->all(); + } + + public function show($id) { + return $this->user->findOrFail($id); + } + + public function update($id) { + $s = $this->user->find($id); + + if (!empty(Input::get('password'))) + $s->password = Hash::make(Input::get('password')); + + if ($s->save()) { + return $this->show($id); + } else { + throw new \Dingo\Api\Exception\UpdateResourceFailedException; + } + } + + public function store() { + $rules = [ + 'email' => ['required', 'email', 'unique:users'], + 'password' => ['required', 'min:7'] + ]; + $payload = Input::only('email', 'password'); + $validator = Validator::make($payload, $rules); + if ($validator->fails()) { + throw new ValidationException('Could not create new user.', $validator->errors()); + } + + $this->user->email = Input::get('email'); + $this->user->password = empty(Input::get('password')) ? null : Hash::make(Input::get('password')); + + if ($this->user->save()) { + return $this->response->created(); + } else { + throw new \Dingo\Api\Exception\StoreResourceFailedException; + } + } + +}
\ No newline at end of file diff --git a/src/controllers/UserTokenController.php b/src/controllers/UserTokenController.php new file mode 100644 index 0000000..20bd06c --- /dev/null +++ b/src/controllers/UserTokenController.php @@ -0,0 +1,51 @@ +<?php +namespace CamilStaps\BotleaguesApi\Controllers; + +use CamilStaps\BotleaguesApi\Database\UserToken; +use Illuminate\Support\Facades\Input; +use Illuminate\Support\Facades\Auth; + +class UserTokenController extends BaseController { + + protected $userToken; + + public function __construct(UserToken $userToken) { + $this->userToken = $userToken; + } + + /** + * Only the tokens of the authenticated user are shown + */ + public function index() { + return $this->userToken->where('userId', '=', Auth::user()->id)->get(); + } + + /** + * Only the tokens of the authenticated user are available + */ + public function show($id) { + return $this->userToken->where('userId', '=', Auth::user()->id)->findOrFail($id); + } + + /** + * Set the userId and create a random token + */ + public function store() { + $this->userToken->userId = Auth::user()->id; + $this->userToken->token = base64_encode(openssl_random_pseudo_bytes(64)); + + if ($this->userToken->save()) { + // Remove the token field from the hidden fields + $hidden = $this->userToken->getHidden(); + foreach ($hidden as $k => $v) + if ($v == 'token') + unset($hidden[$k]); + $this->userToken->setHidden($hidden); + + return $this->userToken; + } else { + throw new \Dingo\Api\Exception\StoreResourceFailedException; + } + } + +}
\ No newline at end of file |