Set fields to pireps in prefile/update calls

This commit is contained in:
Nabeel Shahzad 2018-02-06 13:46:23 -06:00
parent 8b2eddc72d
commit de0c71cb06
5 changed files with 60 additions and 19 deletions

View File

@ -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);
}

View File

@ -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');

View File

@ -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);
}
}

View File

@ -3,8 +3,7 @@
namespace App\Models;
/**
* Class PirepField
*
* Class PirepFieldValues
* @package App\Models
*/
class PirepFieldValues extends BaseModel

View File

@ -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