Add /pireps/id/acars/events endpoint

This commit is contained in:
Nabeel Shahzad 2018-01-30 12:06:46 -06:00
parent c8ac739820
commit ec5478a766
5 changed files with 86 additions and 9 deletions

View File

@ -2,6 +2,7 @@
namespace App\Http\Controllers\Api; namespace App\Http\Controllers\Api;
use App\Http\Requests\Acars\EventRequest;
use App\Http\Requests\Acars\UpdateRequest; use App\Http\Requests\Acars\UpdateRequest;
use Auth; use Auth;
use Log; use Log;
@ -276,7 +277,7 @@ class PirepController extends RestController
* @return \Illuminate\Http\JsonResponse * @return \Illuminate\Http\JsonResponse
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException * @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
*/ */
public function acars_log($id, LogRequest $request) public function acars_logs($id, LogRequest $request)
{ {
# Check if the status is cancelled... # Check if the status is cancelled...
$pirep = $this->pirepRepo->find($id); $pirep = $this->pirepRepo->find($id);
@ -291,9 +292,37 @@ class PirepController extends RestController
$log['pirep_id'] = $id; $log['pirep_id'] = $id;
$log['type'] = AcarsType::LOG; $log['type'] = AcarsType::LOG;
if(array_has($log, 'event')) { $acars = Acars::create($log);
$log['log'] = $log['event']; $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 = Acars::create($log);
$acars->save(); $acars->save();

View File

@ -0,0 +1,33 @@
<?php
namespace App\Http\Requests\Acars;
use Auth;
use App\Models\Pirep;
use Illuminate\Foundation\Http\FormRequest;
/**
* Class EventRequest
* @package App\Http\Requests\Acars
*/
class EventRequest extends FormRequest
{
public function authorize()
{
$pirep = Pirep::findOrFail($this->route('pirep_id'), ['user_id']);
return $pirep->user_id === Auth::id();
}
public function rules()
{
$rules = [
'events' => 'required|array',
'events.*.event' => 'required',
'events.*.lat' => 'nullable|numeric',
'events.*.lon' => 'nullable|numeric',
'events.*.created_at' => 'nullable|date',
];
return $rules;
}
}

View File

@ -23,7 +23,6 @@ class LogRequest extends FormRequest
$rules = [ $rules = [
'logs' => 'required|array', 'logs' => 'required|array',
'logs.*.log' => 'required', 'logs.*.log' => 'required',
'logs.*.event' => 'nullable',
'logs.*.lat' => 'nullable|numeric', 'logs.*.lat' => 'nullable|numeric',
'logs.*.lon' => 'nullable|numeric', 'logs.*.lon' => 'nullable|numeric',
'logs.*.created_at' => 'nullable|date', 'logs.*.created_at' => 'nullable|date',

View File

@ -49,9 +49,9 @@ Route::group(['middleware' => ['api.auth']], function ()
Route::post('pireps/{pirep_id}/acars/position', 'PirepController@acars_store'); Route::post('pireps/{pirep_id}/acars/position', 'PirepController@acars_store');
Route::post('pireps/{pirep_id}/acars/positions', 'PirepController@acars_store'); Route::post('pireps/{pirep_id}/acars/positions', 'PirepController@acars_store');
Route::post('pireps/{pirep_id}/acars/event', 'PirepController@acars_log'); Route::post('pireps/{pirep_id}/acars/events', 'PirepController@acars_events');
Route::post('pireps/{pirep_id}/acars/log', 'PirepController@acars_log'); #Route::post('pireps/{pirep_id}/acars/log', 'PirepController@acars_log');
Route::post('pireps/{pirep_id}/acars/logs', 'PirepController@acars_log'); Route::post('pireps/{pirep_id}/acars/logs', 'PirepController@acars_logs');
Route::get('settings', 'SettingsController@index'); Route::get('settings', 'SettingsController@index');

View File

@ -171,8 +171,10 @@ class AcarsTest extends TestCase
$response = $this->post($uri, [ $response = $this->post($uri, [
'flight_time' => 130, 'flight_time' => 130,
'fuel_used' => 8000.19,
'distance' => 400, 'distance' => 400,
]); ]);
$response->assertStatus(200); $response->assertStatus(200);
# Add a comment # Add a comment
@ -305,7 +307,21 @@ class AcarsTest extends TestCase
] ]
]; ];
$uri = '/api/pireps/' . $pirep_id . '/acars/log'; $uri = '/api/pireps/' . $pirep_id . '/acars/logs';
$response = $this->post($uri, $post_log);
$response->assertStatus(200);
$body = $response->json();
$this->assertEquals(1, $body['count']);
$acars = factory(App\Models\Acars::class)->make();
$post_log = [
'events' => [
['event' => $acars->log]
]
];
$uri = '/api/pireps/' . $pirep_id . '/acars/events';
$response = $this->post($uri, $post_log); $response = $this->post($uri, $post_log);
$response->assertStatus(200); $response->assertStatus(200);
$body = $response->json(); $body = $response->json();