diff --git a/app/Database/factories/FlightFactory.php b/app/Database/factories/FlightFactory.php index 327ba2f4..248427cf 100644 --- a/app/Database/factories/FlightFactory.php +++ b/app/Database/factories/FlightFactory.php @@ -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(), diff --git a/app/Http/Controllers/Api/FlightController.php b/app/Http/Controllers/Api/FlightController.php index a27613cf..7d474f58 100644 --- a/app/Http/Controllers/Api/FlightController.php +++ b/app/Http/Controllers/Api/FlightController.php @@ -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); diff --git a/app/Http/Controllers/Frontend/FlightController.php b/app/Http/Controllers/Frontend/FlightController.php index ae5bb966..30ded3ac 100644 --- a/app/Http/Controllers/Frontend/FlightController.php +++ b/app/Http/Controllers/Frontend/FlightController.php @@ -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); } diff --git a/app/Repositories/Criteria/WhereCriteria.php b/app/Repositories/Criteria/WhereCriteria.php index 97fb2caf..906b4f99 100644 --- a/app/Repositories/Criteria/WhereCriteria.php +++ b/app/Repositories/Criteria/WhereCriteria.php @@ -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; diff --git a/app/Repositories/FlightRepository.php b/app/Repositories/FlightRepository.php index ee789744..ab3481bb 100644 --- a/app/Repositories/FlightRepository.php +++ b/app/Repositories/FlightRepository.php @@ -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; diff --git a/swagger.yml b/swagger.yml new file mode 100644 index 00000000..e69de29b diff --git a/tests/FlightTest.php b/tests/FlightTest.php index b438f661..f07bbb17 100644 --- a/tests/FlightTest.php +++ b/tests/FlightTest.php @@ -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();