From d3cf57a1d11fab9337de4ef29ff41f51e476bf4e Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Sun, 11 Jun 2017 11:36:16 -0500 Subject: [PATCH] add airports table and #7 integrate permissions --- .env.example | 2 - Makefile | 18 +- README.md | 1 + .../Controllers/Admin/AirportController.php | 156 ++++++++++++++++++ .../Controllers/Auth/RegisterController.php | 11 +- .../Controllers/Frontend/BaseController.php | 4 +- .../Frontend/DashboardController.php | 2 +- app/Http/Requests/CreateAirportRequest.php | 30 ++++ app/Http/Requests/UpdateAirportRequest.php | 30 ++++ app/Models/Airport.php | 52 ++++++ app/Repositories/AirportRepository.php | 24 +++ config/cache.php | 2 +- ...017_06_11_135707_create_airports_table.php | 37 +++++ database/seeds/DatabaseSeeder.php | 58 ++++++- database/seeds/InitialDataSeeder.php | 39 ----- .../views/admin/airports/create.blade.php | 22 +++ resources/views/admin/airports/edit.blade.php | 23 +++ .../views/admin/airports/fields.blade.php | 31 ++++ .../views/admin/airports/index.blade.php | 23 +++ resources/views/admin/airports/show.blade.php | 19 +++ .../admin/airports/show_fields.blade.php | 38 +++++ .../views/admin/airports/table.blade.php | 26 +++ resources/views/admin/dashboard.blade.php | 20 +++ routes/admin.php | 11 +- routes/web.php | 21 ++- 25 files changed, 640 insertions(+), 60 deletions(-) create mode 100644 app/Http/Controllers/Admin/AirportController.php create mode 100644 app/Http/Requests/CreateAirportRequest.php create mode 100644 app/Http/Requests/UpdateAirportRequest.php create mode 100644 app/Models/Airport.php create mode 100644 app/Repositories/AirportRepository.php create mode 100644 database/migrations/2017_06_11_135707_create_airports_table.php delete mode 100644 database/seeds/InitialDataSeeder.php create mode 100644 resources/views/admin/airports/create.blade.php create mode 100644 resources/views/admin/airports/edit.blade.php create mode 100644 resources/views/admin/airports/fields.blade.php create mode 100644 resources/views/admin/airports/index.blade.php create mode 100644 resources/views/admin/airports/show.blade.php create mode 100644 resources/views/admin/airports/show_fields.blade.php create mode 100644 resources/views/admin/airports/table.blade.php diff --git a/.env.example b/.env.example index d8afcdd8..30eb1afe 100644 --- a/.env.example +++ b/.env.example @@ -12,5 +12,3 @@ DB_USERNAME= DB_PASSWORD= CACHE_DRIVER=array -SESSION_DRIVER=array -QUEUE_DRIVER=sync diff --git a/Makefile b/Makefile index 73e8a6d3..52d6370f 100644 --- a/Makefile +++ b/Makefile @@ -10,24 +10,30 @@ all: build build: composer install --no-interaction + php artisan optimize + php artisan config:cache @make db -install: +install: db echo "" +reset: + @rm database/testing.sqlite + @php artisan optimize + @php artisan config:clear + @sqlite3 database/testing.sqlite "" + @php artisan migrate:refresh --seed + db: sqlite3 database/testing.sqlite "" php artisan migrate + php artisan db:seed unittest-db: rm -f database/unittest.sqlite sqlite3 database/unittest.sqlite "" php artisan migrate:refresh --seed --env unittest -reset-db: - rm database/testing.sqlite - make db - tests: test test: @@ -35,7 +41,7 @@ test: schema: #php artisan infyom:scaffold Airlines --fieldsFile=database/schema/airlines.json - php artisan infyom:scaffold Aircraft --fieldsFile=database/schema/aircraft.json + #php artisan infyom:scaffold Aircraft --fieldsFile=database/schema/aircraft.json echo "" docker: diff --git a/README.md b/README.md index 050b412c..5d60e6d8 100755 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ run the following commands. for right now, we're running on sqlite. for mysql, s ```bash cp .env.example .env composer install --no-interaction +php artisan optimize sqlite3 database/testing.sqlite "" php artisan migrate:refresh --seed ``` diff --git a/app/Http/Controllers/Admin/AirportController.php b/app/Http/Controllers/Admin/AirportController.php new file mode 100644 index 00000000..f39b0591 --- /dev/null +++ b/app/Http/Controllers/Admin/AirportController.php @@ -0,0 +1,156 @@ +airportRepository = $airportRepo; + } + + /** + * Display a listing of the Airport. + * + * @param Request $request + * @return Response + */ + public function index(Request $request) + { + $this->airportRepository->pushCriteria(new RequestCriteria($request)); + $airports = $this->airportRepository->all(); + + return view('admin.airports.index') + ->with('airports', $airports); + } + + /** + * Show the form for creating a new Airport. + * + * @return Response + */ + public function create() + { + return view('admin.airports.create'); + } + + /** + * Store a newly created Airport in storage. + * + * @param CreateAirportRequest $request + * + * @return Response + */ + public function store(CreateAirportRequest $request) + { + $input = $request->all(); + + $airport = $this->airportRepository->create($input); + + Flash::success('Airport saved successfully.'); + + return redirect(route('admin.airports.index')); + } + + /** + * Display the specified Airport. + * + * @param int $id + * + * @return Response + */ + public function show($id) + { + $airport = $this->airportRepository->findWithoutFail($id); + + if (empty($airport)) { + Flash::error('Airport not found'); + + return redirect(route('admin.airports.index')); + } + + return view('admin.airports.show')->with('airport', $airport); + } + + /** + * Show the form for editing the specified Airport. + * + * @param int $id + * + * @return Response + */ + public function edit($id) + { + $airport = $this->airportRepository->findWithoutFail($id); + + if (empty($airport)) { + Flash::error('Airport not found'); + + return redirect(route('admin.airports.index')); + } + + return view('admin.airports.edit')->with('airport', $airport); + } + + /** + * Update the specified Airport in storage. + * + * @param int $id + * @param UpdateAirportRequest $request + * + * @return Response + */ + public function update($id, UpdateAirportRequest $request) + { + $airport = $this->airportRepository->findWithoutFail($id); + + if (empty($airport)) { + Flash::error('Airport not found'); + + return redirect(route('admin.airports.index')); + } + + $airport = $this->airportRepository->update($request->all(), $id); + + Flash::success('Airport updated successfully.'); + + return redirect(route('admin.airports.index')); + } + + /** + * Remove the specified Airport from storage. + * + * @param int $id + * + * @return Response + */ + public function destroy($id) + { + $airport = $this->airportRepository->findWithoutFail($id); + + if (empty($airport)) { + Flash::error('Airport not found'); + + return redirect(route('admin.airports.index')); + } + + $this->airportRepository->delete($id); + + Flash::success('Airport deleted successfully.'); + + return redirect(route('admin.airports.index')); + } +} diff --git a/app/Http/Controllers/Auth/RegisterController.php b/app/Http/Controllers/Auth/RegisterController.php index 34c376cf..750a4d59 100755 --- a/app/Http/Controllers/Auth/RegisterController.php +++ b/app/Http/Controllers/Auth/RegisterController.php @@ -2,8 +2,9 @@ namespace App\Http\Controllers\Auth; -use App\User; use Validator; +use App\Models\Role; +use App\Models\User; use App\Http\Controllers\Controller; use Illuminate\Foundation\Auth\RegistersUsers; @@ -50,7 +51,7 @@ class RegisterController extends Controller return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:users', - 'password' => 'required|min:6|confirmed', + 'password' => 'required|min:5|confirmed', ]); } @@ -62,10 +63,14 @@ class RegisterController extends Controller */ protected function create(array $data) { - return User::create([ + $user = User::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); + + $role = Role::where('name', 'admin')->first(); + $user->attachRole($role); + return $user->refresh(); } } diff --git a/app/Http/Controllers/Frontend/BaseController.php b/app/Http/Controllers/Frontend/BaseController.php index 8727055d..a1f09416 100644 --- a/app/Http/Controllers/Frontend/BaseController.php +++ b/app/Http/Controllers/Frontend/BaseController.php @@ -2,7 +2,9 @@ namespace App\Http\Controllers\Frontend; -class BaseController extends Controller +use App\Http\Controllers\AppBaseController; + +class BaseController extends AppBaseController { } diff --git a/app/Http/Controllers/Frontend/DashboardController.php b/app/Http/Controllers/Frontend/DashboardController.php index 71bf5ce0..c75827b9 100644 --- a/app/Http/Controllers/Frontend/DashboardController.php +++ b/app/Http/Controllers/Frontend/DashboardController.php @@ -11,6 +11,6 @@ class DashboardController extends BaseController */ public function index() { - return view('frontend/dashboard'); + return view('frontend.dashboard'); } } diff --git a/app/Http/Requests/CreateAirportRequest.php b/app/Http/Requests/CreateAirportRequest.php new file mode 100644 index 00000000..cfa70ca1 --- /dev/null +++ b/app/Http/Requests/CreateAirportRequest.php @@ -0,0 +1,30 @@ + 'required' + ]; + + public function save(array $options = []) + { + if(in_array('icao', $options)) { + $options['icao'] = strtoupper($options['icao']); + } + + return parent::save($options); + } +} diff --git a/app/Repositories/AirportRepository.php b/app/Repositories/AirportRepository.php new file mode 100644 index 00000000..e94d4ba1 --- /dev/null +++ b/app/Repositories/AirportRepository.php @@ -0,0 +1,24 @@ + 'laravel', + 'prefix' => 'phpvms', ]; diff --git a/database/migrations/2017_06_11_135707_create_airports_table.php b/database/migrations/2017_06_11_135707_create_airports_table.php new file mode 100644 index 00000000..4d8993f8 --- /dev/null +++ b/database/migrations/2017_06_11_135707_create_airports_table.php @@ -0,0 +1,37 @@ +increments('id'); + $table->string('icao', 5)->unique(); + $table->string('name'); + $table->string('location')->nullable(); + $table->float('lat', 7, 4)->default(0.0); + $table->float('lon', 7, 4)->default(0.0); + $table->timestamps(); + $table->softDeletes(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('airports'); + } +} diff --git a/database/seeds/DatabaseSeeder.php b/database/seeds/DatabaseSeeder.php index 5ec2951a..ee742a3d 100755 --- a/database/seeds/DatabaseSeeder.php +++ b/database/seeds/DatabaseSeeder.php @@ -1,5 +1,6 @@ call(AircraftClassesSeeder::class); + $this->user_seeder(); + $this->airport_seeder(); + } + + protected function time() { + return Carbon::now()->format('Y-m-d H:i:s'); + } + /** + * Add an initial admin user and roles + */ + protected function user_seeder() + { + foreach ([ + ['id' => 1,'name' => 'admin', 'display_name' => 'Administrators'], + ['id' => 2, 'name' => 'user', 'display_name' => 'Pilot'], + ] as $group) { DB::table('roles')->insert($group); } + + DB::table('users')->insert([ + 'id' => 1, + 'name' => 'Admin User', + 'email' => 'admin@phpvms.net', + 'password' => bcrypt('phpvms'), + 'created_at' => Carbon::now('UTC'), + 'updated_at' => Carbon::now('UTC'), + ]); + + // add as both admin and user role + DB::table('role_user')->insert(['user_id' => 1, 'role_id' => 1]); + DB::table('role_user')->insert(['user_id' => 1, 'role_id' => 2]); + } + + /** + * Add a few initial airports + */ + protected function airport_seeder() + { + $airports = [ + [ + 'icao' => 'KAUS', + 'name' => 'Austin-Bergstrom International Airport', + 'location' => 'Austin, Texas, USA', + 'lat' => 30.1945278, + 'lon' => -97.6698889, + ], + [ + 'icao' => 'KJFK', + 'name' => 'John F Kennedy International Airport', + 'location' => 'New York, New York, USA', + 'lat' => 40.6399257, + 'lon' => -73.7786950, + ], + ]; + + foreach($airports as $airport) { + DB::table('airports')->insert($airport); + } } } diff --git a/database/seeds/InitialDataSeeder.php b/database/seeds/InitialDataSeeder.php deleted file mode 100644 index c496523e..00000000 --- a/database/seeds/InitialDataSeeder.php +++ /dev/null @@ -1,39 +0,0 @@ -user_seeder(); - } - - /** - * Add an initial admin user and roles - */ - protected function user_seeder() - { - foreach ([ - ['id' => 1,'name' => 'admin', 'display_name' => 'Administrators'], - ['id' => 2, 'name' => 'user', 'display_name' => 'Pilot'], - ] as $group) { DB::table('roles')->insert($group); } - - DB::table('users')->insert([ - 'id' => 1, - 'name' => 'Admin User', - 'email' => 'admin@phpvms.net', - 'password' => bcrypt('admin'), - ]); - - DB::table('role_user')->insert([ - 'user_id' => 1, - 'role_id' => 1, - ]); - } -} diff --git a/resources/views/admin/airports/create.blade.php b/resources/views/admin/airports/create.blade.php new file mode 100644 index 00000000..ef4dec3f --- /dev/null +++ b/resources/views/admin/airports/create.blade.php @@ -0,0 +1,22 @@ +@extends('admin.app') + +@section('content') +
+

