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

124 lines
3.4 KiB
PHP
Raw Normal View History

2017-08-15 07:26:20 +08:00
<?php
namespace App\Http\Controllers\Api;
2018-02-21 12:33:09 +08:00
use App\Http\Resources\Flight as FlightResource;
use App\Http\Resources\Navdata as NavdataResource;
use App\Interfaces\Controller;
use App\Repositories\Criteria\WhereCriteria;
2018-02-21 12:33:09 +08:00
use App\Repositories\FlightRepository;
use App\Services\FlightService;
use Auth;
2017-08-15 07:26:20 +08:00
use Illuminate\Http\Request;
use Prettus\Repository\Criteria\RequestCriteria;
use Prettus\Repository\Exceptions\RepositoryException;
2017-08-15 07:26:20 +08:00
/**
* Class FlightController
* @package App\Http\Controllers\Api
*/
class FlightController extends Controller
2017-08-15 07:26:20 +08:00
{
private $flightRepo,
$flightSvc;
2018-02-21 12:33:09 +08:00
/**
* FlightController constructor.
* @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
* @param Request $request
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
*/
public function index(Request $request)
{
$user = Auth::user();
$where = ['active' => true];
if(setting('pilots.restrict_to_company')) {
$where['airline_id'] = Auth::user()->airline_id;
}
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();
foreach ($flights as $flight) {
$this->flightSvc->filterSubfleets($user, $flight);
}
return FlightResource::collection($flights);
}
/**
* @param $id
* @return FlightResource
*/
2017-08-15 12:36:49 +08:00
public function get($id)
{
$flight = $this->flightRepo->find($id);
$this->flightSvc->filterSubfleets(Auth::user(), $flight);
return new FlightResource($flight);
2017-08-15 12:36:49 +08:00
}
/**
* @param Request $request
* @return mixed
*/
2017-08-15 07:26:20 +08:00
public function search(Request $request)
{
$user = Auth::user();
try {
$where = ['active' => true];
if(setting('pilots.restrict_to_company')) {
$where['airline_id'] = Auth::user()->airline_id;
}
if (setting('pilots.only_flights_from_current')) {
$where['dpt_airport_id'] = Auth::user()->curr_airport_id;
}
$this->flightRepo->searchCriteria($request);
$this->flightRepo->pushCriteria(new RequestCriteria($request));
$this->flightRepo->pushCriteria(new WhereCriteria($request, $where));
$flights = $this->flightRepo->paginate();
} catch (RepositoryException $e) {
return response($e, 503);
}
foreach ($flights as $flight) {
$this->flightSvc->filterSubfleets($user, $flight);
}
return FlightResource::collection($flights);
2017-08-15 07:26:20 +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
}