Add pilot ID change fields to the admin screen

This commit is contained in:
Nabeel Shahzad 2019-07-17 09:48:20 -04:00
parent 337fe5cfaa
commit 567ff91de0
7 changed files with 38 additions and 8 deletions

View File

@ -15,6 +15,7 @@ roles:
users: users:
- id: 1 - id: 1
pilot_id: 1
name: Admin User name: Admin User
email: admin@phpvms.net email: admin@phpvms.net
password: admin password: admin
@ -34,6 +35,7 @@ users:
created_at: now created_at: now
updated_at: now updated_at: now
- id: 2 - id: 2
pilot_id: 2
name: Carla Walters name: Carla Walters
email: carla.walters68@example.com email: carla.walters68@example.com
password: admin password: admin
@ -50,6 +52,7 @@ users:
opt_in: 1 opt_in: 1
toc_accepted: 1 toc_accepted: 1
- id: 3 - id: 3
pilot_id: 3
name: Raymond Pearson name: Raymond Pearson
email: raymond.pearson56@example.com email: raymond.pearson56@example.com
password: admin password: admin

View File

@ -5,7 +5,7 @@ namespace App\Http\Requests;
use App\Models\Pirep; use App\Models\Pirep;
use App\Repositories\PirepFieldRepository; use App\Repositories\PirepFieldRepository;
use Illuminate\Foundation\Http\FormRequest; use Illuminate\Foundation\Http\FormRequest;
use Log; use Illuminate\Support\Facades\Log;
class CreatePirepRequest extends FormRequest class CreatePirepRequest extends FormRequest
{ {

View File

@ -4,6 +4,7 @@ namespace App\Http\Requests;
use App\Models\User; use App\Models\User;
use Illuminate\Foundation\Http\FormRequest; use Illuminate\Foundation\Http\FormRequest;
use function request;
class UpdateUserRequest extends FormRequest class UpdateUserRequest extends FormRequest
{ {
@ -24,6 +25,13 @@ class UpdateUserRequest extends FormRequest
*/ */
public function rules() 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;
} }
} }

View File

@ -96,8 +96,9 @@ class User extends Authenticatable
]; ];
public static $rules = [ public static $rules = [
'name' => 'required', 'pilot_id' => 'required|integer|unique:users,pilot_id',
'email' => 'required|email', 'name' => 'required',
'email' => 'required|email',
]; ];
/** /**

View File

@ -108,6 +108,11 @@ class UserService extends Service
return $user; return $user;
} }
public function isPilotIdAlreadyUsed(int $pilot_id): bool
{
return User::where('pilot_id', '=', $pilot_id)->exists();
}
/** /**
* Change a user's pilot ID * Change a user's pilot ID
* *
@ -120,7 +125,11 @@ class UserService extends Service
*/ */
public function changePilotId(User $user, int $pilot_id): User 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'); Log::error('User with id '.$pilot_id.' already exists');
throw new UserPilotIdExists(); throw new UserPilotIdExists();

View File

@ -1,17 +1,24 @@
<div class="row"> <div class="row">
<div class="form-group col-sm-4"> {{ Form::hidden('id') }}
<div class="form-group col-sm-3">
{{ Form::label('pilot_id', 'Pilot ID:') }}
{{ Form::number('pilot_id', null, ['class' => 'form-control']) }}
<p class="text-danger">{{ $errors->first('pilot_id') }}</p>
</div>
<div class="form-group col-sm-3">
{{ Form::label('name', 'Name:') }} {{ Form::label('name', 'Name:') }}
{{ Form::text('name', null, ['class' => 'form-control']) }} {{ Form::text('name', null, ['class' => 'form-control']) }}
<p class="text-danger">{{ $errors->first('name') }}</p> <p class="text-danger">{{ $errors->first('name') }}</p>
</div> </div>
<div class="form-group col-sm-4"> <div class="form-group col-sm-3">
{{ Form::label('email', 'Email:') }} {{ Form::label('email', 'Email:') }}
{{ Form::text('email', null, ['class' => 'form-control']) }} {{ Form::text('email', null, ['class' => 'form-control']) }}
<p class="text-danger">{{ $errors->first('email') }}</p> <p class="text-danger">{{ $errors->first('email') }}</p>
</div> </div>
<div class="form-group col-sm-4"> <div class="form-group col-sm-3">
{{ Form::label('password', 'Password:') }} {{ Form::label('password', 'Password:') }}
{{ Form::password('password', ['class' => 'form-control']) }} {{ Form::password('password', ['class' => 'form-control']) }}
<p class="text-danger">{{ $errors->first('password') }}</p> <p class="text-danger">{{ $errors->first('password') }}</p>

View File

@ -1,5 +1,6 @@
<table class="table table-hover table-responsive" id="users-table"> <table class="table table-hover table-responsive" id="users-table">
<thead> <thead>
<th>ID</th>
<th>Name</th> <th>Name</th>
<th>Email</th> <th>Email</th>
<th>Registered</th> <th>Registered</th>
@ -9,6 +10,7 @@
<tbody> <tbody>
@foreach($users as $user) @foreach($users as $user)
<tr> <tr>
<td>{{ $user->ident }}</td>
<td> <td>
@if(filled($user->country)) @if(filled($user->country))
<span class="flag-icon flag-icon-{{ $user->country }}" <span class="flag-icon flag-icon-{{ $user->country }}"