From 373f45c86c3bb3910dec5b9d2c0fe913487f0a3c Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Tue, 12 Dec 2017 07:25:11 -0600 Subject: [PATCH] Convert flight transformer to the native Laravel resource object --- Makefile | 4 ++ app/Http/Controllers/Api/FlightController.php | 18 ++++++--- app/Http/Resources/Flight.php | 36 ++++++++++++++++++ app/Models/Transformers/AirportTransform.php | 25 ------------ app/Models/Transformers/FlightTransformer.php | 38 ------------------- app/Repositories/FlightRepository.php | 4 ++ phpvms.iml | 15 -------- routes/api.php | 2 +- tests/FlightTest.php | 21 +++++++++- 9 files changed, 76 insertions(+), 87 deletions(-) create mode 100644 app/Http/Resources/Flight.php delete mode 100644 app/Models/Transformers/AirportTransform.php delete mode 100644 app/Models/Transformers/FlightTransformer.php diff --git a/Makefile b/Makefile index 2979ff33..0f9a7fb9 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,10 @@ clean: @php artisan route:clear @php artisan config:clear +.PHONY: clean-routes +clean-routes: + @php artisan route:clear + .PHONY: build build: @composer install --no-interaction diff --git a/app/Http/Controllers/Api/FlightController.php b/app/Http/Controllers/Api/FlightController.php index c852f62d..5b73ad6a 100644 --- a/app/Http/Controllers/Api/FlightController.php +++ b/app/Http/Controllers/Api/FlightController.php @@ -7,27 +7,33 @@ use Illuminate\Http\Request; use App\Http\Controllers\AppBaseController; use App\Models\Transformers\FlightTransformer; use App\Repositories\FlightRepository; +use App\Http\Resources\Flight as FlightResource; +use Prettus\Repository\Exceptions\RepositoryException; class FlightController extends AppBaseController { protected $flightRepo; - public function __construct( - FlightRepository $flightRepo - ) { + public function __construct(FlightRepository $flightRepo) { $this->flightRepo = $flightRepo; } public function get($id) { $flight = $this->flightRepo->find($id); - return fractal($flight, new FlightTransformer())->respond(); + FlightResource::withoutWrapping(); + return new FlightResource($flight); } public function search(Request $request) { - $flights = $this->flightRepo->searchCriteria($request)->paginate(); - return fractal($flights, new FlightTransformer())->respond(); + try { + $flights = $this->flightRepo->searchCriteria($request)->paginate(); + } catch (RepositoryException $e) { + return response($e, 503); + } + return FlightResource::collection($flights); + //return fractal($flights, new FlightTransformer())->respond(); } } diff --git a/app/Http/Resources/Flight.php b/app/Http/Resources/Flight.php new file mode 100644 index 00000000..7231be8f --- /dev/null +++ b/app/Http/Resources/Flight.php @@ -0,0 +1,36 @@ + $this->id, + 'airline' => $this->airline, + 'flight_number' => $this->flight_number, + 'route_code' => $this->route_code, + 'route_leg' => $this->route_leg, + 'dpt_airport_id' => $this->dpt_airport_id, + 'arr_airport_id' => $this->arr_airport_id, + 'alt_airport_id' => $this->alt_airport_id, + 'route' => $this->route, + 'dpt_time' => $this->dpt_time, + 'arr_time' => $this->arr_time, + 'flight_time' => $this->flight_time, + 'notes' => $this->notes, + 'active' => $this->active, + 'created_at' => $this->created_at, + 'updated_at' => $this->updated_at, + ]; + } +} diff --git a/app/Models/Transformers/AirportTransform.php b/app/Models/Transformers/AirportTransform.php deleted file mode 100644 index f9093141..00000000 --- a/app/Models/Transformers/AirportTransform.php +++ /dev/null @@ -1,25 +0,0 @@ - $ap->id, - 'icao' => $ap->icao, - 'name' => $ap->name, - 'location' => $ap->location, - 'country' => $ap->country, - 'fuel_100ll_cost' => $ap->fuel_100ll_cost, - 'fuel_jeta_cost' => $ap->fuel_jeta_cost, - 'fuel_mogas_cost' => $ap->fuel_mogas_cost, - 'lat' => $ap->lat, - 'lon' => $ap->lon, - ]; - } -} diff --git a/app/Models/Transformers/FlightTransformer.php b/app/Models/Transformers/FlightTransformer.php deleted file mode 100644 index 807c6a44..00000000 --- a/app/Models/Transformers/FlightTransformer.php +++ /dev/null @@ -1,38 +0,0 @@ - $flight->id, - 'airline' => [ - 'id' => $flight->airline->id, - 'code' => $flight->airline->code, - 'name' => $flight->airline->name, - ], - 'dpt' => FlightTransformer::$aptXform->transform($flight->dpt_airport), - 'arr' => FlightTransformer::$aptXform->transform($flight->arr_airport), - 'alt' => [], - ]; - - if($flight->alt_airport_id) { - $flight['alt'] = FlightTransformer::$aptXform->transform($flight->alt_airport); - } - - return $ret; - } -} diff --git a/app/Repositories/FlightRepository.php b/app/Repositories/FlightRepository.php index 10d3b884..c97b2cbb 100644 --- a/app/Repositories/FlightRepository.php +++ b/app/Repositories/FlightRepository.php @@ -39,6 +39,10 @@ class FlightRepository extends BaseRepository implements CacheableInterface 'active' => $only_active, ]; + if ($request->filled('flight_id')) { + $where['id'] = $request->flight_id; + } + if ($request->filled('airline_id')) { $where['airline_id'] = $request->airline_id; } diff --git a/phpvms.iml b/phpvms.iml index 4a276633..8b7db6af 100644 --- a/phpvms.iml +++ b/phpvms.iml @@ -306,21 +306,6 @@ - - - - - - - - - - - - - - -