From 567ff91de0a69f4824254953751bf0b73d21ca33 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Wed, 17 Jul 2019 09:48:20 -0400 Subject: [PATCH] Add pilot ID change fields to the admin screen --- app/Database/seeds/sample.yml | 3 +++ app/Http/Requests/CreatePirepRequest.php | 2 +- app/Http/Requests/UpdateUserRequest.php | 10 +++++++++- app/Models/User.php | 5 +++-- app/Services/UserService.php | 11 ++++++++++- resources/views/admin/users/fields.blade.php | 13 ++++++++++--- resources/views/admin/users/table.blade.php | 2 ++ 7 files changed, 38 insertions(+), 8 deletions(-) diff --git a/app/Database/seeds/sample.yml b/app/Database/seeds/sample.yml index b2a66172..cf67a87a 100644 --- a/app/Database/seeds/sample.yml +++ b/app/Database/seeds/sample.yml @@ -15,6 +15,7 @@ roles: users: - id: 1 + pilot_id: 1 name: Admin User email: admin@phpvms.net password: admin @@ -34,6 +35,7 @@ users: created_at: now updated_at: now - id: 2 + pilot_id: 2 name: Carla Walters email: carla.walters68@example.com password: admin @@ -50,6 +52,7 @@ users: opt_in: 1 toc_accepted: 1 - id: 3 + pilot_id: 3 name: Raymond Pearson email: raymond.pearson56@example.com password: admin diff --git a/app/Http/Requests/CreatePirepRequest.php b/app/Http/Requests/CreatePirepRequest.php index 5a2b39b2..ff87570d 100644 --- a/app/Http/Requests/CreatePirepRequest.php +++ b/app/Http/Requests/CreatePirepRequest.php @@ -5,7 +5,7 @@ namespace App\Http\Requests; use App\Models\Pirep; use App\Repositories\PirepFieldRepository; use Illuminate\Foundation\Http\FormRequest; -use Log; +use Illuminate\Support\Facades\Log; class CreatePirepRequest extends FormRequest { diff --git a/app/Http/Requests/UpdateUserRequest.php b/app/Http/Requests/UpdateUserRequest.php index 06eba56b..ef8eb558 100644 --- a/app/Http/Requests/UpdateUserRequest.php +++ b/app/Http/Requests/UpdateUserRequest.php @@ -4,6 +4,7 @@ namespace App\Http\Requests; use App\Models\User; use Illuminate\Foundation\Http\FormRequest; +use function request; class UpdateUserRequest extends FormRequest { @@ -24,6 +25,13 @@ class UpdateUserRequest extends FormRequest */ public function rules() { - return User::$rules; + $rules = User::$rules; + + $user_id = request('id', null); + + // Validate if the pilot ID is already being used or not + $rules['pilot_id'] = 'required|integer|unique:users,pilot_id,'.$user_id.',id'; + + return $rules; } } diff --git a/app/Models/User.php b/app/Models/User.php index fd1fecfc..eddb3839 100755 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -96,8 +96,9 @@ class User extends Authenticatable ]; public static $rules = [ - 'name' => 'required', - 'email' => 'required|email', + 'pilot_id' => 'required|integer|unique:users,pilot_id', + 'name' => 'required', + 'email' => 'required|email', ]; /** diff --git a/app/Services/UserService.php b/app/Services/UserService.php index 7c91ca0f..96bd20b1 100644 --- a/app/Services/UserService.php +++ b/app/Services/UserService.php @@ -108,6 +108,11 @@ class UserService extends Service return $user; } + public function isPilotIdAlreadyUsed(int $pilot_id): bool + { + return User::where('pilot_id', '=', $pilot_id)->exists(); + } + /** * Change a user's pilot ID * @@ -120,7 +125,11 @@ class UserService extends Service */ public function changePilotId(User $user, int $pilot_id): User { - if (User::where('pilot_id', '=', $pilot_id)->exists()) { + if ($user->pilot_id === $pilot_id) { + return $user; + } + + if ($this->isPilotIdAlreadyUsed($pilot_id)) { Log::error('User with id '.$pilot_id.' already exists'); throw new UserPilotIdExists(); diff --git a/resources/views/admin/users/fields.blade.php b/resources/views/admin/users/fields.blade.php index 997259d8..3d2ddd4f 100644 --- a/resources/views/admin/users/fields.blade.php +++ b/resources/views/admin/users/fields.blade.php @@ -1,17 +1,24 @@
-
+ {{ Form::hidden('id') }} +
+ {{ Form::label('pilot_id', 'Pilot ID:') }} + {{ Form::number('pilot_id', null, ['class' => 'form-control']) }} +

{{ $errors->first('pilot_id') }}

+
+ +
{{ Form::label('name', 'Name:') }} {{ Form::text('name', null, ['class' => 'form-control']) }}

{{ $errors->first('name') }}

-
+
{{ Form::label('email', 'Email:') }} {{ Form::text('email', null, ['class' => 'form-control']) }}

{{ $errors->first('email') }}

-
+
{{ Form::label('password', 'Password:') }} {{ Form::password('password', ['class' => 'form-control']) }}

{{ $errors->first('password') }}

diff --git a/resources/views/admin/users/table.blade.php b/resources/views/admin/users/table.blade.php index 381e0376..ecac353f 100644 --- a/resources/views/admin/users/table.blade.php +++ b/resources/views/admin/users/table.blade.php @@ -1,5 +1,6 @@ + @@ -9,6 +10,7 @@ @foreach($users as $user) +
ID Name Email Registered
{{ $user->ident }} @if(filled($user->country))