phpvms/app/Http/Controllers/Api/AirportController.php

107 lines
2.4 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers\Api;
2018-02-21 12:33:09 +08:00
use App\Http\Resources\Airport as AirportResource;
use App\Interfaces\Controller;
2018-02-21 12:33:09 +08:00
use App\Repositories\AirportRepository;
2018-01-06 05:30:35 +08:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
2018-02-21 12:33:09 +08:00
use Log;
use VaCentral\Airport as AirportLookup;
/**
* Class AirportController
*/
class AirportController extends Controller
{
private $airportRepo;
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
*/
public function __construct(
AirportRepository $airportRepo
) {
$this->airportRepo = $airportRepo;
}
/**
* 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 05:30:35 +08:00
public function index(Request $request)
{
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')
->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')
->paginate();
2018-01-06 05:30:35 +08:00
return AirportResource::collection($airports);
}
/**
* Do a lookup, via vaCentral, for the airport information
2018-08-27 00:40:04 +08:00
*
* @param $id
2018-08-27 00:40:04 +08:00
*
* @return AirportResource
*/
public function get($id)
{
$id = strtoupper($id);
return new AirportResource($this->airportRepo->find($id));
}
/**
* Do a lookup, via vaCentral, for the airport information
2018-08-27 00:40:04 +08:00
*
* @param $id
2018-08-27 00:40:04 +08:00
*
* @return AirportResource
*/
public function lookup($id)
{
$airport = Cache::remember(
config('cache.keys.AIRPORT_VACENTRAL_LOOKUP.key').$id,
config('cache.keys.AIRPORT_VACENTRAL_LOOKUP.time'),
function () use ($id) {
try {
return AirportLookup::get($id);
} catch (\VaCentral\HttpException $e) {
Log::error($e);
return [];
}
}
);
return new AirportResource(collect($airport));
}
}