Convert flight transformer to the native Laravel resource object
This commit is contained in:
parent
2e1c0a75d4
commit
373f45c86c
4
Makefile
4
Makefile
@ -20,6 +20,10 @@ clean:
|
|||||||
@php artisan route:clear
|
@php artisan route:clear
|
||||||
@php artisan config:clear
|
@php artisan config:clear
|
||||||
|
|
||||||
|
.PHONY: clean-routes
|
||||||
|
clean-routes:
|
||||||
|
@php artisan route:clear
|
||||||
|
|
||||||
.PHONY: build
|
.PHONY: build
|
||||||
build:
|
build:
|
||||||
@composer install --no-interaction
|
@composer install --no-interaction
|
||||||
|
@ -7,27 +7,33 @@ use Illuminate\Http\Request;
|
|||||||
use App\Http\Controllers\AppBaseController;
|
use App\Http\Controllers\AppBaseController;
|
||||||
use App\Models\Transformers\FlightTransformer;
|
use App\Models\Transformers\FlightTransformer;
|
||||||
use App\Repositories\FlightRepository;
|
use App\Repositories\FlightRepository;
|
||||||
|
use App\Http\Resources\Flight as FlightResource;
|
||||||
|
use Prettus\Repository\Exceptions\RepositoryException;
|
||||||
|
|
||||||
|
|
||||||
class FlightController extends AppBaseController
|
class FlightController extends AppBaseController
|
||||||
{
|
{
|
||||||
protected $flightRepo;
|
protected $flightRepo;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(FlightRepository $flightRepo) {
|
||||||
FlightRepository $flightRepo
|
|
||||||
) {
|
|
||||||
$this->flightRepo = $flightRepo;
|
$this->flightRepo = $flightRepo;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get($id)
|
public function get($id)
|
||||||
{
|
{
|
||||||
$flight = $this->flightRepo->find($id);
|
$flight = $this->flightRepo->find($id);
|
||||||
return fractal($flight, new FlightTransformer())->respond();
|
FlightResource::withoutWrapping();
|
||||||
|
return new FlightResource($flight);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function search(Request $request)
|
public function search(Request $request)
|
||||||
{
|
{
|
||||||
|
try {
|
||||||
$flights = $this->flightRepo->searchCriteria($request)->paginate();
|
$flights = $this->flightRepo->searchCriteria($request)->paginate();
|
||||||
return fractal($flights, new FlightTransformer())->respond();
|
} catch (RepositoryException $e) {
|
||||||
|
return response($e, 503);
|
||||||
|
}
|
||||||
|
return FlightResource::collection($flights);
|
||||||
|
//return fractal($flights, new FlightTransformer())->respond();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
36
app/Http/Resources/Flight.php
Normal file
36
app/Http/Resources/Flight.php
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Resources;
|
||||||
|
|
||||||
|
use Illuminate\Http\Resources\Json\Resource;
|
||||||
|
|
||||||
|
class Flight extends Resource
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Transform the resource into an array.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function toArray($request)
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $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,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
@ -1,25 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Models\Transformers;
|
|
||||||
|
|
||||||
use App\Models\Airport;
|
|
||||||
use League\Fractal\TransformerAbstract;
|
|
||||||
|
|
||||||
class AirportTransform extends TransformerAbstract
|
|
||||||
{
|
|
||||||
public function transform(Airport $ap)
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
'id' => $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,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,38 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Models\Transformers;
|
|
||||||
|
|
||||||
use App\Models\Flight;
|
|
||||||
use League\Fractal\TransformerAbstract;
|
|
||||||
|
|
||||||
|
|
||||||
class FlightTransformer extends TransformerAbstract
|
|
||||||
{
|
|
||||||
public static $aptXform = null;
|
|
||||||
|
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
FlightTransformer::$aptXform = new AirportTransform();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function transform(Flight $flight)
|
|
||||||
{
|
|
||||||
$ret = [
|
|
||||||
'id' => $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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -39,6 +39,10 @@ class FlightRepository extends BaseRepository implements CacheableInterface
|
|||||||
'active' => $only_active,
|
'active' => $only_active,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
if ($request->filled('flight_id')) {
|
||||||
|
$where['id'] = $request->flight_id;
|
||||||
|
}
|
||||||
|
|
||||||
if ($request->filled('airline_id')) {
|
if ($request->filled('airline_id')) {
|
||||||
$where['airline_id'] = $request->airline_id;
|
$where['airline_id'] = $request->airline_id;
|
||||||
}
|
}
|
||||||
|
15
phpvms.iml
15
phpvms.iml
@ -306,21 +306,6 @@
|
|||||||
</SOURCES>
|
</SOURCES>
|
||||||
</library>
|
</library>
|
||||||
</orderEntry>
|
</orderEntry>
|
||||||
<orderEntry type="module-library">
|
|
||||||
<library name="PHARS">
|
|
||||||
<CLASSES>
|
|
||||||
<root url="phar://$MODULE_DIR$/vendor/phar-io/manifest/tests/_fixture/test.phar/" />
|
|
||||||
<root url="phar://$MODULE_DIR$/vendor/nabeel/vacentral/vendor/phpunit/phpunit/tests/_files/phpunit-example-extension/tools/phpunit.d/phpunit-example-extension-1.0.0.phar/" />
|
|
||||||
<root url="phar://$MODULE_DIR$/vendor/phpunit/phpunit/tests/_files/phpunit-example-extension/tools/phpunit.d/phpunit-example-extension-1.0.1.phar/" />
|
|
||||||
</CLASSES>
|
|
||||||
<JAVADOC />
|
|
||||||
<SOURCES>
|
|
||||||
<root url="phar://$MODULE_DIR$/vendor/phar-io/manifest/tests/_fixture/test.phar/" />
|
|
||||||
<root url="phar://$MODULE_DIR$/vendor/nabeel/vacentral/vendor/phpunit/phpunit/tests/_files/phpunit-example-extension/tools/phpunit.d/phpunit-example-extension-1.0.0.phar/" />
|
|
||||||
<root url="phar://$MODULE_DIR$/vendor/phpunit/phpunit/tests/_files/phpunit-example-extension/tools/phpunit.d/phpunit-example-extension-1.0.1.phar/" />
|
|
||||||
</SOURCES>
|
|
||||||
</library>
|
|
||||||
</orderEntry>
|
|
||||||
</component>
|
</component>
|
||||||
<component name="TemplatesService">
|
<component name="TemplatesService">
|
||||||
<option name="TEMPLATE_FOLDERS">
|
<option name="TEMPLATE_FOLDERS">
|
||||||
|
@ -20,8 +20,8 @@ Route::group([], function () {
|
|||||||
Route::match(['get'], 'airports/{id}', 'AirportController@get');
|
Route::match(['get'], 'airports/{id}', 'AirportController@get');
|
||||||
Route::match(['get'], 'airports/{id}/lookup', 'AirportController@lookup');
|
Route::match(['get'], 'airports/{id}/lookup', 'AirportController@lookup');
|
||||||
|
|
||||||
Route::match(['get'], 'flight/{id}', 'FlightController@get');
|
|
||||||
Route::match(['get'], 'flights/search', 'FlightController@search');
|
Route::match(['get'], 'flights/search', 'FlightController@search');
|
||||||
|
Route::match(['get'], 'flights/{id}', 'FlightController@get');
|
||||||
|
|
||||||
Route::match(['get'], 'pirep/{id}', 'PirepController@get');
|
Route::match(['get'], 'pirep/{id}', 'PirepController@get');
|
||||||
});
|
});
|
||||||
|
@ -24,7 +24,24 @@ class FlightTest extends TestCase
|
|||||||
public function testGetFlight()
|
public function testGetFlight()
|
||||||
{
|
{
|
||||||
$flight_id = $this->addFlight();
|
$flight_id = $this->addFlight();
|
||||||
$this->get('/api/flight/'.$flight_id, self::$auth_headers)
|
$this->get('/api/flights/'.$flight_id, self::$auth_headers)
|
||||||
->assertStatus(200);
|
->assertStatus(200)
|
||||||
|
->assertJson(['dpt_airport_id' => 'KAUS']);
|
||||||
|
|
||||||
|
$this->get('/api/flights/INVALID', self::$auth_headers)
|
||||||
|
->assertStatus(404);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Search based on all different criteria
|
||||||
|
*/
|
||||||
|
public function testSearchFlight()
|
||||||
|
{
|
||||||
|
$flight_id = $this->addFlight();
|
||||||
|
|
||||||
|
# search specifically for a flight ID
|
||||||
|
$query = 'flight_id='.$flight_id;
|
||||||
|
$req = $this->get('/api/flights/search?' . $query, self::$auth_headers);
|
||||||
|
$req->assertStatus(200);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user