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

175 lines
4.7 KiB
PHP
Raw Normal View History

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;
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;
use App\Models\SimBrief;
use App\Repositories\Criteria\WhereCriteria;
2018-02-21 12:33:09 +08:00
use App\Repositories\FlightRepository;
use App\Services\FlightService;
use Exception;
2017-08-15 07:26:20 +08:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Prettus\Repository\Criteria\RequestCriteria;
use Prettus\Repository\Exceptions\RepositoryException;
2017-08-15 07:26:20 +08:00
class FlightController extends Controller
2017-08-15 07:26:20 +08:00
{
2018-08-27 00:40:04 +08:00
private $flightRepo;
private $flightSvc;
2018-02-21 12:33:09 +08:00
/**
* FlightController constructor.
2018-08-27 00:40:04 +08:00
*
2018-02-21 12:33:09 +08:00
* @param FlightRepository $flightRepo
* @param FlightService $flightSvc
2018-02-21 12:33:09 +08:00
*/
public function __construct(
FlightRepository $flightRepo,
FlightService $flightSvc
) {
$this->flightRepo = $flightRepo;
$this->flightSvc = $flightSvc;
}
/**
* Return all the flights, paginated
2018-08-27 00:40:04 +08:00
*
* @param Request $request
2018-08-27 00:40:04 +08:00
*
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
*/
public function index(Request $request)
{
return $this->search($request);
}
/**
* @param $id
2018-08-27 00:40:04 +08:00
*
* @return FlightResource
*/
2017-08-15 12:36:49 +08:00
public function get($id)
{
$user = Auth::user();
$flight = $this->flightRepo->with([
'airline',
'subfleets',
'subfleets.aircraft',
'subfleets.fares',
'field_values',
'simbrief' => function ($query) use ($user) {
return $query->where('user_id', $user->id);
},
])->find($id);
$this->flightSvc->filterSubfleets(Auth::user(), $flight);
return new FlightResource($flight);
2017-08-15 12:36:49 +08:00
}
/**
* @param Request $request
2018-08-27 00:40:04 +08:00
*
* @return mixed
*/
2017-08-15 07:26:20 +08:00
public function search(Request $request)
{
/** @var \App\Models\User $user */
$user = Auth::user();
$where = [
'active' => true,
'visible' => true,
];
// Allow the option to bypass some of these restrictions for the searches
if (!$request->filled('ignore_restrictions') || $request->get('ignore_restrictions') === '0') {
2018-08-27 00:40:04 +08:00
if (setting('pilots.restrict_to_company')) {
$where['airline_id'] = $user->airline_id;
}
if (setting('pilots.only_flights_from_current')) {
$where['dpt_airport_id'] = $user->curr_airport_id;
}
}
try {
2019-05-12 00:37:06 +08:00
$this->flightRepo->resetCriteria();
$this->flightRepo->searchCriteria($request);
$this->flightRepo->pushCriteria(new WhereCriteria($request, $where, [
'airline' => ['active' => true],
]));
$this->flightRepo->pushCriteria(new RequestCriteria($request));
$flights = $this->flightRepo
->with([
'airline',
'subfleets',
'subfleets.aircraft',
'subfleets.fares',
'field_values',
'simbrief' => function ($query) use ($user) {
return $query->where('user_id', $user->id);
},
])
->paginate();
} catch (RepositoryException $e) {
return response($e, 503);
}
// TODO: Remove any flights here that a user doesn't have permissions to
foreach ($flights as $flight) {
$this->flightSvc->filterSubfleets(Auth::user(), $flight);
}
return FlightResource::collection($flights);
2017-08-15 07:26:20 +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',
]);
}
/**
* Get a flight's route
2018-08-27 00:40:04 +08:00
*
* @param $id
* @param Request $request
2018-08-27 00:40:04 +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
}