Skip to content

Commit

Permalink
Search flights by subfleet #484 (#506)
Browse files Browse the repository at this point in the history
* API level search of flights #484

* Add Subfleet to flights page for search
  • Loading branch information
nabeelio authored Jan 16, 2020
1 parent d03a77b commit 2415caa
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 94 deletions.
1 change: 1 addition & 0 deletions app/Http/Controllers/Api/FlightController.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public function search(Request $request)
return response($e, 503);
}

// TODO: Remove any flights here that a user doesn't have permissions to
foreach ($flights as $flight) {
$this->flightSvc->filterSubfleets(Auth::user(), $flight);
}
Expand Down
106 changes: 41 additions & 65 deletions app/Http/Controllers/Frontend/FlightController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,68 @@
use App\Repositories\AirportRepository;
use App\Repositories\Criteria\WhereCriteria;
use App\Repositories\FlightRepository;
use App\Repositories\SubfleetRepository;
use App\Services\GeoService;
use Flash;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Log;
use Illuminate\Support\Facades\Log;
use Prettus\Repository\Criteria\RequestCriteria;
use Prettus\Repository\Exceptions\RepositoryException;

/**
* Class FlightController
*/
class FlightController extends Controller
{
private $airlineRepo;
private $airportRepo;
private $flightRepo;
private $subfleetRepo;
private $geoSvc;

/**
* FlightController constructor.
*
* @param AirlineRepository $airlineRepo
* @param AirportRepository $airportRepo
* @param FlightRepository $flightRepo
* @param GeoService $geoSvc
* @param AirlineRepository $airlineRepo
* @param AirportRepository $airportRepo
* @param FlightRepository $flightRepo
* @param GeoService $geoSvc
* @param SubfleetRepository $subfleetRepo
*/
public function __construct(
AirlineRepository $airlineRepo,
AirportRepository $airportRepo,
FlightRepository $flightRepo,
GeoService $geoSvc
GeoService $geoSvc,
SubfleetRepository $subfleetRepo
) {
$this->airlineRepo = $airlineRepo;
$this->airportRepo = $airportRepo;
$this->flightRepo = $flightRepo;
$this->geoSvc = $geoSvc;
$this->subfleetRepo = $subfleetRepo;
}

/**
* @param Request $request
*
* @throws \Prettus\Repository\Exceptions\RepositoryException
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function index(Request $request)
{
return $this->search($request);
}

/**
* Make a search request using the Repository search
*
* @param Request $request
*
* @throws \Prettus\Repository\Exceptions\RepositoryException
*
* @return mixed
*/
public function search(Request $request)
{
$where = [
'active' => true,
Expand All @@ -67,14 +85,17 @@ public function index(Request $request)
$where['dpt_airport_id'] = Auth::user()->curr_airport_id;
}

$this->flightRepo->resetCriteria();

try {
$this->flightRepo->searchCriteria($request);
$this->flightRepo->pushCriteria(new WhereCriteria($request, $where));
$this->flightRepo->pushCriteria(new RequestCriteria($request));
} catch (RepositoryException $e) {
Log::emergency($e);
}

$flights = $this->flightRepo
$flights = $this->flightRepo->searchCriteria($request)
->with(['dpt_airport', 'arr_airport', 'airline'])
->orderBy('flight_number', 'asc')
->orderBy('route_leg', 'asc')
Expand All @@ -84,10 +105,15 @@ public function index(Request $request)
->pluck('flight_id')->toArray();

return view('flights.index', [
'airlines' => $this->airlineRepo->selectBoxList(true),
'airports' => $this->airportRepo->selectBoxList(true),
'flights' => $flights,
'saved' => $saved_flights,
'airlines' => $this->airlineRepo->selectBoxList(true),
'airports' => $this->airportRepo->selectBoxList(true),
'flights' => $flights,
'saved' => $saved_flights,
'subfleets' => $this->subfleetRepo->selectBoxList(true),
'flight_number' => $request->input('flight_number'),
'arr_icao' => $request->input('arr_icao'),
'dep_icao' => $request->input('dep_icao'),
'subfleet_id' => $request->input('subfleet_id'),
]);
}

Expand All @@ -114,62 +140,12 @@ public function bids(Request $request)
]);
}

