move filterSubfleets() to FlightService class #170
This commit is contained in:
parent
f6b2102e48
commit
13b4a3854b
@ -1,5 +1,4 @@
|
||||
# Changelog
|
||||
|
||||
## Alpha 2
|
||||
|
||||
!! 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
|
||||
- PIREP hours can't be changed after it's no longer in a pending state
|
||||
- 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
|
||||
|
||||
### Fixes
|
||||
#### Fixes
|
||||
|
||||
- PIREP fields being set when filing manually is working
|
||||
- Field for the rank's image changed to string input
|
||||
- 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)
|
||||
|
||||
***
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Services\FlightService;
|
||||
use Auth;
|
||||
use Illuminate\Http\Request;
|
||||
use Prettus\Repository\Criteria\RequestCriteria;
|
||||
@ -17,39 +18,18 @@ use App\Http\Resources\Flight as FlightResource;
|
||||
*/
|
||||
class FlightController extends RestController
|
||||
{
|
||||
protected $flightRepo, $userSvc;
|
||||
protected $flightRepo, $flightSvc, $userSvc;
|
||||
|
||||
public function __construct(
|
||||
FlightRepository $flightRepo,
|
||||
FlightService $flightSvc,
|
||||
UserService $userSvc
|
||||
) {
|
||||
$this->flightRepo = $flightRepo;
|
||||
$this->flightSvc = $flightSvc;
|
||||
$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
|
||||
*/
|
||||
@ -61,7 +41,7 @@ class FlightController extends RestController
|
||||
|
||||
$user = Auth::user();
|
||||
foreach($flights as $flight) {
|
||||
$this->filterSubfleets($user, $flight);
|
||||
$this->flightSvc->filterSubfleets($user, $flight);
|
||||
}
|
||||
|
||||
return FlightResource::collection($flights);
|
||||
@ -74,7 +54,7 @@ class FlightController extends RestController
|
||||
public function get($id)
|
||||
{
|
||||
$flight = $this->flightRepo->find($id);
|
||||
$this->filterSubfleets(Auth::user(), $flight);
|
||||
$this->flightSvc->filterSubfleets(Auth::user(), $flight);
|
||||
|
||||
FlightResource::withoutWrapping();
|
||||
return new FlightResource($flight);
|
||||
@ -96,7 +76,7 @@ class FlightController extends RestController
|
||||
|
||||
$user = Auth::user();
|
||||
foreach ($flights as $flight) {
|
||||
$this->filterSubfleets($user, $flight);
|
||||
$this->flightSvc->filterSubfleets($user, $flight);
|
||||
}
|
||||
|
||||
return FlightResource::collection($flights);
|
||||
|
@ -16,6 +16,34 @@ use App\Models\UserBid;
|
||||
|
||||
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
|
||||
* @param Flight $flight
|
||||
|
Loading…
Reference in New Issue
Block a user