intitial work on rest api
This commit is contained in:
parent
e19b84a078
commit
bc94772ce2
18
app/Http/Controllers/Api/BaseController.php
Normal file
18
app/Http/Controllers/Api/BaseController.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use Illuminate\Http\Response;
|
||||
use App\Http\Controllers\AppBaseController;
|
||||
|
||||
|
||||
class BaseController extends AppBaseController
|
||||
{
|
||||
public function status()
|
||||
{
|
||||
return response()->json(
|
||||
['message' => 'hi'],
|
||||
200
|
||||
);
|
||||
}
|
||||
}
|
39
app/Http/Controllers/Api/FlightController.php
Normal file
39
app/Http/Controllers/Api/FlightController.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
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;
|
||||
|
||||
|
||||
class FlightController extends AppBaseController
|
||||
{
|
||||
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();
|
||||
return fractal($flights, new FlightTransformer())->respond();
|
||||
}
|
||||
}
|
40
app/Http/Middleware/MeasureExecutionTime.php
Normal file
40
app/Http/Middleware/MeasureExecutionTime.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
/**
|
||||
* https://stackoverflow.com/a/34894933
|
||||
*/
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
|
||||
class MeasureExecutionTime
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
// Get the response
|
||||
$response = $next($request);
|
||||
|
||||
// Calculate execution time
|
||||
$executionTime = microtime(true) - LUMEN_START;
|
||||
|
||||
// I assume you're using valid json in your responses
|
||||
// Then I manipulate them below
|
||||
$content = json_decode($response->getContent(), true) + [
|
||||
'execution_time' => $executionTime,
|
||||
];
|
||||
|
||||
// Change the content of your response
|
||||
$response->setData($content);
|
||||
|
||||
// Return the response
|
||||
return $response;
|
||||
}
|
||||
}
|
44
app/Models/Transformers/FlightTransformer.php
Normal file
44
app/Models/Transformers/FlightTransformer.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Transformers;
|
||||
|
||||
use League\Fractal\TransformerAbstract;
|
||||
|
||||
use App\Models\Flight;
|
||||
|
||||
|
||||
class FlightTransformer extends TransformerAbstract
|
||||
{
|
||||
public function transform(Flight $flight)
|
||||
{
|
||||
$ret = [
|
||||
'id' => $flight->id,
|
||||
'airline' => [
|
||||
'id' => $flight->airline->id,
|
||||
'code' => $flight->airline->code,
|
||||
'name' => $flight->airline->name,
|
||||
],
|
||||
'dpt' => [
|
||||
'id' => $flight->dpt_airport->id,
|
||||
'icao' => $flight->dpt_airport->icao,
|
||||
'name' => $flight->dpt_airport->name,
|
||||
],
|
||||
'arr' => [
|
||||
'id' => $flight->arr_airport->id,
|
||||
'icao' => $flight->arr_airport->icao,
|
||||
'name' => $flight->arr_airport->name,
|
||||
],
|
||||
'alt' => [],
|
||||
];
|
||||
|
||||
if($flight->alt_airport_id) {
|
||||
$flight['alt'] = [
|
||||
'id' => $flight->alt_airport->id,
|
||||
'icao' => $flight->alt_airport->icao,
|
||||
'name' => $flight->alt_airport->name,
|
||||
];
|
||||
}
|
||||
|
||||
return $ret;
|
||||
}
|
||||
}
|
@ -66,7 +66,10 @@ class RouteServiceProvider extends ServiceProvider
|
||||
protected function mapApiRoutes()
|
||||
{
|
||||
Route::group([
|
||||
'middleware' => 'api',
|
||||
'middleware' => [
|
||||
'api',
|
||||
\App\Http\Middleware\MeasureExecutionTime::class
|
||||
],
|
||||
'namespace' => $this->namespace."\\API",
|
||||
'prefix' => 'api',
|
||||
'as' => 'api.',
|
||||
|
@ -11,6 +11,8 @@
|
||||
|
|
||||
*/
|
||||
|
||||
define('LUMEN_START', microtime(true));
|
||||
|
||||
$app = new Illuminate\Foundation\Application(
|
||||
realpath(__DIR__.'/../')
|
||||
);
|
||||
|
@ -58,6 +58,8 @@ return [
|
||||
InfyOm\AdminLTETemplates\AdminLTETemplatesServiceProvider::class,
|
||||
Zizaco\Entrust\EntrustServiceProvider::class,
|
||||
anlutro\LaravelSettings\ServiceProvider::class,
|
||||
Spatie\Fractal\FractalServiceProvider::class,
|
||||
|
||||
|
||||
/*
|
||||
* Application Service Providers...
|
||||
@ -72,7 +74,6 @@ return [
|
||||
],
|
||||
|
||||
'aliases' => [
|
||||
|
||||
'App' => Illuminate\Support\Facades\App::class,
|
||||
'Artisan' => Illuminate\Support\Facades\Artisan::class,
|
||||
'Auth' => Illuminate\Support\Facades\Auth::class,
|
||||
@ -87,8 +88,13 @@ return [
|
||||
'Entrust' => Zizaco\Entrust\EntrustFacade::class,
|
||||
'Event' => Illuminate\Support\Facades\Event::class,
|
||||
'File' => Illuminate\Support\Facades\File::class,
|
||||
'Flash' => Laracasts\Flash\Flash::class,
|
||||
'Form' => Collective\Html\FormFacade::class,
|
||||
'Fractal' => Spatie\Fractal\FractalFacade::class,
|
||||
'Gate' => Illuminate\Support\Facades\Gate::class,
|
||||
'Geotools' => Toin0u\Geotools\Facade\Geotools::class,
|
||||
'Hash' => Illuminate\Support\Facades\Hash::class,
|
||||
'Html' => Collective\Html\HtmlFacade::class,
|
||||
'Lang' => Illuminate\Support\Facades\Lang::class,
|
||||
'Log' => Illuminate\Support\Facades\Log::class,
|
||||
'Mail' => Illuminate\Support\Facades\Mail::class,
|
||||
@ -102,17 +108,13 @@ return [
|
||||
'Route' => Illuminate\Support\Facades\Route::class,
|
||||
'Schema' => Illuminate\Support\Facades\Schema::class,
|
||||
'Session' => Illuminate\Support\Facades\Session::class,
|
||||
'Setting' => anlutro\LaravelSettings\Facade::class,
|
||||
'Storage' => Illuminate\Support\Facades\Storage::class,
|
||||
'URL' => Illuminate\Support\Facades\URL::class,
|
||||
'Utils' => App\Facades\Utils::class,
|
||||
'Validator' => Illuminate\Support\Facades\Validator::class,
|
||||
'View' => Illuminate\Support\Facades\View::class,
|
||||
'Form' => Collective\Html\FormFacade::class,
|
||||
'Html' => Collective\Html\HtmlFacade::class,
|
||||
'Flash' => Laracasts\Flash\Flash::class,
|
||||
'Yaml' => Symfony\Component\Yaml\Yaml::class,
|
||||
'Geotools' => Toin0u\Geotools\Facade\Geotools::class,
|
||||
'Setting' => anlutro\LaravelSettings\Facade::class,
|
||||
'Utils' => App\Facades\Utils::class,
|
||||
],
|
||||
|
||||
];
|
||||
|
18
config/laravel-fractal.php
Normal file
18
config/laravel-fractal.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Default Serializer
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| The default serializer to be used when performing a transformation. It
|
||||
| may be left empty to use Fractal's default one. This can either be a
|
||||
| string or a League\Fractal\Serializer\SerializerAbstract subclass.
|
||||
|
|
||||
*/
|
||||
|
||||
'default_serializer' => '',
|
||||
|
||||
];
|
@ -13,6 +13,8 @@ use Illuminate\Http\Request;
|
||||
|
|
||||
*/
|
||||
|
||||
Route::get('/user', function (Request $request) {
|
||||
return $request->user();
|
||||
})->middleware('auth:api');
|
||||
Route::group([], function () {
|
||||
|
||||
Route::match(['get'], 'status', 'BaseController@status');
|
||||
Route::match(['get'], 'flights/search', 'FlightController@search');
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user