phpvms/app/Http/Controllers/Frontend/FlightController.php

99 lines
2.4 KiB
PHP
Raw Normal View History

2017-08-03 04:12:26 +08:00
<?php
namespace App\Http\Controllers\Frontend;
use Illuminate\Http\Request;
2017-08-03 04:29:04 +08:00
use Illuminate\Support\Facades\Auth;
2017-08-03 04:12:26 +08:00
use App\Repositories\FlightRepository;
use App\Http\Controllers\AppBaseController;
2017-08-04 10:02:02 +08:00
use App\Models\UserFlight;
use Mockery\Exception;
2017-08-03 04:12:26 +08:00
class FlightController extends AppBaseController
{
private $flightRepo;
public function __construct(FlightRepository $flightRepo)
{
$this->flightRepo = $flightRepo;
}
public function index(Request $request)
{
2017-08-03 04:29:04 +08:00
$where = ['active' => true];
2017-08-03 04:12:26 +08:00
2017-08-03 04:29:04 +08:00
// default restrictions on the flights shown. Handle search differently
if (config('phpvms.only_flights_from_current')) {
$where['dpt_airport_id'] = Auth::user()->curr_airport_id;
}
// TODO: PAGINATION
$flights = $this->flightRepo->findWhere($where);
2017-08-04 10:02:02 +08:00
$saved_flights = UserFlight::where('user_id', Auth::id())
->pluck('flight_id')->toArray();
2017-08-03 04:12:26 +08:00
return $this->view('flights.index', [
'flights' => $flights,
2017-08-04 10:02:02 +08:00
'saved' => $saved_flights,
2017-08-03 04:12:26 +08:00
]);
}
2017-08-04 10:02:02 +08:00
public function search(Request $request)
2017-08-03 04:12:26 +08:00
{
2017-08-03 04:29:04 +08:00
$where = ['active' => true];
$flights = $this->flightRepo->findWhere($where);
// TODO: PAGINATION
2017-08-04 10:02:02 +08:00
$saved_flights = UserFlight::where('user_id', Auth::id())
->pluck('flight_id')->toArray();
2017-08-03 04:29:04 +08:00
return $this->view('flights.index', [
'flights' => $flights,
2017-08-04 10:02:02 +08:00
'saved' => $saved_flights,
2017-08-03 04:29:04 +08:00
]);
}
2017-08-04 10:02:02 +08:00
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!'
]);
}
}
2017-08-03 04:12:26 +08:00
public function update()
{
}
2017-08-04 10:02:02 +08:00
public function show($id)
{
}
2017-08-03 04:12:26 +08:00
}