From 738cd15db4e652007af19d1393a7869786ee6ed5 Mon Sep 17 00:00:00 2001 From: Kyle Date: Sat, 12 Aug 2017 01:51:55 +0000 Subject: [PATCH] Code Improvements --- .../Controllers/Auth/RegisterController.php | 36 ++++++++++--------- app/Services/PilotService.php | 20 +++++++++-- 2 files changed, 36 insertions(+), 20 deletions(-) diff --git a/app/Http/Controllers/Auth/RegisterController.php b/app/Http/Controllers/Auth/RegisterController.php index 8ea8f543..f66bc995 100755 --- a/app/Http/Controllers/Auth/RegisterController.php +++ b/app/Http/Controllers/Auth/RegisterController.php @@ -3,14 +3,12 @@ namespace App\Http\Controllers\Auth; use Validator; -use App\Models\Role; -use App\Models\User; use App\Models\Airport; use App\Models\Airline; +use App\Services\PilotService; use App\Http\Controllers\Controller; use Illuminate\Foundation\Auth\RegistersUsers; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Hash; class RegisterController extends Controller { @@ -71,9 +69,15 @@ class RegisterController extends Controller ]); } - public function register() + /** + * Get a validator for an incoming registration request. + * + * @param array $data + * @return \Illuminate\Contracts\Validation\Validator + */ + protected function create(array $data) { - // Validate + # First, validate the posted data $this->validate(request(), [ 'name' => 'required', 'email' => 'required|email', @@ -82,17 +86,15 @@ class RegisterController extends Controller 'password' => 'required|confirmed' ]); - # TODO: I'm just feeling we need to do something with Ranking? I forgot. - $user = User::create(['name' => request('name'), - 'email' => request('email'), - 'airline_id' => request('airline'), - 'home_airport_id' => request('home_airport'), - 'curr_airport_id' => request('home_airport'), - 'password' => Hash::make(request('password')), - 'rank_id' => 1]); - //Attach the user roles - $role = Role::where('name', 'user')->first(); - $user->attachRole($role); - return $this->view('auth.registered'); + # Let's call the service + $pilotService = app('App\Services\PilotService'); + + # Let's tell the service to create the pilot + if($pilotService->createPilot($data)) + { + return $this->view('auth.registered'); + } + + # I'm not sure if we really need to add the error something if createPilot fails? } } diff --git a/app/Services/PilotService.php b/app/Services/PilotService.php index d43352bf..8760dac9 100644 --- a/app/Services/PilotService.php +++ b/app/Services/PilotService.php @@ -4,9 +4,9 @@ namespace App\Services; use App\Models\User; use App\Models\Rank; +use App\Models\Role; use Illuminate\Support\Facades\Cache; - - +use Illuminate\Support\Facades\Hash; class PilotService extends BaseService { @@ -55,9 +55,23 @@ class PilotService extends BaseService return $pilot; } - public function create() + public function createPilot(array $data) { + $user = User::create(['name' => $data['name'], + 'email' => $data['email'], + 'airline_id' => $data['airline'], + 'home_airport_id' => $data['home_airport'], + 'curr_airport_id' => $data['home_airport'], + 'password' => Hash::make($data['password'])]); + # Attach the user roles + $role = Role::where('name', 'user')->first(); + $user->attachRole($role); + # Let's check their rank + $this->calculatePilotRank($user); + # TODO: Send out an email + # Looking good, let's return their information + return $user; } }