Add Airport

+
+
+ @include('adminlte-templates::common.errors') +
+ +
+
+ {!! Form::open(['route' => 'admin.airports.store']) !!} + + @include('admin.airports.fields') + + {!! Form::close() !!} +
+
+
+
+@endsection diff --git a/resources/views/admin/airports/edit.blade.php b/resources/views/admin/airports/edit.blade.php new file mode 100644 index 00000000..81b15933 --- /dev/null +++ b/resources/views/admin/airports/edit.blade.php @@ -0,0 +1,23 @@ +@extends('admin.app') + +@section('content') +
+

+ Editing {{ $airport->name }} +

+
+
+ @include('adminlte-templates::common.errors') +
+
+
+ {!! Form::model($airport, ['route' => ['admin.airports.update', $airport->id], 'method' => 'patch']) !!} + + @include('admin.airports.fields') + + {!! Form::close() !!} +
+
+
+
+@endsection diff --git a/resources/views/admin/airports/fields.blade.php b/resources/views/admin/airports/fields.blade.php new file mode 100644 index 00000000..d5be5cf3 --- /dev/null +++ b/resources/views/admin/airports/fields.blade.php @@ -0,0 +1,31 @@ + +
+ {!! Form::label('icao', 'ICAO:') !!} + {!! Form::text('icao', null, ['class' => 'form-control']) !!} +
+ +
+ {!! Form::label('name', 'Name:') !!} + {!! Form::text('name', null, ['class' => 'form-control']) !!} +
+ +
+ {!! Form::label('location', 'Location:') !!} + {!! Form::text('location', null, ['class' => 'form-control']) !!} +
+ +
+ {!! Form::label('lat', 'Latitude:') !!} + {!! Form::number('lat', null, ['class' => 'form-control']) !!} +
+ +
+ {!! Form::label('lon', 'Longitude:') !!} + {!! Form::number('lon', null, ['class' => 'form-control']) !!} +
+ + +
+ {!! Form::submit('Save', ['class' => 'btn btn-primary']) !!} + Cancel +
diff --git a/resources/views/admin/airports/index.blade.php b/resources/views/admin/airports/index.blade.php new file mode 100644 index 00000000..5265fa3c --- /dev/null +++ b/resources/views/admin/airports/index.blade.php @@ -0,0 +1,23 @@ +@extends('admin.app') + +@section('content') +
+

