Some Error handling and acceptance tests

This commit is contained in:
Richard Morgan
2017-05-15 08:56:10 -04:00
parent 42dc785735
commit 2c8c692890
21 changed files with 457 additions and 126 deletions

View File

@@ -2,12 +2,14 @@
namespace App\Http\Controllers;
use App\Libraries\IotaCodes\Client as IotaClient;
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\Pagination\IlluminatePaginatorAdapter;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;
use League\Fractal\Resource\ResourceInterface;
@@ -24,7 +26,22 @@ class TripController extends Controller
{
//
}
/**
* @api {get} /trips list trips
* @apiName list trips
* @apiGroup Trips
* @apiDescription This api returns a paginated list of trips found
*
* @apiParam (query params) {number} [page=1] page to fetch
* @apiParam (query param) {number} [per_page=10] number of results to fetch per page
*
* @apiExample {curl} example
* curl -i http://localhost:8080/trips?page=1&per_page=10
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function listTrips(Request $request)
{
$tripsCollection = Trips::all();
@@ -41,10 +58,25 @@ class TripController extends Controller
$result = (new Collection($paginator, new TripTransformer(), 'trips'))
->setPaginator(new IlluminatePaginatorAdapter($paginator));
return $this->JsonApiResponse($result, 200);
}
/**
* @api {get} /trips/:id get trip
* @apiName get trip
* @apiGroup Trips
* @apiDescription Fetch details of a specific trip
*
* @apiParam (url) {number} id the id of the trip to fetch
*
* @apiExample {curl} example
* curl -i http://localhost:8080/trips/1
*
* @param \Illuminate\Http\Request $request
* @param $id
* @return \Illuminate\Http\JsonResponse
*/
public function getTrip(Request $request, $id)
{
$trip = Trips::find($id);
@@ -58,14 +90,36 @@ class TripController extends Controller
return $this->JsonApiResponse($result, 200, 'flights');
}
/**
* @api {post} /trips/:id/flights add flight
* @apiName add flight
* @apiGroup Trips
* @apiDescription send a destination to this endpoint to add a flight to the trip
*
* @apiParam (form data) {string} destination the destination of the new flight
* @apiParam (url) {number} id the id of the trip to fetch
*
* @apiExample {curl} example
* curl -i http://localhost:8080/trips/1/flights
*
* @param \Illuminate\Http\Request $request
* @param $id
* @return \Illuminate\Http\JsonResponse
*/
public function addFlight(Request $request, $id)
{
$trip = Trips::find($id);
$destination = $request->input('destination');
if (! $destination) {
return $this->returnErrorMessage('destination not set', 400);
}
$client = IotaClient::create();
$airport = $client->getAirport($destination);
if (is_null($airport)) {
return $this->returnErrorMessage('destination not a valid airport code', 400);
}
$flight = new Flights(['destination' => $destination]);
$flight->save();
@@ -74,27 +128,4 @@ class TripController extends Controller
$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);
}
}