diff --git a/app/Http/Controllers/Frontend/FlightController.php b/app/Http/Controllers/Frontend/FlightController.php index 3d967e68..9ad12551 100644 --- a/app/Http/Controllers/Frontend/FlightController.php +++ b/app/Http/Controllers/Frontend/FlightController.php @@ -7,6 +7,8 @@ use Illuminate\Support\Facades\Auth; use App\Repositories\FlightRepository; use App\Http\Controllers\AppBaseController; +use App\Models\UserFlight; +use Mockery\Exception; class FlightController extends AppBaseController { @@ -29,29 +31,68 @@ class FlightController extends AppBaseController // TODO: PAGINATION $flights = $this->flightRepo->findWhere($where); + $saved_flights = UserFlight::where('user_id', Auth::id()) + ->pluck('flight_id')->toArray(); + return $this->view('flights.index', [ 'flights' => $flights, + 'saved' => $saved_flights, ]); } - public function show($id) + public function search(Request $request) { - - } - - public function search(Request $request) { $where = ['active' => true]; $flights = $this->flightRepo->findWhere($where); // TODO: PAGINATION + $saved_flights = UserFlight::where('user_id', Auth::id()) + ->pluck('flight_id')->toArray(); + return $this->view('flights.index', [ 'flights' => $flights, + 'saved' => $saved_flights, ]); } + public function save(Request $request) + { + $user_id = Auth::id(); + $flight_id = $request->input('flight_id'); + $action = $request->input('action'); + + $cols = ['user_id' => $user_id, 'flight_id' => $flight_id]; + + if(strtolower($action) == 'save') { + $uf = UserFlight::create($cols); + $uf->save(); + + return response()->json([ + 'id' => $uf->id, + 'message' => 'Saved!', + ]); + } + + elseif (strtolower($action) == 'remove') { + try { + $uf = UserFlight::where($cols)->first(); + $uf->delete(); + } catch (Exception $e) { } + + return response()->json([ + 'message' => 'Deleted!' + ]); + } + } + public function update() { } + + public function show($id) + { + + } } diff --git a/app/Models/User.php b/app/Models/User.php index f445390f..3ff7f7a6 100755 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -105,6 +105,11 @@ class User extends Authenticatable return $this->belongsTo('App\Models\Airport', 'curr_airport_id'); } + public function flights() + { + return $this->hasMany('App\Models\UserFlight', 'user_id'); + } + public function pireps() { return $this->hasMany('App\Models\Pirep', 'user_id'); diff --git a/app/Models/UserFlight.php b/app/Models/UserFlight.php new file mode 100644 index 00000000..c9744657 --- /dev/null +++ b/app/Models/UserFlight.php @@ -0,0 +1,33 @@ +belongsTo('App\Models\Flight', 'flight_id'); + } + + public function user() + { + return $this->belongsTo('App\Models\User', 'user_id'); + } +} diff --git a/database/migrations/2017_06_17_214650_create_flights_table.php b/database/migrations/2017_06_17_214650_create_flights_table.php index 86762428..b6b3143f 100644 --- a/database/migrations/2017_06_17_214650_create_flights_table.php +++ b/database/migrations/2017_06_17_214650_create_flights_table.php @@ -46,6 +46,15 @@ class CreateFlightsTable extends Migration $table->text('value'); $table->timestamps(); }); + + Schema::create('user_flights', function(Blueprint $table) { + $table->increments('id'); + $table->integer('user_id', false, true); + $table->uuid('flight_id'); + + $table->index('user_id'); + $table->index(['user_id', 'flight_id']); + }); } /** @@ -57,5 +66,6 @@ class CreateFlightsTable extends Migration { Schema::drop('flights'); Schema::drop('flight_fields'); + Schema::drop('user_flights'); } } diff --git a/database/seeds/dev.yml b/database/seeds/dev.yml index 9ff86597..3a7bfede 100644 --- a/database/seeds/dev.yml +++ b/database/seeds/dev.yml @@ -168,6 +168,14 @@ flights: dpt_time: 9AM EST arr_time: 12PM CST route: KJFK KAUS + - id: flightid_3 + airline_id: 1 + flight_number: 200 + dpt_airport_id: 2 + arr_airport_id: 3 + dpt_time: 9AM EST + arr_time: 12PM CST + route: KJFK KAUS flight_fields: - id: 1 @@ -179,6 +187,14 @@ flight_fields: name: cost index value: 100 +user_flights: + - id: 100 + user_id: 1 + flight_id: flightid_1 + - id: 101 + user_id: 1 + flight_id: flightid_3 + pireps: - id: pirepid_1 user_id: 1 diff --git a/resources/views/layouts/default/flights/index.blade.php b/resources/views/layouts/default/flights/index.blade.php index 1f556423..559e0a89 100644 --- a/resources/views/layouts/default/flights/index.blade.php +++ b/resources/views/layouts/default/flights/index.blade.php @@ -16,4 +16,37 @@ @endsection +@section('scripts') + +@endsection diff --git a/resources/views/layouts/default/flights/table.blade.php b/resources/views/layouts/default/flights/table.blade.php index d2846e39..f0314b81 100644 --- a/resources/views/layouts/default/flights/table.blade.php +++ b/resources/views/layouts/default/flights/table.blade.php @@ -14,7 +14,9 @@
-
diff --git a/routes/web.php b/routes/web.php index ab00b7ad..3e12fa0e 100755 --- a/routes/web.php +++ b/routes/web.php @@ -11,10 +11,12 @@ Route::group([ 'middleware' => ['role:admin|user'], ], function () { Route::resource('dashboard', 'DashboardController'); - Route::resource('profile', 'ProfileController'); Route::resource('flights', 'FlightController'); Route::match(['get'], 'flights/search', 'FlightController@search'); + Route::match(['post'], 'flights/save', 'FlightController@save'); + + Route::resource('profile', 'ProfileController'); }); Auth::routes();