#57 user can save flights

This commit is contained in:
Nabeel Shahzad 2017-08-03 21:02:02 -05:00
parent 9156b40979
commit 9d53c0103f
8 changed files with 149 additions and 7 deletions

View File

@ -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)
{
}
}

View File

@ -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');

33
app/Models/UserFlight.php Normal file
View File

@ -0,0 +1,33 @@
<?php
namespace App\Models;
use Eloquent as Model;
/**
* @package App\Models
*/
class UserFlight extends Model
{
public $table = 'user_flights';
public $timestamps = false;
public $fillable
= [
'user_id',
'flight_id',
];
/**
* Relationships
*/
public function flight()
{
return $this->belongsTo('App\Models\Flight', 'flight_id');
}
public function user()
{
return $this->belongsTo('App\Models\User', 'user_id');
}
}

View File

@ -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');
}
}

View File

@ -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

View File

@ -16,4 +16,37 @@
</div>
</div>
@endsection
@section('scripts')
<script>
$(document).ready(function() {
$("button.save_flight").click(function(e) {
var btn = $(this);
var flight_id = btn.attr('x-id');
var params = {
method: 'POST',
data: {'flight_id': flight_id, _token: "{{ csrf_token() }}"},
//headers: { 'X-CSRF-Token': "{{ csrf_token() }}" }
};
if(btn.hasClass('btn-danger')) {
params.data.action = 'remove';
params.success = function() {
console.log('successfully removed flight');
btn.removeClass('btn-danger');
}
} else {
params.data.action = 'save';
params.success = function() {
console.log('successfully saved flight');
btn.addClass('btn-danger');
}
}
$.ajax("/flights/save", params);
e.preventDefault();
});
});
</script>
@endsection

View File

@ -14,7 +14,9 @@
</div>
<div class="col-sm-9 text-sm-right">
<!-- use for saved: btn-outline-primary -->
<button class="btn btn-outline-primary btn-icon btn-icon-mini btn-round" type="button">
<button class="btn btn-icon btn-icon-mini btn-round
{{ in_array($flight->id, $saved) ? 'btn-danger':'' }}
save_flight" x-id="{!! $flight->id !!}" type="button">
<i class="now-ui-icons ui-2_favourite-28"></i>
</button>
</div>

View File

@ -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();