Set fields to pireps in prefile/update calls
This commit is contained in:
parent
8b2eddc72d
commit
de0c71cb06
@ -82,6 +82,28 @@ class PirepController extends RestController
|
|||||||
return new PirepResource($this->pirepRepo->find($id));
|
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
|
* 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'
|
* 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 PREFILED');
|
||||||
Log::info($pirep->id);
|
Log::info($pirep->id);
|
||||||
|
|
||||||
|
$this->updateFields($pirep, $request);
|
||||||
|
|
||||||
PirepResource::withoutWrapping();
|
PirepResource::withoutWrapping();
|
||||||
return new PirepResource($pirep);
|
return new PirepResource($pirep);
|
||||||
}
|
}
|
||||||
@ -138,6 +162,7 @@ class PirepController extends RestController
|
|||||||
$attrs['user_id'] = Auth::id();
|
$attrs['user_id'] = Auth::id();
|
||||||
|
|
||||||
$pirep = $this->pirepRepo->update($attrs, $id);
|
$pirep = $this->pirepRepo->update($attrs, $id);
|
||||||
|
$this->updateFields($pirep, $request);
|
||||||
|
|
||||||
PirepResource::withoutWrapping();
|
PirepResource::withoutWrapping();
|
||||||
return new PirepResource($pirep);
|
return new PirepResource($pirep);
|
||||||
@ -163,14 +188,10 @@ class PirepController extends RestController
|
|||||||
$attrs['state'] = PirepState::PENDING;
|
$attrs['state'] = PirepState::PENDING;
|
||||||
$attrs['status'] = PirepStatus::ARRIVED;
|
$attrs['status'] = PirepStatus::ARRIVED;
|
||||||
|
|
||||||
$pirep_fields = [];
|
|
||||||
if($request->filled('fields')) {
|
|
||||||
$pirep_fields = $request->get('fields');
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$pirep = $this->pirepRepo->update($attrs, $id);
|
$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) {
|
} catch (\Exception $e) {
|
||||||
Log::error($e);
|
Log::error($e);
|
||||||
}
|
}
|
||||||
|
@ -168,7 +168,6 @@ class PirepController extends Controller
|
|||||||
|
|
||||||
public function show($id)
|
public function show($id)
|
||||||
{
|
{
|
||||||
#$pirep = Pirep::where('id', $id);
|
|
||||||
$pirep = $this->pirepRepo->find($id);
|
$pirep = $this->pirepRepo->find($id);
|
||||||
if (empty($pirep)) {
|
if (empty($pirep)) {
|
||||||
Flash::error('Pirep not found');
|
Flash::error('Pirep not found');
|
||||||
|
@ -2,11 +2,8 @@
|
|||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use Log;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class PirepField
|
* Class PirepField
|
||||||
*
|
|
||||||
* @package App\Models
|
* @package App\Models
|
||||||
*/
|
*/
|
||||||
class PirepField extends BaseModel
|
class PirepField extends BaseModel
|
||||||
@ -49,4 +46,14 @@ class PirepField extends BaseModel
|
|||||||
$model->slug = str_slug($model->name);
|
$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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,8 +3,7 @@
|
|||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class PirepField
|
* Class PirepFieldValues
|
||||||
*
|
|
||||||
* @package App\Models
|
* @package App\Models
|
||||||
*/
|
*/
|
||||||
class PirepFieldValues extends BaseModel
|
class PirepFieldValues extends BaseModel
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Services;
|
namespace App\Services;
|
||||||
|
|
||||||
|
use App\Models\PirepField;
|
||||||
use Log;
|
use Log;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use App\Repositories\AcarsRepository;
|
use App\Repositories\AcarsRepository;
|
||||||
@ -177,13 +178,8 @@ class PIREPService extends BaseService
|
|||||||
$pirep->save();
|
$pirep->save();
|
||||||
$pirep->refresh();
|
$pirep->refresh();
|
||||||
|
|
||||||
foreach ($field_values as $fv) {
|
if(\count($field_values) > 0) {
|
||||||
$v = new PirepFieldValues();
|
$this->updateCustomFields($pirep->id, $field_values);
|
||||||
$v->pirep_id = $pirep->id;
|
|
||||||
$v->name = $fv['name'];
|
|
||||||
$v->value = $fv['value'];
|
|
||||||
$v->source = $fv['source'];
|
|
||||||
$v->save();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Log::info('New PIREP filed', [$pirep]);
|
Log::info('New PIREP filed', [$pirep]);
|
||||||
@ -198,6 +194,25 @@ class PIREPService extends BaseService
|
|||||||
return $pirep;
|
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 Pirep $pirep
|
||||||
* @param int $new_state
|
* @param int $new_state
|
||||||
|
Loading…
Reference in New Issue
Block a user