Add greater than/less than search options #297

This commit is contained in:
Nabeel Shahzad
2019-05-10 16:03:04 -05:00
parent 1dae81b707
commit 5b061ba636
7 changed files with 69 additions and 10 deletions

View File

@@ -22,7 +22,7 @@ $factory->define(App\Models\Flight::class, function (Faker $faker) {
'alt_airport_id' => function () { 'alt_airport_id' => function () {
return factory(App\Models\Airport::class)->create()->id; return factory(App\Models\Airport::class)->create()->id;
}, },
'distance' => $faker->numberBetween(0, 3000), 'distance' => $faker->numberBetween(0, 1000),
'route' => null, 'route' => null,
'level' => 0, 'level' => 0,
'dpt_time' => $faker->time(), 'dpt_time' => $faker->time(),

View File

@@ -89,31 +89,34 @@ class FlightController extends Controller
*/ */
public function search(Request $request) public function search(Request $request)
{ {
$user = Auth::user(); $where = [
'active' => true,
try { 'visible' => true,
$where = [ ];
'active' => true,
'visible' => true,
];
// Allow the option to bypass some of these restrictions for the searches
if (!$request->filled('ignore_restrictions') || $request->ignore_restrictions === '0') {
if (setting('pilots.restrict_to_company')) { if (setting('pilots.restrict_to_company')) {
$where['airline_id'] = Auth::user()->airline_id; $where['airline_id'] = Auth::user()->airline_id;
} }
if (setting('pilots.only_flights_from_current')) { if (setting('pilots.only_flights_from_current')) {
$where['dpt_airport_id'] = Auth::user()->curr_airport_id; $where['dpt_airport_id'] = Auth::user()->curr_airport_id;
} }
}
try {
$this->flightRepo->searchCriteria($request); $this->flightRepo->searchCriteria($request);
$this->flightRepo->pushCriteria(new RequestCriteria($request));
$this->flightRepo->pushCriteria(new WhereCriteria($request, $where)); $this->flightRepo->pushCriteria(new WhereCriteria($request, $where));
$this->flightRepo->pushCriteria(new RequestCriteria($request));
$flights = $this->flightRepo->paginate(); $flights = $this->flightRepo->paginate();
} catch (RepositoryException $e) { } catch (RepositoryException $e) {
return response($e, 503); return response($e, 503);
} }
foreach ($flights as $flight) { foreach ($flights as $flight) {
$this->flightSvc->filterSubfleets($user, $flight); $this->flightSvc->filterSubfleets(Auth::user(), $flight);
} }
return FlightResource::collection($flights); return FlightResource::collection($flights);

View File

@@ -13,6 +13,7 @@ use Flash;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Log; use Log;
use Prettus\Repository\Criteria\RequestCriteria;
use Prettus\Repository\Exceptions\RepositoryException; use Prettus\Repository\Exceptions\RepositoryException;
/** /**
@@ -68,6 +69,7 @@ class FlightController extends Controller
try { try {
$this->flightRepo->pushCriteria(new WhereCriteria($request, $where)); $this->flightRepo->pushCriteria(new WhereCriteria($request, $where));
$this->flightRepo->pushCriteria(new RequestCriteria($request));
} catch (RepositoryException $e) { } catch (RepositoryException $e) {
Log::emergency($e); Log::emergency($e);
} }

View File

@@ -18,6 +18,12 @@ class WhereCriteria implements CriteriaInterface
protected $request; protected $request;
protected $where; protected $where;
/**
* Create a new Where search.
*
* @param $request
* @param $where
*/
public function __construct($request, $where) public function __construct($request, $where)
{ {
$this->request = $request; $this->request = $request;

View File

@@ -103,6 +103,16 @@ class FlightRepository extends Repository implements CacheableInterface
$where['arr_airport_id'] = $request->arr_icao; $where['arr_airport_id'] = $request->arr_icao;
} }
// Distance, greater than
if ($request->filled('dgt')) {
$where[] = ['distance', '>=', $request->dgt];
}
// Distance, less than
if ($request->filled('dlt')) {
$where[] = ['distance', '<', $request->dlt];
}
$this->pushCriteria(new WhereCriteria($request, $where)); $this->pushCriteria(new WhereCriteria($request, $where));
return $this; return $this;

0
swagger.yml Normal file
View File

View File

@@ -342,6 +342,44 @@ class FlightTest extends TestCase
$this->assertEquals($flight->id, $body['data'][0]['id']); $this->assertEquals($flight->id, $body['data'][0]['id']);
} }
public function testFlightSearchApiDistance()
{
$total_flights = 10;
$this->user = factory(App\Models\User::class)->create();
$flights = factory(App\Models\Flight::class, $total_flights)->create([
'airline_id' => $this->user->airline_id,
]);
// Max distance generated in factory is 1000, so set a random flight
// and try to find it again through the search
$flight = $flights->random();
$flight->distance = 1500;
$flight->save();
$distance_gt = 1000;
$distance_lt = 1600;
// look for all of the flights now less than the "factory default" of 1000
$query = 'dlt=1000&ignore_restrictions=1';
$req = $this->get('/api/flights/search?'.$query);
$body = $req->json();
$this->assertCount($total_flights - 1, $body['data']);
// Try using greater than
$query = 'dgt='.$distance_gt.'&ignore_restrictions=1';
$req = $this->get('/api/flights/search?'.$query);
$body = $req->json();
$this->assertCount(1, $body['data']);
$this->assertEquals($flight->id, $body['data'][0]['id']);
$query = 'dgt='.$distance_gt.'&dlt='.$distance_lt.'&ignore_restrictions=1';
$req = $this->get('/api/flights/search?'.$query);
$body = $req->json();
$this->assertCount(1, $body['data']);
$this->assertEquals($flight->id, $body['data'][0]['id']);
}
public function testAddSubfleet() public function testAddSubfleet()
{ {
$subfleet = factory(App\Models\Subfleet::class)->create(); $subfleet = factory(App\Models\Subfleet::class)->create();