2017-08-15 07:26:20 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
2018-02-10 05:42:37 +08:00
|
|
|
use App\Repositories\Criteria\WhereCriteria;
|
2018-02-10 04:36:36 +08:00
|
|
|
use App\Services\FlightService;
|
2018-02-10 04:26:14 +08:00
|
|
|
use Auth;
|
2017-08-15 07:26:20 +08:00
|
|
|
use Illuminate\Http\Request;
|
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-02-10 04:26:14 +08:00
|
|
|
use App\Services\UserService;
|
2017-12-05 00:59:25 +08:00
|
|
|
use App\Repositories\FlightRepository;
|
2018-02-21 02:06:52 +08:00
|
|
|
|
2017-12-12 21:25:11 +08:00
|
|
|
use App\Http\Resources\Flight as FlightResource;
|
2018-02-21 02:06:52 +08:00
|
|
|
use App\Http\Resources\Navdata as NavdataResource;
|
2017-08-15 07:26:20 +08:00
|
|
|
|
2018-02-10 04:26:14 +08:00
|
|
|
/**
|
|
|
|
* Class FlightController
|
|
|
|
* @package App\Http\Controllers\Api
|
|
|
|
*/
|
2018-01-06 00:45:34 +08:00
|
|
|
class FlightController extends RestController
|
2017-08-15 07:26:20 +08:00
|
|
|
{
|
2018-02-10 04:36:36 +08:00
|
|
|
protected $flightRepo, $flightSvc, $userSvc;
|
2017-12-05 00:59:25 +08:00
|
|
|
|
2018-02-10 04:26:14 +08:00
|
|
|
public function __construct(
|
|
|
|
FlightRepository $flightRepo,
|
2018-02-10 04:36:36 +08:00
|
|
|
FlightService $flightSvc,
|
2018-02-10 04:26:14 +08:00
|
|
|
UserService $userSvc
|
|
|
|
) {
|
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
|
|
|
$this->userSvc = $userSvc;
|
|
|
|
}
|
|
|
|
|
2018-01-07 05:21:21 +08:00
|
|
|
/**
|
|
|
|
* Return all the flights, paginated
|
2018-02-11 07:34:46 +08:00
|
|
|
* @param Request $request
|
|
|
|
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
|
2018-01-07 05:21:21 +08:00
|
|
|
*/
|
|
|
|
public function index(Request $request)
|
|
|
|
{
|
2018-02-10 04:26:14 +08:00
|
|
|
$user = Auth::user();
|
2018-02-10 05:42:37 +08:00
|
|
|
|
|
|
|
$where = ['active' => true];
|
|
|
|
if (setting('pilots.only_flights_from_current', false)) {
|
|
|
|
$where['dpt_airport_id'] = $user->curr_airport_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$flights = $this->flightRepo
|
|
|
|
->whereOrder($where, 'flight_number', 'asc')
|
|
|
|
->paginate();
|
2018-02-10 05:36:13 +08:00
|
|
|
|
2018-02-10 04:26:14 +08:00
|
|
|
foreach($flights as $flight) {
|
2018-02-10 04:36:36 +08:00
|
|
|
$this->flightSvc->filterSubfleets($user, $flight);
|
2018-02-10 04:26:14 +08:00
|
|
|
}
|
|
|
|
|
2018-01-07 05:21:21 +08:00
|
|
|
return FlightResource::collection($flights);
|
|
|
|
}
|
|
|
|
|
2018-02-10 04:26:14 +08:00
|
|
|
/**
|
|
|
|
* @param $id
|
|
|
|
* @return FlightResource
|
|
|
|
*/
|
2017-08-15 12:36:49 +08:00
|
|
|
public function get($id)
|
|
|
|
{
|
2017-12-05 00:59:25 +08:00
|
|
|
$flight = $this->flightRepo->find($id);
|
2018-02-10 04:36:36 +08:00
|
|
|
$this->flightSvc->filterSubfleets(Auth::user(), $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
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2017-08-15 07:26:20 +08:00
|
|
|
public function search(Request $request)
|
|
|
|
{
|
2018-02-10 05:36:13 +08:00
|
|
|
$user = Auth::user();
|
|
|
|
|
2017-12-12 21:25:11 +08:00
|
|
|
try {
|
2018-02-10 05:42:37 +08:00
|
|
|
$where = ['active' => true];
|
|
|
|
if (setting('pilots.only_flights_from_current')) {
|
|
|
|
$where['dpt_airport_id'] = Auth::user()->curr_airport_id;
|
|
|
|
}
|
|
|
|
|
2018-01-07 02:07:22 +08:00
|
|
|
$this->flightRepo->searchCriteria($request);
|
|
|
|
$this->flightRepo->pushCriteria(new RequestCriteria($request));
|
2018-02-10 05:42:37 +08:00
|
|
|
$this->flightRepo->pushCriteria(new WhereCriteria($request, $where));
|
|
|
|
$flights = $this->flightRepo->paginate();
|
2017-12-12 21:25:11 +08:00
|
|
|
} catch (RepositoryException $e) {
|
|
|
|
return response($e, 503);
|
|
|
|
}
|
2017-12-13 01:49:35 +08:00
|
|
|
|
2018-02-10 04:26:14 +08:00
|
|
|
foreach ($flights as $flight) {
|
2018-02-10 04:36:36 +08:00
|
|
|
$this->flightSvc->filterSubfleets($user, $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
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a flight's route
|
|
|
|
* @param $id
|
|
|
|
* @param Request $request
|
|
|
|
* @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
|
|
|
}
|