Some Error handling and acceptance tests
This commit is contained in:
@@ -7,7 +7,9 @@ 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;
|
||||
@@ -16,62 +18,90 @@ use League\Fractal\Serializer\JsonApiSerializer;
|
||||
class FlightController extends Controller
|
||||
{
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
* @api {get} /flights list flights
|
||||
* @apiName List Flights
|
||||
* @apiGroup Flights
|
||||
* @apiDescription List all defined flights
|
||||
*
|
||||
* @return void
|
||||
* @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/flights?page=1&per_page=10
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function listFlights(Request $request)
|
||||
{
|
||||
$flightCollection = Flights::all();
|
||||
//TODO: add pagination
|
||||
|
||||
|
||||
//add pagination
|
||||
$per_page = $request->input('per_page', 10);
|
||||
$page = $request->input('page', 1);
|
||||
$paginator = new LengthAwarePaginator(
|
||||
$flightCollection->forPage($page, $per_page),
|
||||
$flightCollection->count(),
|
||||
$per_page,
|
||||
$page
|
||||
);
|
||||
|
||||
$result = (new Collection($paginator, new TripTransformer(), 'trips'))
|
||||
->setPaginator(new IlluminatePaginatorAdapter($paginator));
|
||||
|
||||
$result = new Collection($flightCollection, new FlightTransform(), 'flights');
|
||||
|
||||
|
||||
return $this->JsonApiResponse($result, 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* @api {get} /flights/:id get flight
|
||||
* @apiName Get Flight
|
||||
* @apiGroup Flights
|
||||
* @apiDescription get details of a specific flight
|
||||
*
|
||||
* @apiParam (url) {number} id the id of the flight to fetch
|
||||
*
|
||||
* @apiExample {curl} example
|
||||
* curl -i http://localhost:8080/flights/1
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param $id
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
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');
|
||||
if (! $flight) {
|
||||
return $this->returnErrorMessage("flight id $id not found", 400);
|
||||
}
|
||||
|
||||
$result = new Item($flight, new FlightTransform(), 'flights');
|
||||
|
||||
return $this->JsonApiResponse($result, 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the response to Json
|
||||
* @api {delete} /flights/:id delete a flight
|
||||
* @apiName Delete a Flight
|
||||
* @apiGroup Flights
|
||||
* @apiDescription delete a flight and remove it from any trips
|
||||
*
|
||||
* @param \League\Fractal\Resource\Item $resource
|
||||
* @param $statusCode
|
||||
* @apiParam (url) {number} id the id of the flight to fetch
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param $id
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
protected function JsonApiResponse(ResourceInterface $resource, $statusCode)
|
||||
public function removeFlight(Request $request, $id)
|
||||
{
|
||||
$manager = new Manager();
|
||||
$manager->setSerializer(new JsonApiSerializer('http://docker.dev:8080'));
|
||||
$manager->parseIncludes('flights');
|
||||
/** @var \App\Libraries\Trips\Models\Flights $flight */
|
||||
$flight = Flights::find($id);
|
||||
$deleted = $flight->delete();
|
||||
|
||||
$result = new Item($flight, new FlightTransform(), 'flights');
|
||||
|
||||
return response()->json($manager->createData($resource)->toArray(), $statusCode);
|
||||
return $this->JsonApiResponse($result, 200);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user