/**
* Make a search request using the Repository search
*
* @param Request $request
*
* @throws \Prettus\Repository\Exceptions\RepositoryException
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function search(Request $request)
{
$where = [
'active' => true,
'visible' => true,
];

if (setting('pilots.restrict_to_company')) {
$where['airline_id'] = Auth::user()->airline_id;
}

// default restrictions on the flights shown. Handle search differently
if (setting('pilots.only_flights_from_current')) {
$where['dpt_airport_id'] = Auth::user()->curr_airport_id;
}

$this->flightRepo->resetCriteria();

try {
$this->flightRepo->pushCriteria(new WhereCriteria($request, $where));
} catch (RepositoryException $e) {
Log::emergency($e);
}

$flights = $this->flightRepo->searchCriteria($request)
->with(['dpt_airport', 'arr_airport', 'airline'])
->orderBy('flight_number', 'asc')
->orderBy('route_leg', 'asc')
->paginate();

$saved_flights = Bid::where('user_id', Auth::id())
->pluck('flight_id')->toArray();

return view('flights.index', [
'airlines' => $this->airlineRepo->selectBoxList(true),
'airports' => $this->airportRepo->selectBoxList(true),
'flights' => $flights,
'saved' => $saved_flights,
]);
}

/**
* Show the flight information page
*
* @param $id
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
* @return mixed
*/
public function show($id)
{
Expand Down
21 changes: 18 additions & 3 deletions app/Repositories/Criteria/WhereCriteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Prettus\Repository\Contracts\CriteriaInterface;
use Prettus\Repository\Contracts\RepositoryInterface;

Expand All @@ -17,17 +18,20 @@ class WhereCriteria implements CriteriaInterface
*/
protected $request;
protected $where;
protected $relations;

/**
* Create a new Where search.
*
* @param $request
* @param $where
* @param Request $request
* @param array $where
* @param array [$relations] Any whereHas (key = table name, value = array of criterea
*/
public function __construct($request, $where)
public function __construct(Request $request, $where, $relations = [])
{
$this->request = $request;
$this->where = $where;
$this->relations = $relations;
}

/**
Expand All @@ -46,6 +50,17 @@ public function apply($model, RepositoryInterface $repository)
$model = $model->where($this->where);
}

// See if any relationships need to be included in this WHERE
if ($this->relations) {
foreach ($this->relations as $relation => $criterea) {
$model = $model
->with($relation)
->whereHas($relation, function (Builder $query) use ($criterea) {
$query->where($criterea);
});
}
}

return $model;
}
}
30 changes: 19 additions & 11 deletions app/Repositories/FlightRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,55 +74,63 @@ public function findFlight($airline_id, $flight_num, $route_code = null, $route_
public function searchCriteria(Request $request, bool $only_active = true): self
{
$where = [];
$relations = [];

if ($only_active === true) {
$where['active'] = $only_active;
$where['visible'] = $only_active;
}

if ($request->filled('flight_id')) {
$where['id'] = $request->flight_id;
$where['id'] = $request->input('flight_id');
}

if ($request->filled('airline_id')) {
$where['airline_id'] = $request->airline_id;
$where['airline_id'] = $request->input('airline_id');
}

if ($request->filled('flight_number')) {
$where['flight_number'] = $request->flight_number;
$where['flight_number'] = $request->input('flight_number');
}

if ($request->filled('route_code')) {
$where['route_code'] = $request->route_code;
$where['route_code'] = $request->input('route_code');
}

if ($request->filled('dpt_airport_id')) {
$where['dpt_airport_id'] = strtoupper($request->dpt_airport_id);
$where['dpt_airport_id'] = strtoupper($request->input('dpt_airport_id'));
}

if ($request->filled('dep_icao')) {
$where['dpt_airport_id'] = strtoupper($request->dep_icao);
$where['dpt_airport_id'] = strtoupper($request->input('dep_icao'));
}

if ($request->filled('arr_airport_id')) {
$where['arr_airport_id'] = strtoupper($request->arr_airport_id);
$where['arr_airport_id'] = strtoupper($request->input('arr_airport_id'));
}

if ($request->filled('arr_icao')) {
$where['arr_airport_id'] = strtoupper($request->arr_icao);
$where['arr_airport_id'] = strtoupper($request->input('arr_icao'));
}

// Distance, greater than
if ($request->filled('dgt')) {
$where[] = ['distance', '>=', $request->dgt];
$where[] = ['distance', '>=', $request->input('dgt')];
}

// Distance, less than
if ($request->filled('dlt')) {
$where[] = ['distance', '<=', $request->dlt];
$where[] = ['distance', '<=', $request->input('dlt')];
}

$this->pushCriteria(new WhereCriteria($request, $where));
// Do a special query for finding the child subfleets
if ($request->filled('subfleet_id')) {
$relations['subfleets'] = [
'subfleets.id' => $request->input('subfleet_id'),
];
}

$this->pushCriteria(new WhereCriteria($request, $where, $relations));

return $this;
}
Expand Down
26 changes: 23 additions & 3 deletions app/Repositories/SubfleetRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
use Prettus\Repository\Contracts\CacheableInterface;
use Prettus\Repository\Traits\CacheableRepository;

/**
* Class SubfleetRepository
*/
class SubfleetRepository extends Repository implements CacheableInterface
{
use CacheableRepository;
Expand All @@ -26,4 +23,27 @@ public function model()
{
return Subfleet::class;
}

/**
* Return the list of aircraft formatted for a select box
*
* @param bool $add_blank
*
* @return array
*/
public function selectBoxList($add_blank = false): array
{
$retval = [];
$items = $this->all();

if ($add_blank) {
$retval[''] = '';
}

foreach ($items as $i) {
$retval[$i->id] = $i->name;
}

return $retval;
}
}
1 change: 1 addition & 0 deletions resources/lang/en/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
'arrival' => 'Arrival',
'aircraft' => 'Aircraft',
'airline' => 'Airline',
'subfleet' => 'Subfleet',
'distance' => 'Distance',
'fuel' => 'Fuel',
'metar' => 'METAR',
Expand Down
1 change: 1 addition & 0 deletions resources/lang/es/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
'arrival' => 'Llegada',
'aircraft' => 'Aeronave',
'airline' => 'Aerolínea',
'subfleet' => 'Subfleet',
'distance' => 'Distancía',
'fuel' => 'Combustible',
'metar' => 'METAR',
Expand Down
1 change: 1 addition & 0 deletions resources/lang/it/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
'arrival' => 'Arrivo',
'aircraft' => 'Aereomobile',
'airline' => 'Compagnia Aerea',
'subfleet' => 'Subfleet',
'distance' => 'Distanza',
'fuel' => 'Carburante',
'metar' => 'METAR',
Expand Down
5 changes: 5 additions & 0 deletions resources/views/layouts/default/flights/search.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
{{ Form::select('arr_icao', $airports, null , ['class' => 'form-control select2']) }}
</div>

<div style="margin-top: 10px; margin-left: 5px;">
<p>@lang('common.subfleet')</p>
{{ Form::select('subfleet_id', $subfleets, null , ['class' => 'form-control select2']) }}
</div>

<div class="clear" style="margin-top: 10px; margin-left: 5px;">
{{ Form::submit(__('common.find'), ['class' => 'btn btn-primary']) }}&nbsp;
<a href="{{ route('frontend.flights.index') }}">@lang('common.reset')</a>
Expand Down
Loading

0 comments on commit 2415caa

Please sign in to comment.