$MODEL_NAME_PLURAL_HUMAN$

+

+ Add New +

+
+
+
+ + @include('flash::message') + +
+
+
+ @include('admin.airports.table') +
+
+
+@endsection + diff --git a/resources/views/admin/airports/show.blade.php b/resources/views/admin/airports/show.blade.php new file mode 100644 index 00000000..d2ac23d5 --- /dev/null +++ b/resources/views/admin/airports/show.blade.php @@ -0,0 +1,19 @@ +@extends('admin.app') + +@section('content') +
+

+ {!! $airport->name !!} +

+
+
+
+
+
+ @include('admin.airports.show_fields') + Back +
+
+
+
+@endsection diff --git a/resources/views/admin/airports/show_fields.blade.php b/resources/views/admin/airports/show_fields.blade.php new file mode 100644 index 00000000..e106cbf5 --- /dev/null +++ b/resources/views/admin/airports/show_fields.blade.php @@ -0,0 +1,38 @@ + +
+ {!! Form::label('icao', 'ICAO:') !!} +

{!! $airport->icao !!}

+
+ +
+ {!! Form::label('name', 'Name:') !!} +

{!! $airport->name !!}

+
+ +
+ {!! Form::label('location', 'Location:') !!} +

{!! $airport->location !!}

+
+ +
+ {!! Form::label('lat', 'Latitude:') !!} +

