From e032fd2dbf2ad31a59ad203ca0ae1899a5d446af Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Mon, 4 Dec 2017 10:59:25 -0600 Subject: [PATCH] cleanup searchCriteria generation for flights --- app/Http/Controllers/Api/FlightController.php | 34 ++++++------------- .../Controllers/Frontend/FlightController.php | 18 +++++----- app/Repositories/FlightRepository.php | 31 +++++++++++++++++ 3 files changed, 52 insertions(+), 31 deletions(-) diff --git a/app/Http/Controllers/Api/FlightController.php b/app/Http/Controllers/Api/FlightController.php index 0909e30f..c852f62d 100644 --- a/app/Http/Controllers/Api/FlightController.php +++ b/app/Http/Controllers/Api/FlightController.php @@ -4,42 +4,30 @@ namespace App\Http\Controllers\Api; use Illuminate\Http\Request; -use App\Models\Airline; -use App\Models\Airport; -use App\Models\Flight; -use App\Models\Transformers\FlightTransformer; - use App\Http\Controllers\AppBaseController; +use App\Models\Transformers\FlightTransformer; +use App\Repositories\FlightRepository; class FlightController extends AppBaseController { + protected $flightRepo; + + public function __construct( + FlightRepository $flightRepo + ) { + $this->flightRepo = $flightRepo; + } public function get($id) { - $flight = Flight::find($id); + $flight = $this->flightRepo->find($id); return fractal($flight, new FlightTransformer())->respond(); } public function search(Request $request) { - $where = []; - if($request->airline) { - $airline = Airline::where('code', $request->airline)->first()->id; - $where['airline_id'] = $airline; - } - - if($request->depICAO) { - $airport = Airport::where('icao', $request->depICAO)->first()->id; - $where['dpt_airport_id'] = $airport; - } - - if($request->arrICAO) { - $airport = Airport::where('icao', $request->depICAO)->first()->id; - $where['dpt_airport_id'] = $airport; - } - - $flights = Flight::where($where)->get(); + $flights = $this->flightRepo->searchCriteria($request)->paginate(); return fractal($flights, new FlightTransformer())->respond(); } } diff --git a/app/Http/Controllers/Frontend/FlightController.php b/app/Http/Controllers/Frontend/FlightController.php index b244cd7d..02d5f626 100644 --- a/app/Http/Controllers/Frontend/FlightController.php +++ b/app/Http/Controllers/Frontend/FlightController.php @@ -2,16 +2,18 @@ namespace App\Http\Controllers\Frontend; +use Log; + use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use App\Repositories\FlightRepository; use App\Http\Controllers\AppBaseController; -use App\Models\Flight; use App\Models\UserFlight; use App\Repositories\Criteria\WhereCriteria; use Mockery\Exception; use Prettus\Repository\Criteria\RequestCriteria; +use Prettus\Repository\Exceptions\RepositoryException; class FlightController extends AppBaseController { @@ -31,7 +33,12 @@ class FlightController extends AppBaseController $where['dpt_airport_id'] = Auth::user()->curr_airport_id; } - $this->flightRepo->pushCriteria(new WhereCriteria($request, $where)); + try { + $this->flightRepo->pushCriteria(new WhereCriteria($request, $where)); + } catch (RepositoryException $e) { + Log::emergency($e); + } + $flights = $this->flightRepo->paginate(); $saved_flights = UserFlight::where('user_id', Auth::id()) @@ -51,12 +58,7 @@ class FlightController extends AppBaseController */ public function search(Request $request) { - $where = ['active' => true]; - - $this->flightRepo->pushCriteria(new RequestCriteria($request)); - $flights = $this->flightRepo->paginate(); - - // TODO: PAGINATION + $flights = $this->flightRepo->searchCriteria($request)->paginate(); $saved_flights = UserFlight::where('user_id', Auth::id()) ->pluck('flight_id')->toArray(); diff --git a/app/Repositories/FlightRepository.php b/app/Repositories/FlightRepository.php index 0b36caf3..bdc2459a 100644 --- a/app/Repositories/FlightRepository.php +++ b/app/Repositories/FlightRepository.php @@ -3,6 +3,8 @@ namespace App\Repositories; use App\Models\Flight; +use App\Repositories\Criteria\WhereCriteria; +use Illuminate\Http\Request; use Prettus\Repository\Contracts\CacheableInterface; use Prettus\Repository\Traits\CacheableRepository; @@ -22,4 +24,33 @@ class FlightRepository extends BaseRepository implements CacheableInterface { return Flight::class; } + + /** + * Create the search criteria and return this with the stuff pushed + * @param Request $request + * @param bool $only_active + * @return $this + * @throws \Prettus\Repository\Exceptions\RepositoryException + */ + public function searchCriteria(Request $request, bool $only_active=true) + { + $where = [ + 'active' => $only_active, + ]; + + if ($request->airline) { + $where['airline_id'] = $request->airline; + } + + if ($request->depICAO) { + $where['dpt_airport_id'] = $request->depICAO; + } + + if ($request->arrICAO) { + $where['dpt_airport_id'] = $request->arrICAO; + } + + $this->pushCriteria(new WhereCriteria($request, $where)); + return $this; + } }