Change the filtering to operate without being in the service layer #174

This commit is contained in:
Nabeel Shahzad
2018-02-09 15:42:37 -06:00
parent 17f1085ffb
commit 80cd80a5cf
2 changed files with 19 additions and 4 deletions

View File

@@ -2,6 +2,7 @@
namespace App\Http\Controllers\Api;
use App\Repositories\Criteria\WhereCriteria;
use App\Services\FlightService;
use Auth;
use Illuminate\Http\Request;
@@ -36,7 +37,15 @@ class FlightController extends RestController
public function index(Request $request)
{
$user = Auth::user();
$flights = $this->flightSvc->filterFlights($user)->paginate();
$where = ['active' => true];
if (setting('pilots.only_flights_from_current', false)) {
$where['dpt_airport_id'] = $user->curr_airport_id;
}
$flights = $this->flightRepo
->whereOrder($where, 'flight_number', 'asc')
->paginate();
foreach($flights as $flight) {
$this->flightSvc->filterSubfleets($user, $flight);
@@ -67,9 +76,15 @@ class FlightController extends RestController
$user = Auth::user();
try {
$where = ['active' => true];
if (setting('pilots.only_flights_from_current')) {
$where['dpt_airport_id'] = Auth::user()->curr_airport_id;
}
$this->flightRepo->searchCriteria($request);
$this->flightRepo->pushCriteria(new RequestCriteria($request));
$flights = $this->flightSvc->filterFlights($user)->paginate();
$this->flightRepo->pushCriteria(new WhereCriteria($request, $where));
$flights = $this->flightRepo->paginate();
} catch (RepositoryException $e) {
return response($e, 503);
}

View File

@@ -71,7 +71,7 @@ class FlightTest extends TestCase
public function testFindAllFlights()
{
$this->user = factory(App\Models\User::class)->create();
factory(App\Models\Flight::class, 70)->create([
factory(App\Models\Flight::class, 20)->create([
'airline_id' => $this->user->airline_id
]);
@@ -81,7 +81,7 @@ class FlightTest extends TestCase
$this->assertEquals(2, $body['meta']['last_page']);
$res = $this->get('/api/flights?page=2');
$res->assertJsonCount(20, 'data');
$res->assertJsonCount(5, 'data');
}
public function testFlightSearchApi()