diff --git a/app/Http/Controllers/Api/PirepController.php b/app/Http/Controllers/Api/PirepController.php index 72a5dbfb..0e61194d 100644 --- a/app/Http/Controllers/Api/PirepController.php +++ b/app/Http/Controllers/Api/PirepController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers\Api; +use App\Http\Requests\Acars\EventRequest; use App\Http\Requests\Acars\UpdateRequest; use Auth; use Log; @@ -276,7 +277,7 @@ class PirepController extends RestController * @return \Illuminate\Http\JsonResponse * @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... $pirep = $this->pirepRepo->find($id); @@ -291,9 +292,37 @@ class PirepController extends RestController $log['pirep_id'] = $id; $log['type'] = AcarsType::LOG; - if(array_has($log, 'event')) { - $log['log'] = $log['event']; - } + $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(); diff --git a/app/Http/Requests/Acars/EventRequest.php b/app/Http/Requests/Acars/EventRequest.php new file mode 100644 index 00000000..1c4a6dea --- /dev/null +++ b/app/Http/Requests/Acars/EventRequest.php @@ -0,0 +1,33 @@ +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; + } +} diff --git a/app/Http/Requests/Acars/LogRequest.php b/app/Http/Requests/Acars/LogRequest.php index 68f34613..431fc120 100644 --- a/app/Http/Requests/Acars/LogRequest.php +++ b/app/Http/Requests/Acars/LogRequest.php @@ -23,7 +23,6 @@ class LogRequest extends FormRequest $rules = [ 'logs' => 'required|array', 'logs.*.log' => 'required', - 'logs.*.event' => 'nullable', 'logs.*.lat' => 'nullable|numeric', 'logs.*.lon' => 'nullable|numeric', 'logs.*.created_at' => 'nullable|date', diff --git a/app/Routes/api.php b/app/Routes/api.php index 8bf16b2d..0cc65774 100755 --- a/app/Routes/api.php +++ b/app/Routes/api.php @@ -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/positions', 'PirepController@acars_store'); - Route::post('pireps/{pirep_id}/acars/event', '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/events', 'PirepController@acars_events'); + #Route::post('pireps/{pirep_id}/acars/log', 'PirepController@acars_log'); + Route::post('pireps/{pirep_id}/acars/logs', 'PirepController@acars_logs'); Route::get('settings', 'SettingsController@index'); diff --git a/tests/AcarsTest.php b/tests/AcarsTest.php index ef0f5555..8621654c 100644 --- a/tests/AcarsTest.php +++ b/tests/AcarsTest.php @@ -171,8 +171,10 @@ class AcarsTest extends TestCase $response = $this->post($uri, [ 'flight_time' => 130, + 'fuel_used' => 8000.19, 'distance' => 400, ]); + $response->assertStatus(200); # 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->assertStatus(200); $body = $response->json();