move filterSubfleets() to FlightService class #170

This commit is contained in:
Nabeel Shahzad 2018-02-09 14:36:36 -06:00
parent f6b2102e48
commit 13b4a3854b
3 changed files with 38 additions and 31 deletions

View File

@ -1,5 +1,4 @@
# Changelog # Changelog
## Alpha 2 ## Alpha 2
!! Please do a full reinstall, with recreating the database !! Please do a full reinstall, with recreating the database
@ -8,15 +7,15 @@
- Add a `SKIN_NAME` template variable to reference the current skin, vs hardcoding the skin name in the templates - Add a `SKIN_NAME` template variable to reference the current skin, vs hardcoding the skin name in the templates
- PIREP hours can't be changed after it's no longer in a pending state - PIREP hours can't be changed after it's no longer in a pending state
- DB: `airport.tz` to `airport.timezone` - DB: `airport.tz` to `airport.timezone`
- API: Most calls, with exception of ACARS, are now private and require an API key to access - API: Most calls, with exception of ACARS, are now private and require an API key to access [#173](https://github.com/nabeelio/phpvms/issues/173)
- API: Allow a `fields` object to set custom PIREP fields, also returns the current values - API: Allow a `fields` object to set custom PIREP fields, also returns the current values
### Fixes #### Fixes
- PIREP fields being set when filing manually is working - PIREP fields being set when filing manually is working
- Field for the rank's image changed to string input - Field for the rank's image changed to string input
- API: Fixed typo from `subfleet` to `subfleets` in the `/api/flights` call(s) - API: Fixed typo from `subfleet` to `subfleets` in the `/api/flights` call(s)
- API: Subfleets returned in the flight calls respect the `pireps.restrict_aircraft_to_rank` setting - API: Subfleets returned in the flight calls respect the `pireps.restrict_aircraft_to_rank` setting [#170](https://github.com/nabeelio/phpvms/issues/170)
*** ***

View File

@ -2,6 +2,7 @@
namespace App\Http\Controllers\Api; namespace App\Http\Controllers\Api;
use App\Services\FlightService;
use Auth; use Auth;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Prettus\Repository\Criteria\RequestCriteria; use Prettus\Repository\Criteria\RequestCriteria;
@ -17,39 +18,18 @@ use App\Http\Resources\Flight as FlightResource;
*/ */
class FlightController extends RestController class FlightController extends RestController
{ {
protected $flightRepo, $userSvc; protected $flightRepo, $flightSvc, $userSvc;
public function __construct( public function __construct(
FlightRepository $flightRepo, FlightRepository $flightRepo,
FlightService $flightSvc,
UserService $userSvc UserService $userSvc
) { ) {
$this->flightRepo = $flightRepo; $this->flightRepo = $flightRepo;
$this->flightSvc = $flightSvc;
$this->userSvc = $userSvc; $this->userSvc = $userSvc;
} }
/**
* Filter out subfleets to only include aircraft that a user has access to
* @param $user
* @param $flight
* @return mixed
*/
public function filterSubfleets($user, $flight)
{
if(setting('pireps.restrict_aircraft_to_rank', false) === false) {
return $flight;
}
$allowed_subfleets = $this->userSvc->getAllowableSubfleets($user)->pluck('id');
$flight->subfleets = $flight->subfleets->filter(
function($subfleet, $item) use ($allowed_subfleets) {
if ($allowed_subfleets->contains($subfleet->id)) {
return true;
}
});
return $flight;
}
/** /**
* Return all the flights, paginated * Return all the flights, paginated
*/ */
@ -61,7 +41,7 @@ class FlightController extends RestController
$user = Auth::user(); $user = Auth::user();
foreach($flights as $flight) { foreach($flights as $flight) {
$this->filterSubfleets($user, $flight); $this->flightSvc->filterSubfleets($user, $flight);
} }
return FlightResource::collection($flights); return FlightResource::collection($flights);
@ -74,7 +54,7 @@ class FlightController extends RestController
public function get($id) public function get($id)
{ {
$flight = $this->flightRepo->find($id); $flight = $this->flightRepo->find($id);
$this->filterSubfleets(Auth::user(), $flight); $this->flightSvc->filterSubfleets(Auth::user(), $flight);
FlightResource::withoutWrapping(); FlightResource::withoutWrapping();
return new FlightResource($flight); return new FlightResource($flight);
@ -96,7 +76,7 @@ class FlightController extends RestController
$user = Auth::user(); $user = Auth::user();
foreach ($flights as $flight) { foreach ($flights as $flight) {
$this->filterSubfleets($user, $flight); $this->flightSvc->filterSubfleets($user, $flight);
} }
return FlightResource::collection($flights); return FlightResource::collection($flights);

View File

@ -16,6 +16,34 @@ use App\Models\UserBid;
class FlightService extends BaseService class FlightService extends BaseService
{ {
protected $userSvc;
public function __construct(UserService $userSvc)
{
$this->userSvc = $userSvc;
}
/**
* Filter out subfleets to only include aircraft that a user has access to
* @param $user
* @param $flight
* @return mixed
*/
public function filterSubfleets($user, $flight)
{
if (setting('pireps.restrict_aircraft_to_rank', false)) {
$allowed_subfleets = $this->userSvc->getAllowableSubfleets($user)->pluck('id');
$flight->subfleets = $flight->subfleets->filter(
function ($subfleet, $item) use ($allowed_subfleets) {
if ($allowed_subfleets->contains($subfleet->id)) {
return true;
}
});
}
return $flight;
}
/** /**
* Delete a flight, and all the user bids, etc associated with it * Delete a flight, and all the user bids, etc associated with it
* @param Flight $flight * @param Flight $flight