{!! $airport->lat !!}

+
+ +
+ {!! Form::label('lon', 'Longitude:') !!} +

{!! $airport->lon !!}

+
+ + +
+ {!! Form::label('created_at', 'Created At:') !!} +

{!! $airport->created_at !!}

+
+ + +
+ {!! Form::label('updated_at', 'Updated At:') !!} +

{!! $airport->updated_at !!}

+
+ diff --git a/resources/views/admin/airports/table.blade.php b/resources/views/admin/airports/table.blade.php new file mode 100644 index 00000000..df443bfb --- /dev/null +++ b/resources/views/admin/airports/table.blade.php @@ -0,0 +1,26 @@ + + + + + + + + + @foreach($airports as $airport) + + + + + + + @endforeach + +
ICAONameLocationAction
{!! $airport->icao !!}{!! $airport->name !!}{!! $airport->location !!} ({!! $airport->lat !!}x{!! $airport->lon !!}) + {!! Form::open(['route' => ['admin.airports.destroy', $airport->id], 'method' => 'delete']) !!} +
+ + + {!! Form::button('', ['type' => 'submit', 'class' => 'btn btn-danger btn-xs', 'onclick' => "return confirm('Are you sure?')"]) !!} +
+ {!! Form::close() !!} +
diff --git a/resources/views/admin/dashboard.blade.php b/resources/views/admin/dashboard.blade.php index e69de29b..c2a62613 100644 --- a/resources/views/admin/dashboard.blade.php +++ b/resources/views/admin/dashboard.blade.php @@ -0,0 +1,20 @@ +@extends('admin.app') + +@section('content') +
+

