Search flights by subfleet #484 (#506)

* API level search of flights #484

* Add Subfleet to flights page for search
This commit is contained in:
Nabeel S
2020-01-16 17:36:03 -05:00
committed by GitHub
parent d03a77bd4b
commit 2415caab54
11 changed files with 185 additions and 94 deletions

View File

@@ -4,6 +4,7 @@ namespace App\Repositories\Criteria;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Prettus\Repository\Contracts\CriteriaInterface;
use Prettus\Repository\Contracts\RepositoryInterface;
@@ -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;
}
/**
@@ -46,6 +50,17 @@ class WhereCriteria implements CriteriaInterface
$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;
}
}

View File

@@ -74,6 +74,7 @@ class FlightRepository extends Repository implements CacheableInterface
public function searchCriteria(Request $request, bool $only_active = true): self
{
$where = [];
$relations = [];
if ($only_active === true) {
$where['active'] = $only_active;
@@ -81,48 +82,55 @@ class FlightRepository extends Repository implements CacheableInterface
}
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;
}

View File

@@ -7,9 +7,6 @@ use App\Models\Subfleet;
use Prettus\Repository\Contracts\CacheableInterface;
use Prettus\Repository\Traits\CacheableRepository;
/**
* Class SubfleetRepository
*/
class SubfleetRepository extends Repository implements CacheableInterface
{
use CacheableRepository;
@@ -26,4 +23,27 @@ class SubfleetRepository extends Repository implements CacheableInterface
{
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;
}
}