diff --git a/app/Http/Controllers/Api/PirepController.php b/app/Http/Controllers/Api/PirepController.php index 0e61194d..4391bb27 100644 --- a/app/Http/Controllers/Api/PirepController.php +++ b/app/Http/Controllers/Api/PirepController.php @@ -82,6 +82,28 @@ class PirepController extends RestController return new PirepResource($this->pirepRepo->find($id)); } + /** + * @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' @@ -112,6 +134,8 @@ class PirepController extends RestController Log::info('PIREP PREFILED'); Log::info($pirep->id); + $this->updateFields($pirep, $request); + PirepResource::withoutWrapping(); return new PirepResource($pirep); } @@ -138,6 +162,7 @@ class PirepController extends RestController $attrs['user_id'] = Auth::id(); $pirep = $this->pirepRepo->update($attrs, $id); + $this->updateFields($pirep, $request); PirepResource::withoutWrapping(); return new PirepResource($pirep); @@ -163,14 +188,10 @@ class PirepController extends RestController $attrs['state'] = PirepState::PENDING; $attrs['status'] = PirepStatus::ARRIVED; - $pirep_fields = []; - if($request->filled('fields')) { - $pirep_fields = $request->get('fields'); - } - try { $pirep = $this->pirepRepo->update($attrs, $id); - $pirep = $this->pirepSvc->create($pirep, $pirep_fields); + $pirep = $this->pirepSvc->create($pirep); + $this->updateFields($pirep, $request); } catch (\Exception $e) { Log::error($e); } diff --git a/app/Http/Controllers/Frontend/PirepController.php b/app/Http/Controllers/Frontend/PirepController.php index 21647fa5..4a14700d 100644 --- a/app/Http/Controllers/Frontend/PirepController.php +++ b/app/Http/Controllers/Frontend/PirepController.php @@ -168,7 +168,6 @@ class PirepController extends Controller public function show($id) { - #$pirep = Pirep::where('id', $id); $pirep = $this->pirepRepo->find($id); if (empty($pirep)) { Flash::error('Pirep not found'); diff --git a/app/Models/PirepField.php b/app/Models/PirepField.php index 7c8cfc25..24e4a7c7 100644 --- a/app/Models/PirepField.php +++ b/app/Models/PirepField.php @@ -2,11 +2,8 @@ namespace App\Models; -use Log; - /** * Class PirepField - * * @package App\Models */ class PirepField extends BaseModel @@ -49,4 +46,14 @@ class PirepField extends BaseModel $model->slug = str_slug($model->name); }); } + + /** + * When setting the name attribute, also set the slug + * @param $name + */ + public function setNameAttribute($name) + { + $this->attributes['name'] = $name; + $this->attributes['slug'] = str_slug($name); + } } diff --git a/app/Models/PirepFieldValues.php b/app/Models/PirepFieldValues.php index 23829dd1..40d58719 100644 --- a/app/Models/PirepFieldValues.php +++ b/app/Models/PirepFieldValues.php @@ -3,8 +3,7 @@ namespace App\Models; /** - * Class PirepField - * + * Class PirepFieldValues * @package App\Models */ class PirepFieldValues extends BaseModel diff --git a/app/Services/PIREPService.php b/app/Services/PIREPService.php index 5a17e27f..fa723c27 100644 --- a/app/Services/PIREPService.php +++ b/app/Services/PIREPService.php @@ -2,6 +2,7 @@ namespace App\Services; +use App\Models\PirepField; use Log; use Carbon\Carbon; use App\Repositories\AcarsRepository; @@ -177,13 +178,8 @@ class PIREPService extends BaseService $pirep->save(); $pirep->refresh(); - foreach ($field_values as $fv) { - $v = new PirepFieldValues(); - $v->pirep_id = $pirep->id; - $v->name = $fv['name']; - $v->value = $fv['value']; - $v->source = $fv['source']; - $v->save(); + if(\count($field_values) > 0) { + $this->updateCustomFields($pirep->id, $field_values); } Log::info('New PIREP filed', [$pirep]); @@ -198,6 +194,25 @@ class PIREPService extends BaseService return $pirep; } + /** + * Update any custom PIREP fields + * @param $pirep_id + * @param array $field_values + */ + public function updateCustomFields($pirep_id, array $field_values) + { + foreach ($field_values as $fv) { + PirepFieldValues::updateOrCreate( + [ 'pirep_id' => $pirep_id, + 'name' => $fv['name'] + ], + [ 'value' => $fv['value'], + 'source' => $fv['source'] + ] + ); + } + } + /** * @param Pirep $pirep * @param int $new_state