list trips
add, remove flights
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -3,3 +3,5 @@
|
|||||||
Homestead.json
|
Homestead.json
|
||||||
Homestead.yaml
|
Homestead.yaml
|
||||||
.env
|
.env
|
||||||
|
|
||||||
|
tests/_output/*
|
||||||
@@ -45,10 +45,11 @@ class AirportController extends Controller
|
|||||||
public function resourceList(Request $request)
|
public function resourceList(Request $request)
|
||||||
{
|
{
|
||||||
$client = Client::create();
|
$client = Client::create();
|
||||||
|
$collection = $client->listAirports($request->input('autocomplete'));
|
||||||
|
|
||||||
|
//add pagination
|
||||||
$per_page = $request->input('per_page', 10);
|
$per_page = $request->input('per_page', 10);
|
||||||
$page = $request->input('page', 1);
|
$page = $request->input('page', 1);
|
||||||
|
|
||||||
$collection = $client->listAirports($request->input('autocomplete'));
|
|
||||||
$paginator = new LengthAwarePaginator(
|
$paginator = new LengthAwarePaginator(
|
||||||
$collection->forPage($page, $per_page),
|
$collection->forPage($page, $per_page),
|
||||||
$collection->count(),
|
$collection->count(),
|
||||||
@@ -56,8 +57,8 @@ class AirportController extends Controller
|
|||||||
$page
|
$page
|
||||||
);
|
);
|
||||||
|
|
||||||
$airports = new Collection($paginator, new AirportTransformer(), 'airports');
|
$airports = (new Collection($paginator, new AirportTransformer(), 'airports'))
|
||||||
$airports->setPaginator(new IlluminatePaginatorAdapter($paginator));
|
->setPaginator(new IlluminatePaginatorAdapter($paginator));
|
||||||
|
|
||||||
return $this->JsonApiResponse($airports, 200);
|
return $this->JsonApiResponse($airports, 200);
|
||||||
}
|
}
|
||||||
|
|||||||
77
app/Http/Controllers/FlightController.php
Normal file
77
app/Http/Controllers/FlightController.php
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Libraries\Trips\FlightTransform;
|
||||||
|
use App\Libraries\Trips\Models\Flights;
|
||||||
|
use App\Libraries\Trips\Models\Trips;
|
||||||
|
use App\Libraries\Trips\TripTransformer;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use League\Fractal\Manager;
|
||||||
|
use League\Fractal\Resource\Collection;
|
||||||
|
use League\Fractal\Resource\Item;
|
||||||
|
use League\Fractal\Resource\ResourceInterface;
|
||||||
|
use League\Fractal\Serializer\JsonApiSerializer;
|
||||||
|
|
||||||
|
class FlightController extends Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Create a new controller instance.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
public function listFlights(Request $request)
|
||||||
|
{
|
||||||
|
$flightCollection = Flights::all();
|
||||||
|
//TODO: add pagination
|
||||||
|
|
||||||
|
$result = new Collection($flightCollection, new FlightTransform(), 'flights');
|
||||||
|
|
||||||
|
return $this->JsonApiResponse($result, 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getFlight(Request $request, $id)
|
||||||
|
{
|
||||||
|
$flight = Flights::find($id);
|
||||||
|
|
||||||
|
$result = new Item($flight, new FlightTransform(), 'flights');
|
||||||
|
|
||||||
|
return $this->JsonApiResponse($result, 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function removeFlight(Request $request, $id)
|
||||||
|
{
|
||||||
|
/** @var \App\Libraries\Trips\Models\Flights $flight */
|
||||||
|
$flight = Flights::find($id);
|
||||||
|
$deleted = $flight->delete();
|
||||||
|
|
||||||
|
// $result = new Item($flight, [
|
||||||
|
// 'id' => $id,
|
||||||
|
// 'message' => 'flight removed'
|
||||||
|
// ], 'message');
|
||||||
|
$result = new Item($flight, new FlightTransform(), 'flights');
|
||||||
|
|
||||||
|
return $this->JsonApiResponse($result, 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert the response to Json
|
||||||
|
*
|
||||||
|
* @param \League\Fractal\Resource\Item $resource
|
||||||
|
* @param $statusCode
|
||||||
|
* @return \Illuminate\Http\JsonResponse
|
||||||
|
*/
|
||||||
|
protected function JsonApiResponse(ResourceInterface $resource, $statusCode)
|
||||||
|
{
|
||||||
|
$manager = new Manager();
|
||||||
|
$manager->setSerializer(new JsonApiSerializer('http://docker.dev:8080'));
|
||||||
|
$manager->parseIncludes('flights');
|
||||||
|
|
||||||
|
return response()->json($manager->createData($resource)->toArray(), $statusCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
100
app/Http/Controllers/TripController.php
Normal file
100
app/Http/Controllers/TripController.php
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Libraries\Trips\Models\Flights;
|
||||||
|
use App\Libraries\Trips\Models\Trips;
|
||||||
|
use App\Libraries\Trips\TripTransformer;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Pagination\LengthAwarePaginator;
|
||||||
|
use League\Fractal\Manager;
|
||||||
|
use League\Fractal\Resource\Collection;
|
||||||
|
use League\Fractal\Resource\Item;
|
||||||
|
use League\Fractal\Resource\ResourceInterface;
|
||||||
|
use League\Fractal\Serializer\JsonApiSerializer;
|
||||||
|
|
||||||
|
class TripController extends Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Create a new controller instance.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
public function listTrips(Request $request)
|
||||||
|
{
|
||||||
|
$tripsCollection = Trips::all();
|
||||||
|
|
||||||
|
//add pagination
|
||||||
|
$per_page = $request->input('per_page', 10);
|
||||||
|
$page = $request->input('page', 1);
|
||||||
|
$paginator = new LengthAwarePaginator(
|
||||||
|
$tripsCollection->forPage($page, $per_page),
|
||||||
|
$tripsCollection->count(),
|
||||||
|
$per_page,
|
||||||
|
$page
|
||||||
|
);
|
||||||
|
|
||||||
|
$result = (new Collection($paginator, new TripTransformer(), 'trips'))
|
||||||
|
->setPaginator(new IlluminatePaginatorAdapter($paginator));
|
||||||
|
|
||||||
|
return $this->JsonApiResponse($result, 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getTrip(Request $request, $id)
|
||||||
|
{
|
||||||
|
$trip = Trips::find($id);
|
||||||
|
|
||||||
|
if (! $trip) {
|
||||||
|
return $this->returnErrorMessage("trip id $id not found", 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = new Item($trip, new TripTransformer(), 'trips');
|
||||||
|
|
||||||
|
return $this->JsonApiResponse($result, 200, 'flights');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addFlight(Request $request, $id)
|
||||||
|
{
|
||||||
|
$trip = Trips::find($id);
|
||||||
|
$destination = $request->input('destination');
|
||||||
|
|
||||||
|
if (! $destination) {
|
||||||
|
return $this->returnErrorMessage('destination not set', 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
$flight = new Flights(['destination' => $destination]);
|
||||||
|
$flight->save();
|
||||||
|
$trip->flights()->attach($flight->id);
|
||||||
|
|
||||||
|
$result = new Item($trip, new TripTransformer(), 'trips');
|
||||||
|
return $this->JsonApiResponse($result, 201, 'flights');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert the response to Json
|
||||||
|
*
|
||||||
|
* @param \League\Fractal\Resource\Item $resource
|
||||||
|
* @param $statusCode
|
||||||
|
* @return \Illuminate\Http\JsonResponse
|
||||||
|
*/
|
||||||
|
protected function JsonApiResponse(ResourceInterface $resource, $statusCode, $includes = '')
|
||||||
|
{
|
||||||
|
$manager = new Manager();
|
||||||
|
$manager->setSerializer(new JsonApiSerializer('http://docker.dev:8080'));
|
||||||
|
$manager->parseIncludes($includes);
|
||||||
|
|
||||||
|
return response()->json($manager->createData($resource)->toArray(), $statusCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function returnErrorMessage($message, $statusCode = 400)
|
||||||
|
{
|
||||||
|
return response()->json([
|
||||||
|
'error_message' => $message
|
||||||
|
], $statusCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
26
app/Libraries/Trips/FlightTransform.php
Normal file
26
app/Libraries/Trips/FlightTransform.php
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
namespace App\Libraries\Trips;
|
||||||
|
|
||||||
|
use App\Libraries\Trips\Models\Flights;
|
||||||
|
|
||||||
|
use League\Fractal\TransformerAbstract;
|
||||||
|
|
||||||
|
class FlightTransform extends TransformerAbstract
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Turn this item object into a generic array
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function transform(Flights $flight)
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $flight->id,
|
||||||
|
'name' => $flight->destination,
|
||||||
|
'links' => [
|
||||||
|
'rel' => 'self',
|
||||||
|
'uri' => '/flights/'.$flight->id,
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
24
app/Libraries/Trips/Models/Flights.php
Normal file
24
app/Libraries/Trips/Models/Flights.php
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Libraries\Trips\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class Flights extends Model
|
||||||
|
{
|
||||||
|
|
||||||
|
protected $fillable = ['destination'];
|
||||||
|
|
||||||
|
public function trips()
|
||||||
|
{
|
||||||
|
return $this->belongsToMany(Trips::class, 'trip_flights');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function toArray()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $this->id,
|
||||||
|
'destination' => $this->destination,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
19
app/Libraries/Trips/Models/Trips.php
Normal file
19
app/Libraries/Trips/Models/Trips.php
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Libraries\Trips\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class Trips extends Model
|
||||||
|
{
|
||||||
|
|
||||||
|
protected $fillable = ['name'];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* relation to flights
|
||||||
|
*/
|
||||||
|
public function flights()
|
||||||
|
{
|
||||||
|
return $this->belongsToMany(Flights::class, 'trip_flights');
|
||||||
|
}
|
||||||
|
}
|
||||||
49
app/Libraries/Trips/TripTransformer.php
Normal file
49
app/Libraries/Trips/TripTransformer.php
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
namespace App\Libraries\Trips;
|
||||||
|
|
||||||
|
use App\Libraries\Trips\Models\Flights;
|
||||||
|
use App\Libraries\Trips\Models\Trips;
|
||||||
|
use League\Fractal\TransformerAbstract;
|
||||||
|
|
||||||
|
class TripTransformer extends TransformerAbstract
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* List of resources possible to include
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $availableIncludes = [
|
||||||
|
'flights'
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Turn this item object into a generic array
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function transform(Trips $trip)
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $trip->id,
|
||||||
|
'name' => $trip->name,
|
||||||
|
'updated_at' => $trip->updated_at,
|
||||||
|
'flights' => $trip->flights()->get(),
|
||||||
|
'links' => [
|
||||||
|
'rel' => 'self',
|
||||||
|
'uri' => '/trips/'.$trip->code,
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Include Author
|
||||||
|
*
|
||||||
|
* @return \League\Fractal\Resource\ResourceAbstract
|
||||||
|
*/
|
||||||
|
public function includeFlights(Trips $trip)
|
||||||
|
{
|
||||||
|
$flights = $trip->flights()->get();
|
||||||
|
|
||||||
|
return $this->collection($flights, new FlightTransform());
|
||||||
|
}
|
||||||
|
}
|
||||||
24
database/factories/TripsFactory.php
Normal file
24
database/factories/TripsFactory.php
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Model Factories
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| Here you may define all of your model factories. Model factories give
|
||||||
|
| you a convenient way to create models for testing and seeding your
|
||||||
|
| database. Just tell the factory how a default model should look.
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
$factory->define(App\Libraries\Trips\Models\Trips::class, function (Faker\Generator $faker) {
|
||||||
|
return [
|
||||||
|
'name' => $faker->text(10),
|
||||||
|
];
|
||||||
|
});
|
||||||
|
|
||||||
|
$factory->define(App\Libraries\Trips\Models\Flights::class, function (Faker\Generator $faker) {
|
||||||
|
return [
|
||||||
|
'destination' => $faker->countryISOAlpha3, //TODO change this
|
||||||
|
];
|
||||||
|
});
|
||||||
0
database/migrations/.gitkeep
Normal file → Executable file
0
database/migrations/.gitkeep
Normal file → Executable file
58
database/migrations/2017_05_14_152748_trips.php
Executable file
58
database/migrations/2017_05_14_152748_trips.php
Executable file
@@ -0,0 +1,58 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
|
||||||
|
class Trips extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::create('trips', function (Blueprint $table) {
|
||||||
|
$table->increments('id');
|
||||||
|
$table->string('name');
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
|
||||||
|
Schema::create('flights', function (Blueprint $table) {
|
||||||
|
$table->increments('id');
|
||||||
|
$table->string('destination');
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
|
||||||
|
Schema::create('trip_flights', function (Blueprint $table) {
|
||||||
|
$table->increments('id');
|
||||||
|
$table->integer('trips_id')->unsigned();
|
||||||
|
$table->integer('flights_id')->unsigned();
|
||||||
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->unique(['trips_id', 'flights_id']);
|
||||||
|
|
||||||
|
$table->foreign('trips_id')->references('id')->on('trips');
|
||||||
|
});
|
||||||
|
|
||||||
|
Schema::create('airports', function (Blueprint $table) {
|
||||||
|
$table->increments('id');
|
||||||
|
$table->string('name', 256);
|
||||||
|
$table->string('code', 3);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::drop('trip_flights');
|
||||||
|
Schema::drop('trips');
|
||||||
|
Schema::drop('flights');
|
||||||
|
Schema::drop('airports');
|
||||||
|
}
|
||||||
|
}
|
||||||
11
database/seeds/DatabaseSeeder.php
Normal file → Executable file
11
database/seeds/DatabaseSeeder.php
Normal file → Executable file
@@ -12,5 +12,16 @@ class DatabaseSeeder extends Seeder
|
|||||||
public function run()
|
public function run()
|
||||||
{
|
{
|
||||||
// $this->call('UsersTableSeeder');
|
// $this->call('UsersTableSeeder');
|
||||||
|
|
||||||
|
factory(App\Libraries\Trips\Models\Trips::class, 5)
|
||||||
|
->create()
|
||||||
|
->each(function ($trip) {
|
||||||
|
$flights = factory(App\Libraries\Trips\Models\Flights::class, 2)
|
||||||
|
->create()
|
||||||
|
->each(function ($flight) use ($trip) {
|
||||||
|
$trip->flights()->attach($flight->id);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
21
database/seeds/TripsSeeder.php
Executable file
21
database/seeds/TripsSeeder.php
Executable file
@@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Seeder;
|
||||||
|
|
||||||
|
class TripsSeeder extends Seeder
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the database seeds.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function run()
|
||||||
|
{
|
||||||
|
dd('here');
|
||||||
|
factory(App\Libraries\Trips\Models\Trips::class, 5)
|
||||||
|
->create()
|
||||||
|
->each(function ($trip) {
|
||||||
|
$trip->flights->save(factory(App\Libraries\Trips\Models\Flights::class, 2)->make());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,3 +17,13 @@ $app->get('/', function () use ($app) {
|
|||||||
|
|
||||||
$app->get('/airports', 'AirportController@resourceList');
|
$app->get('/airports', 'AirportController@resourceList');
|
||||||
$app->get('/airports/{code}', 'AirportController@getAirport');
|
$app->get('/airports/{code}', 'AirportController@getAirport');
|
||||||
|
|
||||||
|
$app->get('/trips', 'TripController@listTrips');
|
||||||
|
$app->get('/trips/{id}', 'TripController@getTrip');
|
||||||
|
|
||||||
|
$app->get('/trips/{id}/flights', 'TripController@addFlight');
|
||||||
|
$app->post('/trips/{id}/flights', 'TripController@addFlight');
|
||||||
|
|
||||||
|
$app->get('/flights', 'FlightController@listFlights');
|
||||||
|
$app->get('/flights/{id}', 'FlightController@getFlight');
|
||||||
|
$app->delete('/flights/{id}', 'FlightController@removeFlight');
|
||||||
|
|||||||
0
tests/ExampleTest.php
Normal file → Executable file
0
tests/ExampleTest.php
Normal file → Executable file
0
tests/TestCase.php
Normal file → Executable file
0
tests/TestCase.php
Normal file → Executable file
Reference in New Issue
Block a user