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

493 lines
14 KiB
PHP
Raw Normal View History

2017-08-15 12:36:49 +08:00
<?php
namespace App\Http\Controllers\Api;
use App\Http\Requests\Acars\CommentRequest;
2018-02-21 12:33:09 +08:00
use App\Http\Requests\Acars\EventRequest;
use App\Http\Requests\Acars\FileRequest;
use App\Http\Requests\Acars\LogRequest;
use App\Http\Requests\Acars\PositionRequest;
use App\Http\Requests\Acars\PrefileRequest;
use App\Http\Requests\Acars\RouteRequest;
2018-02-21 12:33:09 +08:00
use App\Http\Requests\Acars\UpdateRequest;
use App\Http\Resources\AcarsRoute as AcarsRouteResource;
use App\Http\Resources\Pirep as PirepResource;
use App\Http\Resources\PirepComment as PirepCommentResource;
use App\Models\Acars;
use App\Models\Enums\AcarsType;
2018-02-21 12:33:09 +08:00
use App\Models\Enums\PirepSource;
use App\Models\Enums\PirepState;
use App\Models\Enums\PirepStatus;
2018-02-21 12:33:09 +08:00
use App\Models\Pirep;
use App\Models\PirepComment;
use App\Repositories\AcarsRepository;
use App\Repositories\PirepRepository;
2018-02-21 12:33:09 +08:00
use App\Services\GeoService;
use App\Services\PIREPService;
use App\Services\UserService;
use Auth;
use Illuminate\Http\Request;
use Log;
2018-01-04 00:25:55 +08:00
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
2017-08-15 12:36:49 +08:00
2018-01-05 09:33:23 +08:00
class PirepController extends RestController
2017-08-15 12:36:49 +08:00
{
2018-01-05 09:33:23 +08:00
protected $acarsRepo,
$geoSvc,
$pirepRepo,
$pirepSvc,
$userSvc;
2018-01-05 09:33:23 +08:00
/**
* PirepController constructor.
* @param AcarsRepository $acarsRepo
* @param GeoService $geoSvc
* @param PirepRepository $pirepRepo
* @param PIREPService $pirepSvc
2018-02-21 12:33:09 +08:00
* @param UserService $userSvc
2018-01-05 09:33:23 +08:00
*/
public function __construct(
AcarsRepository $acarsRepo,
2017-12-28 10:52:37 +08:00
GeoService $geoSvc,
2017-12-27 04:54:28 +08:00
PirepRepository $pirepRepo,
PIREPService $pirepSvc,
UserService $userSvc
) {
$this->acarsRepo = $acarsRepo;
2017-12-28 10:52:37 +08:00
$this->geoSvc = $geoSvc;
$this->pirepRepo = $pirepRepo;
2017-12-27 04:54:28 +08:00
$this->pirepSvc = $pirepSvc;
$this->userSvc = $userSvc;
}
/**
* Check if a PIREP is cancelled
* @param $pirep
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
*/
protected function checkCancelled(Pirep $pirep)
{
if (!$pirep->allowedUpdates()) {
throw new BadRequestHttpException('PIREP has been cancelled, comments can\'t be posted');
}
}
/**
* @param $id
* @return PirepResource
*/
2017-08-15 12:36:49 +08:00
public function get($id)
{
return new PirepResource($this->pirepRepo->find($id));
2017-08-15 12:36:49 +08:00
}
/**
* @param $pirep
* @param Request $request
*/
protected function updateFields($pirep, Request $request)
{
if (!$request->filled('fields')) {
return;
}
$pirep_fields = [];
foreach ($request->input('fields') as $field_name => $field_value) {
$pirep_fields[] = [
'name' => $field_name,
'value' => $field_value,
'source' => $pirep->source,
];
}
$this->pirepSvc->updateCustomFields($pirep->id, $pirep_fields);
}
/**
* Create a new PIREP and place it in a "inprogress" and "prefile" state
* Once ACARS updates are being processed, then it can go into an 'ENROUTE'
* status, and whatever other statuses may be defined
*
* @param PrefileRequest $request
* @return PirepResource
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
*/
public function prefile(PrefileRequest $request)
{
Log::info('PIREP Prefile, user '.Auth::id(), $request->post());
$user = Auth::user();
$attrs = $request->post();
$attrs['user_id'] = $user->id;
2018-02-11 03:41:24 +08:00
$attrs['source'] = PirepSource::ACARS;
$attrs['state'] = PirepState::IN_PROGRESS;
$attrs['status'] = PirepStatus::PREFILE;
2017-12-27 04:54:28 +08:00
$pirep = new Pirep($attrs);
# See if this user is allowed to fly this aircraft
if(setting('pireps.restrict_aircraft_to_rank', false)) {
$can_use_ac = $this->userSvc->aircraftAllowed($user, $pirep->aircraft_id);
if (!$can_use_ac) {
throw new BadRequestHttpException('User is not allowed to fly this aircraft');
}
}
# Find if there's a duplicate, if so, let's work on that
$dupe_pirep = $this->pirepSvc->findDuplicate($pirep);
if($dupe_pirep !== false) {
$pirep = $dupe_pirep;
}
$pirep->save();
Log::info('PIREP PREFILED');
Log::info($pirep->id);
$this->updateFields($pirep, $request);
2017-12-27 04:54:28 +08:00
return new PirepResource($pirep);
}
2018-01-31 01:36:22 +08:00
/**
* Create a new PIREP and place it in a "inprogress" and "prefile" state
* Once ACARS updates are being processed, then it can go into an 'ENROUTE'
* status, and whatever other statuses may be defined
*
* @param $id
* @param UpdateRequest $request
* @return PirepResource
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
* @throws \Prettus\Validator\Exceptions\ValidatorException
*/
public function update($id, UpdateRequest $request)
{
Log::info('PIREP Update, user ' . Auth::id(), $request->post());
$user = Auth::user();
2018-01-31 01:36:22 +08:00
$pirep = $this->pirepRepo->find($id);
$this->checkCancelled($pirep);
$attrs = $request->post();
$attrs['user_id'] = Auth::id();
# If aircraft is being changed, see if this user is allowed to fly this aircraft
if (array_key_exists('aircraft_id', $attrs)
&& setting('pireps.restrict_aircraft_to_rank', false)
) {
$can_use_ac = $this->userSvc->aircraftAllowed($user, $pirep->aircraft_id);
if (!$can_use_ac) {
throw new BadRequestHttpException('User is not allowed to fly this aircraft');
}
}
2018-01-31 01:36:22 +08:00
$pirep = $this->pirepRepo->update($attrs, $id);
$this->updateFields($pirep, $request);
2018-01-31 01:36:22 +08:00
return new PirepResource($pirep);
}
2017-12-27 04:54:28 +08:00
/**
* File the PIREP
* @param $id
* @param FileRequest $request
2017-12-27 04:54:28 +08:00
* @return PirepResource
2018-01-04 00:25:55 +08:00
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
* @throws \Exception
2017-12-27 04:54:28 +08:00
*/
public function file($id, FileRequest $request)
2017-12-27 04:54:28 +08:00
{
Log::info('PIREP file, user ' . Auth::id(), $request->post());
2018-01-02 06:01:01 +08:00
$user = Auth::user();
# Check if the status is cancelled...
2018-01-04 00:25:55 +08:00
$pirep = $this->pirepRepo->find($id);
$this->checkCancelled($pirep);
2018-01-04 00:25:55 +08:00
$attrs = $request->post();
# If aircraft is being changed, see if this user is allowed to fly this aircraft
if (array_key_exists('aircraft_id', $attrs)
&& setting('pireps.restrict_aircraft_to_rank', false)
) {
$can_use_ac = $this->userSvc->aircraftAllowed($user, $pirep->aircraft_id);
if (!$can_use_ac) {
throw new BadRequestHttpException('User is not allowed to fly this aircraft');
}
}
$attrs['state'] = PirepState::PENDING;
$attrs['status'] = PirepStatus::ARRIVED;
2017-12-27 04:54:28 +08:00
try {
$pirep = $this->pirepRepo->update($attrs, $id);
$pirep = $this->pirepSvc->create($pirep);
$this->updateFields($pirep, $request);
2017-12-27 04:54:28 +08:00
} catch (\Exception $e) {
Log::error($e);
}
# See if there there is any route data posted
# If there isn't, then just write the route data from the
# route that's been posted from the PIREP
$w = ['pirep_id' => $pirep->id, 'type' => AcarsType::ROUTE];
$count = Acars::where($w)->count(['id']);
if($count === 0) {
$this->pirepSvc->saveRoute($pirep);
}
return new PirepResource($pirep);
}
2018-01-04 00:25:55 +08:00
/**
* Cancel the PIREP
* @param $id
* @param Request $request
* @return PirepResource
* @throws \Prettus\Validator\Exceptions\ValidatorException
2018-01-04 00:25:55 +08:00
*/
public function cancel($id, Request $request)
{
Log::info('PIREP Cancel, user ' . Auth::id(), $request->post());
2018-01-04 00:25:55 +08:00
$pirep = $this->pirepRepo->update([
2018-01-04 00:25:55 +08:00
'state' => PirepState::CANCELLED,
], $id);
2018-01-04 00:25:55 +08:00
return new PirepResource($pirep);
}
/**
2017-12-29 04:35:28 +08:00
* Return the GeoJSON for the ACARS line
* @param $id
2017-12-29 04:35:28 +08:00
* @param Request $request
* @return \Illuminate\Contracts\Routing\ResponseFactory
*/
2018-01-05 09:33:23 +08:00
public function acars_geojson($id, Request $request)
{
2017-12-26 08:22:46 +08:00
$pirep = $this->pirepRepo->find($id);
2017-12-29 04:35:28 +08:00
$geodata = $this->geoSvc->getFeatureFromAcars($pirep);
2017-12-26 08:22:46 +08:00
2017-12-29 04:35:28 +08:00
return response(\json_encode($geodata), 200, [
'Content-Type' => 'application/json',
]);
}
2018-01-05 09:33:23 +08:00
/**
* Return the routes for the ACARS line
2018-01-05 09:33:23 +08:00
* @param $id
* @param Request $request
* @return AcarsRouteResource
*/
public function acars_get($id, Request $request)
{
$this->pirepRepo->find($id);
2018-01-05 09:33:23 +08:00
return new AcarsRouteResource(Acars::where([
'pirep_id' => $id,
'type' => AcarsType::FLIGHT_PATH
])->orderBy('created_at', 'asc')->get());
}
/**
* Post ACARS updates for a PIREP
* @param $id
* @param PositionRequest $request
* @return \Illuminate\Http\JsonResponse
2018-01-04 00:25:55 +08:00
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
*/
public function acars_store($id, PositionRequest $request)
{
2018-01-04 00:25:55 +08:00
# Check if the status is cancelled...
$pirep = $this->pirepRepo->find($id);
$this->checkCancelled($pirep);
2018-01-04 00:25:55 +08:00
2018-01-25 03:38:06 +08:00
Log::info(
'Posting ACARS update (user: '.Auth::user()->pilot_id.', pirep id :'.$id.'): ',
$request->post()
);
$count = 0;
$positions = $request->post('positions');
2018-01-05 09:33:23 +08:00
foreach($positions as $position)
{
$position['pirep_id'] = $id;
$position['type'] = AcarsType::FLIGHT_PATH;
$update = Acars::create($position);
$update->save();
++$count;
2018-01-05 09:33:23 +08:00
}
# Change the PIREP status
$pirep->status = PirepStatus::ENROUTE;
$pirep->save();
return $this->message($count . ' positions added', $count);
}
/**
* Post ACARS LOG update for a PIREP. These updates won't show up on the map
* But rather in a log file.
* @param $id
* @param LogRequest $request
* @return \Illuminate\Http\JsonResponse
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
*/
2018-01-31 02:06:46 +08:00
public function acars_logs($id, LogRequest $request)
{
# Check if the status is cancelled...
$pirep = $this->pirepRepo->find($id);
$this->checkCancelled($pirep);
Log::info('Posting ACARS log, PIREP: '.$id, $request->post());
$count = 0;
$logs = $request->post('logs');
foreach($logs as $log) {
$log['pirep_id'] = $id;
$log['type'] = AcarsType::LOG;
2018-01-31 02:06:46 +08:00
$acars = Acars::create($log);
$acars->save();
++$count;
}
return $this->message($count . ' logs added', $count);
}
/**
* Post ACARS LOG update for a PIREP. These updates won't show up on the map
* But rather in a log file.
* @param $id
* @param EventRequest $request
* @return \Illuminate\Http\JsonResponse
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
*/
public function acars_events($id, EventRequest $request)
{
# Check if the status is cancelled...
$pirep = $this->pirepRepo->find($id);
$this->checkCancelled($pirep);
Log::info('Posting ACARS event, PIREP: ' . $id, $request->post());
$count = 0;
$logs = $request->post('events');
foreach ($logs as $log) {
$log['pirep_id'] = $id;
$log['type'] = AcarsType::LOG;
$log['log'] = $log['event'];
$acars = Acars::create($log);
$acars->save();
++$count;
}
return $this->message($count . ' logs added', $count);
2018-01-05 09:33:23 +08:00
}
/**
* Add a new comment
* @param $id
* @param Request $request
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
*/
public function comments_get($id, Request $request)
{
$pirep = $this->pirepRepo->find($id);
return PirepCommentResource::collection($pirep->comments);
}
/**
* Add a new comment
* @param $id
* @param CommentRequest $request
* @return PirepCommentResource
*/
public function comments_post($id, CommentRequest $request)
{
$pirep = $this->pirepRepo->find($id);
$this->checkCancelled($pirep);
Log::info('Posting comment, PIREP: '.$id, $request->post());
# Add it
$comment = new PirepComment($request->post());
$comment->pirep_id = $id;
$comment->user_id = Auth::id();
$comment->save();
return new PirepCommentResource($comment);
}
2018-01-05 09:33:23 +08:00
/**
* @param $id
* @param Request $request
2018-02-21 12:33:09 +08:00
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
2018-01-05 09:33:23 +08:00
*/
public function route_get($id, Request $request)
{
$this->pirepRepo->find($id);
2018-02-11 07:48:51 +08:00
return AcarsRouteResource::collection(Acars::where([
2018-01-05 09:33:23 +08:00
'pirep_id' => $id,
'type' => AcarsType::ROUTE
])->orderBy('order', 'asc')->get());
}
/**
* Post the ROUTE for a PIREP, can be done from the ACARS log
* @param $id
* @param RouteRequest $request
* @return \Illuminate\Http\JsonResponse
2018-01-05 09:33:23 +08:00
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
*/
public function route_post($id, RouteRequest $request)
2018-01-05 09:33:23 +08:00
{
# Check if the status is cancelled...
$pirep = $this->pirepRepo->find($id);
$this->checkCancelled($pirep);
Log::info('Posting ROUTE, PIREP: '.$id, $request->post());
2018-01-05 09:33:23 +08:00
$count = 0;
$route = $request->post('route', []);
2018-01-05 09:33:23 +08:00
foreach($route as $position) {
$position['pirep_id'] = $id;
$position['type'] = AcarsType::ROUTE;
$acars = Acars::create($position);
$acars->save();
++$count;
2018-01-05 09:33:23 +08:00
}
return $this->message($count . ' points added', $count);
2018-01-05 09:33:23 +08:00
}
/**
* @param $id
* @param Request $request
* @return \Illuminate\Http\JsonResponse
2018-02-11 07:48:51 +08:00
* @throws \Exception
2018-01-05 09:33:23 +08:00
*/
public function route_delete($id, Request $request)
{
$this->pirepRepo->find($id);
Acars::where([
'pirep_id' => $id,
'type' => AcarsType::ROUTE
])->delete();
2018-01-05 09:33:23 +08:00
return $this->message('Route deleted');
}
2017-08-15 12:36:49 +08:00
}