Dashboard

+
+
+
+ + @include('flash::message') + +
+
+
+ wassup +
+
+
+@endsection + diff --git a/routes/admin.php b/routes/admin.php index 32319b7e..b70d8311 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -3,18 +3,19 @@ * Admin routes */ -Route::get('/admin', [ +/*Route::get('/admin', [ 'middleware' => ['role:admin'], - 'uses' => 'DashboardController@index' + 'uses' => 'Http\Controllers\Admin\DashboardController@index' ]); Route::group([ - 'namespace' => 'Admin', - 'middleware' => ['role:admin'], + 'namespace' => 'Http\Controllers\Admin', + //'middleware' => ['role:admin'], 'prefix' => 'admin', ], function () { + Route::resource('airports', 'AirportController'); Route::resource('airlines', 'AirlinesController'); Route::resource('aircraft', 'AircraftController'); Route::resource('aircraftclasses', 'AircraftClassController'); Route::resource('fares', 'FareController'); -}); +});*/ diff --git a/routes/web.php b/routes/web.php index 3b5fe701..96cdc54b 100755 --- a/routes/web.php +++ b/routes/web.php @@ -19,7 +19,26 @@ Route::get('/home', 'HomeController@index'); Route::group([ 'namespace' => 'Frontend', - 'middleware' => ['role:admin', 'role:user'], + 'middleware' => ['role:admin|user'], ], function () { Route::resource('dashboard', 'DashboardController'); }); + +/** + * Admin Routes + */ + +Route::group([ + 'namespace' => 'Admin', + 'middleware' => ['role:admin'], + 'prefix' => 'admin', +], function () { + Route::get('', ['uses' => 'DashboardController@index']); + Route::get('/', ['uses' => 'DashboardController@index']); + + Route::resource('airports', 'AirportController'); + Route::resource('airlines', 'AirlinesController'); + Route::resource('aircraft', 'AircraftController'); + Route::resource('aircraftclasses', 'AircraftClassController'); + Route::resource('fares', 'FareController'); +});