From 13b4a3854bd2002e456be6ab27545543b8344598 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Fri, 9 Feb 2018 14:36:36 -0600 Subject: [PATCH] move filterSubfleets() to FlightService class #170 --- CHANGELOG.md | 7 ++-- app/Http/Controllers/Api/FlightController.php | 34 ++++--------------- app/Services/FlightService.php | 28 +++++++++++++++ 3 files changed, 38 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cc649223..88016835 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) *** diff --git a/app/Http/Controllers/Api/FlightController.php b/app/Http/Controllers/Api/FlightController.php index 5e9865d6..f485eea3 100644 --- a/app/Http/Controllers/Api/FlightController.php +++ b/app/Http/Controllers/Api/FlightController.php @@ -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); diff --git a/app/Services/FlightService.php b/app/Services/FlightService.php index b40d1631..eafc28b3 100644 --- a/app/Services/FlightService.php +++ b/app/Services/FlightService.php @@ -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