Add greater than/less than search options #297
This commit is contained in:
parent
1dae81b707
commit
5b061ba636
@ -22,7 +22,7 @@ $factory->define(App\Models\Flight::class, function (Faker $faker) {
|
||||
'alt_airport_id' => function () {
|
||||
return factory(App\Models\Airport::class)->create()->id;
|
||||
},
|
||||
'distance' => $faker->numberBetween(0, 3000),
|
||||
'distance' => $faker->numberBetween(0, 1000),
|
||||
'route' => null,
|
||||
'level' => 0,
|
||||
'dpt_time' => $faker->time(),
|
||||
|
@ -89,31 +89,34 @@ class FlightController extends Controller
|
||||
*/
|
||||
public function search(Request $request)
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
try {
|
||||
$where = [
|
||||
'active' => true,
|
||||
'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')) {
|
||||
$where['airline_id'] = Auth::user()->airline_id;
|
||||
}
|
||||
|
||||
if (setting('pilots.only_flights_from_current')) {
|
||||
$where['dpt_airport_id'] = Auth::user()->curr_airport_id;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
$this->flightRepo->searchCriteria($request);
|
||||
$this->flightRepo->pushCriteria(new RequestCriteria($request));
|
||||
$this->flightRepo->pushCriteria(new WhereCriteria($request, $where));
|
||||
$this->flightRepo->pushCriteria(new RequestCriteria($request));
|
||||
|
||||
$flights = $this->flightRepo->paginate();
|
||||
} catch (RepositoryException $e) {
|
||||
return response($e, 503);
|
||||
}
|
||||
|
||||
foreach ($flights as $flight) {
|
||||
$this->flightSvc->filterSubfleets($user, $flight);
|
||||
$this->flightSvc->filterSubfleets(Auth::user(), $flight);
|
||||
}
|
||||
|
||||
return FlightResource::collection($flights);
|
||||
|
@ -13,6 +13,7 @@ use Flash;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Log;
|
||||
use Prettus\Repository\Criteria\RequestCriteria;
|
||||
use Prettus\Repository\Exceptions\RepositoryException;
|
||||
|
||||
/**
|
||||
@ -68,6 +69,7 @@ class FlightController extends Controller
|
||||
|
||||
try {
|
||||
$this->flightRepo->pushCriteria(new WhereCriteria($request, $where));
|
||||
$this->flightRepo->pushCriteria(new RequestCriteria($request));
|
||||
} catch (RepositoryException $e) {
|
||||
Log::emergency($e);
|
||||
}
|
||||
|
@ -18,6 +18,12 @@ class WhereCriteria implements CriteriaInterface
|
||||
protected $request;
|
||||
protected $where;
|
||||
|
||||
/**
|
||||
* Create a new Where search.
|
||||
*
|
||||
* @param $request
|
||||
* @param $where
|
||||
*/
|
||||
public function __construct($request, $where)
|
||||
{
|
||||
$this->request = $request;
|
||||
|
@ -103,6 +103,16 @@ class FlightRepository extends Repository implements CacheableInterface
|
||||
$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));
|
||||
|
||||
return $this;
|
||||
|
0
swagger.yml
Normal file
0
swagger.yml
Normal file
@ -342,6 +342,44 @@ class FlightTest extends TestCase
|
||||
$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()
|
||||
{
|
||||
$subfleet = factory(App\Models\Subfleet::class)->create();
|
||||
|
Loading…
Reference in New Issue
Block a user