2017-08-15 07:26:20 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
2019-07-16 03:51:35 +08:00
|
|
|
use App\Contracts\Controller;
|
2020-03-23 21:31:35 +08:00
|
|
|
use App\Exceptions\AssetNotFound;
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Http\Resources\Flight as FlightResource;
|
|
|
|
use App\Http\Resources\Navdata as NavdataResource;
|
2020-03-23 21:31:35 +08:00
|
|
|
use App\Models\SimBrief;
|
2018-02-10 05:42:37 +08:00
|
|
|
use App\Repositories\Criteria\WhereCriteria;
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Repositories\FlightRepository;
|
2020-10-28 06:46:15 +08:00
|
|
|
use App\Services\FareService;
|
2018-02-10 04:36:36 +08:00
|
|
|
use App\Services\FlightService;
|
2020-03-23 21:31:35 +08:00
|
|
|
use Exception;
|
2017-08-15 07:26:20 +08:00
|
|
|
use Illuminate\Http\Request;
|
2019-11-06 00:44:31 +08:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2018-01-07 02:07:22 +08:00
|
|
|
use Prettus\Repository\Criteria\RequestCriteria;
|
2018-02-10 04:26:14 +08:00
|
|
|
use Prettus\Repository\Exceptions\RepositoryException;
|
2017-08-15 07:26:20 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
class FlightController extends Controller
|
2017-08-15 07:26:20 +08:00
|
|
|
{
|
2020-10-28 06:46:15 +08:00
|
|
|
/** @var \App\Services\FareService */
|
|
|
|
private $fareSvc;
|
|
|
|
|
|
|
|
/** @var \App\Repositories\FlightRepository */
|
2018-08-27 00:40:04 +08:00
|
|
|
private $flightRepo;
|
2020-10-28 06:46:15 +08:00
|
|
|
|
|
|
|
/** @var \App\Services\FlightService */
|
2018-08-27 00:40:04 +08:00
|
|
|
private $flightSvc;
|
2017-12-05 00:59:25 +08:00
|
|
|
|
2018-02-21 12:33:09 +08:00
|
|
|
/**
|
2020-10-28 06:46:15 +08:00
|
|
|
* @param FareService $fareSvc
|
2018-02-21 12:33:09 +08:00
|
|
|
* @param FlightRepository $flightRepo
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param FlightService $flightSvc
|
2018-02-21 12:33:09 +08:00
|
|
|
*/
|
2018-02-10 04:26:14 +08:00
|
|
|
public function __construct(
|
2020-10-28 06:46:15 +08:00
|
|
|
FareService $fareSvc,
|
2018-02-10 04:26:14 +08:00
|
|
|
FlightRepository $flightRepo,
|
2018-03-20 09:50:40 +08:00
|
|
|
FlightService $flightSvc
|
2018-02-10 04:26:14 +08:00
|
|
|
) {
|
2020-10-28 06:46:15 +08:00
|
|
|
$this->fareSvc = $fareSvc;
|
2017-12-05 00:59:25 +08:00
|
|
|
$this->flightRepo = $flightRepo;
|
2018-02-10 04:36:36 +08:00
|
|
|
$this->flightSvc = $flightSvc;
|
2018-02-10 04:26:14 +08:00
|
|
|
}
|
|
|
|
|
2018-01-07 05:21:21 +08:00
|
|
|
/**
|
|
|
|
* Return all the flights, paginated
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-11 07:34:46 +08:00
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-11 07:34:46 +08:00
|
|
|
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
|
2018-01-07 05:21:21 +08:00
|
|
|
*/
|
|
|
|
public function index(Request $request)
|
|
|
|
{
|
2020-01-10 23:16:57 +08:00
|
|
|
return $this->search($request);
|
2018-01-07 05:21:21 +08:00
|
|
|
}
|
|
|
|
|
2018-02-10 04:26:14 +08:00
|
|
|
/**
|
|
|
|
* @param $id
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-10 04:26:14 +08:00
|
|
|
* @return FlightResource
|
|
|
|
*/
|
2017-08-15 12:36:49 +08:00
|
|
|
public function get($id)
|
|
|
|
{
|
2020-10-25 03:11:08 +08:00
|
|
|
/** @var \App\Models\User $user */
|
2020-03-23 21:31:35 +08:00
|
|
|
$user = Auth::user();
|
2020-10-25 03:11:08 +08:00
|
|
|
|
|
|
|
/** @var \App\Models\Flight $flight */
|
2020-03-23 21:31:35 +08:00
|
|
|
$flight = $this->flightRepo->with([
|
|
|
|
'airline',
|
2020-10-25 03:11:08 +08:00
|
|
|
'fares',
|
2020-03-23 21:31:35 +08:00
|
|
|
'subfleets',
|
|
|
|
'subfleets.aircraft',
|
|
|
|
'subfleets.fares',
|
|
|
|
'field_values',
|
|
|
|
'simbrief' => function ($query) use ($user) {
|
|
|
|
return $query->where('user_id', $user->id);
|
|
|
|
},
|
|
|
|
])->find($id);
|
|
|
|
|
2020-10-25 03:11:08 +08:00
|
|
|
$flight = $this->flightSvc->filterSubfleets($user, $flight);
|
2020-10-28 06:46:15 +08:00
|
|
|
$flight = $this->fareSvc->getReconciledFaresForFlight($flight);
|
2018-02-10 04:26:14 +08:00
|
|
|
|
2017-12-12 21:25:11 +08:00
|
|
|
return new FlightResource($flight);
|
2017-08-15 12:36:49 +08:00
|
|
|
}
|
|
|
|
|
2017-12-13 06:58:27 +08:00
|
|
|
/**
|
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-13 06:58:27 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2017-08-15 07:26:20 +08:00
|
|
|
public function search(Request $request)
|
|
|
|
{
|
2020-05-23 23:43:29 +08:00
|
|
|
/** @var \App\Models\User $user */
|
2020-03-23 21:31:35 +08:00
|
|
|
$user = Auth::user();
|
2020-05-23 23:43:29 +08:00
|
|
|
|
2019-05-11 05:03:04 +08:00
|
|
|
$where = [
|
|
|
|
'active' => true,
|
|
|
|
'visible' => true,
|
|
|
|
];
|
2018-07-13 11:29:50 +08:00
|
|
|
|
2019-05-11 05:03:04 +08:00
|
|
|
// Allow the option to bypass some of these restrictions for the searches
|
2020-05-23 23:43:29 +08:00
|
|
|
if (!$request->filled('ignore_restrictions') || $request->get('ignore_restrictions') === '0') {
|
2018-08-27 00:40:04 +08:00
|
|
|
if (setting('pilots.restrict_to_company')) {
|
2020-05-23 23:43:29 +08:00
|
|
|
$where['airline_id'] = $user->airline_id;
|
2018-05-19 08:37:11 +08:00
|
|
|
}
|
2019-05-11 05:03:04 +08:00
|
|
|
|
2018-02-10 05:42:37 +08:00
|
|
|
if (setting('pilots.only_flights_from_current')) {
|
2020-05-23 23:43:29 +08:00
|
|
|
$where['dpt_airport_id'] = $user->curr_airport_id;
|
2018-02-10 05:42:37 +08:00
|
|
|
}
|
2019-05-11 05:03:04 +08:00
|
|
|
}
|
2018-02-10 05:42:37 +08:00
|
|
|
|
2019-05-11 05:03:04 +08:00
|
|
|
try {
|
2019-05-12 00:37:06 +08:00
|
|
|
$this->flightRepo->resetCriteria();
|
2018-01-07 02:07:22 +08:00
|
|
|
$this->flightRepo->searchCriteria($request);
|
2020-05-23 23:43:29 +08:00
|
|
|
$this->flightRepo->pushCriteria(new WhereCriteria($request, $where, [
|
|
|
|
'airline' => ['active' => true],
|
|
|
|
]));
|
|
|
|
|
2019-05-11 05:03:04 +08:00
|
|
|
$this->flightRepo->pushCriteria(new RequestCriteria($request));
|
|
|
|
|
2020-03-05 07:14:03 +08:00
|
|
|
$flights = $this->flightRepo
|
|
|
|
->with([
|
|
|
|
'airline',
|
2020-10-25 03:11:08 +08:00
|
|
|
'fares',
|
2020-03-05 07:14:03 +08:00
|
|
|
'subfleets',
|
|
|
|
'subfleets.aircraft',
|
|
|
|
'subfleets.fares',
|
|
|
|
'field_values',
|
2020-03-23 21:31:35 +08:00
|
|
|
'simbrief' => function ($query) use ($user) {
|
|
|
|
return $query->where('user_id', $user->id);
|
|
|
|
},
|
2020-03-05 07:14:03 +08:00
|
|
|
])
|
|
|
|
->paginate();
|
2017-12-12 21:25:11 +08:00
|
|
|
} catch (RepositoryException $e) {
|
|
|
|
return response($e, 503);
|
|
|
|
}
|
2017-12-13 01:49:35 +08:00
|
|
|
|
2020-01-17 06:36:03 +08:00
|
|
|
// TODO: Remove any flights here that a user doesn't have permissions to
|
2018-02-10 04:26:14 +08:00
|
|
|
foreach ($flights as $flight) {
|
2020-10-25 03:11:08 +08:00
|
|
|
$this->flightSvc->filterSubfleets($user, $flight);
|
2020-10-28 06:46:15 +08:00
|
|
|
$this->fareSvc->getReconciledFaresForFlight($flight);
|
2018-02-10 04:26:14 +08:00
|
|
|
}
|
|
|
|
|
2017-12-12 21:25:11 +08:00
|
|
|
return FlightResource::collection($flights);
|
2017-08-15 07:26:20 +08:00
|
|
|
}
|
2018-02-21 02:06:52 +08:00
|
|
|
|
2020-03-23 21:31:35 +08:00
|
|
|
/**
|
|
|
|
* Output the flight briefing from simbrief or whatever other format
|
|
|
|
*
|
|
|
|
* @param string $id The flight ID
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function briefing($id)
|
|
|
|
{
|
|
|
|
$user = Auth::user();
|
|
|
|
$w = [
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'flight_id' => $id,
|
|
|
|
];
|
|
|
|
|
|
|
|
$simbrief = SimBrief::where($w)->first();
|
|
|
|
|
|
|
|
if ($simbrief === null) {
|
|
|
|
throw new AssetNotFound(new Exception('Flight briefing not found'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return response($simbrief->acars_xml, 200, [
|
|
|
|
'Content-Type' => 'application/xml',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-02-21 02:06:52 +08:00
|
|
|
/**
|
|
|
|
* Get a flight's route
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param $id
|
2018-02-21 02:06:52 +08:00
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-21 02:06:52 +08:00
|
|
|
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
|
|
|
|
*/
|
|
|
|
public function route($id, Request $request)
|
|
|
|
{
|
|
|
|
$flight = $this->flightRepo->find($id);
|
|
|
|
$route = $this->flightSvc->getRoute($flight);
|
|
|
|
|
|
|
|
return NavdataResource::collection($route);
|
|
|
|
}
|
2017-08-15 07:26:20 +08:00
|
|
|
}
|