Upsert on ACARS positions #572 (#573)

This commit is contained in:
Nabeel S
2020-02-22 16:03:01 -05:00
committed by GitHub
parent 6e87f7804c
commit 9ed07da9c3
6 changed files with 91 additions and 28 deletions

View File

@@ -154,7 +154,7 @@ class AcarsController extends Controller
$position['pirep_id'] = $id;
$position['type'] = AcarsType::FLIGHT_PATH;
if (array_key_exists('sim_time', $position)) {
if (isset($position['sim_time'])) {
if ($position['sim_time'] instanceof \DateTime) {
$position['sim_time'] = Carbon::instance($position['sim_time']);
} else {
@@ -162,7 +162,7 @@ class AcarsController extends Controller
}
}
if (array_key_exists('created_at', $position)) {
if (isset($position['created_at'])) {
if ($position['created_at'] instanceof \DateTime) {
$position['created_at'] = Carbon::instance($position['created_at']);
} else {
@@ -171,8 +171,16 @@ class AcarsController extends Controller
}
try {
$update = Acars::create($position);
$update->save();
if (isset($position['id'])) {
Acars::updateOrInsert(
['id' => $position['id']],
$position
);
} else {
$update = Acars::create($position);
$update->save();
}
$count++;
} catch (QueryException $ex) {
Log::info('Error on adding ACARS position: '.$ex->getMessage());
@@ -215,17 +223,25 @@ class AcarsController extends Controller
$log['pirep_id'] = $id;
$log['type'] = AcarsType::LOG;
if (array_key_exists('sim_time', $log)) {
if (isset($log['sim_time'])) {
$log['sim_time'] = Carbon::createFromTimeString($log['sim_time']);
}
if (array_key_exists('created_at', $log)) {
if (isset($log['created_at'])) {
$log['created_at'] = Carbon::createFromTimeString($log['created_at']);
}
try {
$acars = Acars::create($log);
$acars->save();
if (isset($log['id'])) {
Acars::updateOrInsert(
['id' => $log['id']],
$log
);
} else {
$acars = Acars::create($log);
$acars->save();
}
$count++;
} catch (QueryException $ex) {
Log::info('Error on adding ACARS position: '.$ex->getMessage());
@@ -262,17 +278,25 @@ class AcarsController extends Controller
$log['type'] = AcarsType::LOG;
$log['log'] = $log['event'];
if (array_key_exists('sim_time', $log)) {
if (isset($log['sim_time'])) {
$log['sim_time'] = Carbon::createFromTimeString($log['sim_time']);
}
if (array_key_exists('created_at', $log)) {
if (isset($log['created_at'])) {
$log['created_at'] = Carbon::createFromTimeString($log['created_at']);
}
try {
$acars = Acars::create($log);
$acars->save();
if (isset($log['id'])) {
Acars::updateOrInsert(
['id' => $log['id']],
$log
);
} else {
$acars = Acars::create($log);
$acars->save();
}
$count++;
} catch (QueryException $ex) {
Log::info('Error on adding ACARS position: '.$ex->getMessage());

View File

@@ -542,8 +542,12 @@ class PirepController extends Controller
$position['pirep_id'] = $id;
$position['type'] = AcarsType::ROUTE;
$acars = Acars::create($position);
$acars->save();
if (isset($position['id'])) {
Acars::updateOrInsert(['id' => $position['id']], $position);
} else {
$acars = Acars::create($position);
$acars->save();
}
$count++;
}

View File

@@ -2,8 +2,7 @@
namespace App\Models\Traits;
use App\Contracts\Model;
use Hashids\Hashids;
use App\Support\Utils;
trait HashIdTrait
{
@@ -14,9 +13,7 @@ trait HashIdTrait
*/
final protected static function createNewHashId(): string
{
$hashids = new Hashids('', Model::ID_MAX_LENGTH);
$mt = str_replace('.', '', microtime(true));
return $hashids->encode($mt);
return Utils::generateNewId();
}
/**

View File

@@ -2,6 +2,8 @@
namespace App\Support;
use App\Contracts\Model;
use Hashids\Hashids;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Support\Str;
use Nwidart\Modules\Facades\Module;
@@ -11,6 +13,24 @@ use Nwidart\Modules\Facades\Module;
*/
class Utils
{
/**
* Generate a new ID with a given length
*
* @param int [$length]
*
* @return string
*/
public static function generateNewId(int $length = null)
{
if (!$length) {
$length = Model::ID_MAX_LENGTH;
}
$hashids = new Hashids('', $length);
$mt = str_replace('.', '', microtime(true));
return $hashids->encode($mt);
}
/**
* Returns a 40 character API key that a user can use
*