Filter out flights depending on the only_flights_from_current setting #174
This commit is contained in:
parent
9b55a9691f
commit
17f1085ffb
@ -35,11 +35,9 @@ class FlightController extends RestController
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$flights = $this->flightRepo
|
||||
->orderBy('flight_number', 'asc')
|
||||
->paginate(50);
|
||||
|
||||
$user = Auth::user();
|
||||
$flights = $this->flightSvc->filterFlights($user)->paginate();
|
||||
|
||||
foreach($flights as $flight) {
|
||||
$this->flightSvc->filterSubfleets($user, $flight);
|
||||
}
|
||||
@ -66,15 +64,16 @@ class FlightController extends RestController
|
||||
*/
|
||||
public function search(Request $request)
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
try {
|
||||
$this->flightRepo->searchCriteria($request);
|
||||
$this->flightRepo->pushCriteria(new RequestCriteria($request));
|
||||
$flights = $this->flightRepo->paginate();
|
||||
$flights = $this->flightSvc->filterFlights($user)->paginate();
|
||||
} catch (RepositoryException $e) {
|
||||
return response($e, 503);
|
||||
}
|
||||
|
||||
$user = Auth::user();
|
||||
foreach ($flights as $flight) {
|
||||
$this->flightSvc->filterSubfleets($user, $flight);
|
||||
}
|
||||
|
@ -8,7 +8,9 @@
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Repositories\FlightRepository;
|
||||
use function foo\func;
|
||||
use Illuminate\Support\Collection;
|
||||
use Log;
|
||||
|
||||
use App\Models\Flight;
|
||||
@ -17,13 +19,30 @@ use App\Models\UserBid;
|
||||
|
||||
class FlightService extends BaseService
|
||||
{
|
||||
protected $userSvc;
|
||||
protected $flightRepo, $userSvc;
|
||||
|
||||
public function __construct(UserService $userSvc)
|
||||
public function __construct(FlightRepository $flightRepo, UserService $userSvc)
|
||||
{
|
||||
$this->flightRepo = $flightRepo;
|
||||
$this->userSvc = $userSvc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter out any flights according to different settings
|
||||
* @param $user
|
||||
* @return FlightRepository
|
||||
*/
|
||||
public function filterFlights($user)
|
||||
{
|
||||
$where = [];
|
||||
if (setting('pilots.only_flights_from_current', false)) {
|
||||
$where['dpt_airport_id'] = $user->curr_airport_id;
|
||||
}
|
||||
|
||||
return $this->flightRepo
|
||||
->whereOrder($where, 'flight_number', 'asc');
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter out subfleets to only include aircraft that a user has access to
|
||||
* @param $user
|
||||
|
@ -160,6 +160,11 @@ class UserTest extends TestCase
|
||||
/*
|
||||
* Do some sanity checks first
|
||||
*/
|
||||
|
||||
# Make sure no flights are filtered out
|
||||
$this->settingsRepo->store('pilots.only_flights_from_current', false);
|
||||
|
||||
# And restrict the aircraft
|
||||
$this->settingsRepo->store('pireps.restrict_aircraft_to_rank', false);
|
||||
|
||||
$response = $this->get('/api/flights/' . $flight->id, [], $user);
|
||||
@ -183,8 +188,8 @@ class UserTest extends TestCase
|
||||
* Make sure it's filtered out from the flight list
|
||||
*/
|
||||
$response = $this->get('/api/flights', [], $user);
|
||||
$response->assertStatus(200);
|
||||
$body = $response->json();
|
||||
$response->assertStatus(200);
|
||||
$this->assertCount(1, $body['data'][0]['subfleets']);
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user