2017-12-08 07:22:15 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
use App\Contracts\Controller;
|
2019-07-16 03:51:35 +08:00
|
|
|
use App\Http\Resources\Airport as AirportResource;
|
2019-08-27 00:32:46 +08:00
|
|
|
use App\Http\Resources\AirportDistance as AirportDistanceResource;
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Repositories\AirportRepository;
|
2019-08-23 02:32:49 +08:00
|
|
|
use App\Services\AirportService;
|
2018-01-06 05:30:35 +08:00
|
|
|
use Illuminate\Http\Request;
|
2017-12-08 07:22:15 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
/**
|
|
|
|
* Class AirportController
|
|
|
|
*/
|
|
|
|
class AirportController extends Controller
|
2017-12-08 07:22:15 +08:00
|
|
|
{
|
2018-03-20 09:50:40 +08:00
|
|
|
private $airportRepo;
|
2019-08-23 02:32:49 +08:00
|
|
|
private $airportSvc;
|
2017-12-08 07:22:15 +08:00
|
|
|
|
2018-02-21 12:33:09 +08:00
|
|
|
/**
|
|
|
|
* AirportController constructor.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-21 12:33:09 +08:00
|
|
|
* @param AirportRepository $airportRepo
|
2019-08-23 02:32:49 +08:00
|
|
|
* @param AirportService $airportSvc
|
2018-02-21 12:33:09 +08:00
|
|
|
*/
|
2017-12-08 07:22:15 +08:00
|
|
|
public function __construct(
|
2019-08-23 02:32:49 +08:00
|
|
|
AirportRepository $airportRepo,
|
|
|
|
AirportService $airportSvc
|
2017-12-08 07:22:15 +08:00
|
|
|
) {
|
|
|
|
$this->airportRepo = $airportRepo;
|
2019-08-23 02:32:49 +08:00
|
|
|
$this->airportSvc = $airportSvc;
|
2017-12-08 07:22:15 +08:00
|
|
|
}
|
|
|
|
|
2018-01-06 00:23:26 +08:00
|
|
|
/**
|
|
|
|
* Return all the airports, paginated
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-21 12:33:09 +08:00
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-21 12:33:09 +08:00
|
|
|
* @return mixed
|
2018-01-06 00:23:26 +08:00
|
|
|
*/
|
2018-01-06 05:30:35 +08:00
|
|
|
public function index(Request $request)
|
2018-01-06 00:23:26 +08:00
|
|
|
{
|
2018-01-06 05:30:35 +08:00
|
|
|
$where = [];
|
|
|
|
if ($request->filled('hub')) {
|
|
|
|
$where['hub'] = $request->get('hub');
|
|
|
|
}
|
|
|
|
|
|
|
|
$airports = $this->airportRepo
|
|
|
|
->whereOrder($where, 'icao', 'asc')
|
2018-03-01 05:06:57 +08:00
|
|
|
->paginate();
|
2018-01-06 05:30:35 +08:00
|
|
|
|
|
|
|
return AirportResource::collection($airports);
|
|
|
|
}
|
|
|
|
|
2018-02-21 12:33:09 +08:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
|
|
|
|
*/
|
2018-01-06 05:30:35 +08:00
|
|
|
public function index_hubs()
|
|
|
|
{
|
|
|
|
$where = [
|
|
|
|
'hub' => true,
|
|
|
|
];
|
|
|
|
|
|
|
|
$airports = $this->airportRepo
|
|
|
|
->whereOrder($where, 'icao', 'asc')
|
2018-03-01 05:06:57 +08:00
|
|
|
->paginate();
|
2018-01-06 05:30:35 +08:00
|
|
|
|
2018-01-06 00:23:26 +08:00
|
|
|
return AirportResource::collection($airports);
|
|
|
|
}
|
|
|
|
|
2017-12-12 12:05:22 +08:00
|
|
|
/**
|
|
|
|
* Do a lookup, via vaCentral, for the airport information
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-12 12:05:22 +08:00
|
|
|
* @param $id
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-12 12:05:22 +08:00
|
|
|
* @return AirportResource
|
|
|
|
*/
|
|
|
|
public function get($id)
|
|
|
|
{
|
|
|
|
$id = strtoupper($id);
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2017-12-12 12:05:22 +08:00
|
|
|
return new AirportResource($this->airportRepo->find($id));
|
|
|
|
}
|
|
|
|
|
2017-12-08 07:22:15 +08:00
|
|
|
/**
|
|
|
|
* Do a lookup, via vaCentral, for the airport information
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-08 07:22:15 +08:00
|
|
|
* @param $id
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-08 07:22:15 +08:00
|
|
|
* @return AirportResource
|
|
|
|
*/
|
|
|
|
public function lookup($id)
|
|
|
|
{
|
2019-08-23 02:32:49 +08:00
|
|
|
$airport = $this->airportSvc->lookupAirport($id);
|
2017-12-08 07:22:15 +08:00
|
|
|
return new AirportResource(collect($airport));
|
|
|
|
}
|
2019-08-27 00:32:46 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Do a lookup, via vaCentral, for the airport information
|
|
|
|
*
|
|
|
|
* @param $fromIcao
|
|
|
|
* @param $toIcao
|
|
|
|
*
|
|
|
|
* @return AirportDistanceResource
|
|
|
|
*/
|
|
|
|
public function distance($fromIcao, $toIcao)
|
|
|
|
{
|
|
|
|
$distance = $this->airportSvc->calculateDistance($fromIcao, $toIcao);
|
|
|
|
return new AirportDistanceResource([
|
|
|
|
'fromIcao' => $fromIcao,
|
|
|
|
'toIcao' => $toIcao,
|
|
|
|
'distance' => $distance,
|
|
|
|
]);
|
|
|
|
}
|
2017-12-08 07:22:15 +08:00
|
|
|
}
|