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

73 lines
1.8 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers\Api;
use App\Http\Resources\Aircraft as AircraftResource;
use App\Http\Resources\Subfleet as SubfleetResource;
use App\Interfaces\Controller;
2018-02-21 12:33:09 +08:00
use App\Repositories\AircraftRepository;
use App\Repositories\SubfleetRepository;
use Illuminate\Http\Request;
/**
* Class FleetController
*/
class FleetController extends Controller
{
2018-08-27 00:40:04 +08:00
private $aircraftRepo;
private $subfleetRepo;
2018-02-21 12:33:09 +08:00
/**
* FleetController constructor.
2018-08-27 00:40:04 +08:00
*
2018-02-21 12:33:09 +08:00
* @param AircraftRepository $aircraftRepo
* @param SubfleetRepository $subfleetRepo
*/
public function __construct(
AircraftRepository $aircraftRepo,
SubfleetRepository $subfleetRepo
) {
$this->aircraftRepo = $aircraftRepo;
$this->subfleetRepo = $subfleetRepo;
}
/**
* Return all the subfleets and the aircraft and any other associated data
* Paginated
*/
public function index()
{
$subfleets = $this->subfleetRepo
->with(['aircraft', 'airline', 'fares', 'ranks'])
->paginate();
return SubfleetResource::collection($subfleets);
}
/**
* Get a specific aircraft. Query string required to specify the tail
* /api/aircraft/XYZ?type=registration
2018-08-27 00:40:04 +08:00
*
* @param $id
* @param Request $request
2018-08-27 00:40:04 +08:00
*
* @return AircraftResource
*/
public function get_aircraft($id, Request $request)
{
$where = [];
if ($request->filled('type')) {
$where[$request->get('type')] = $id;
} else {
$where['id'] = $id;
}
$aircraft = $this->aircraftRepo
->with(['subfleet', 'subfleet.fares'])
->findWhere($where)
->first();
return new AircraftResource($aircraft);
}
}