Upsert on ACARS positions #572 (#573)

pull/577/head
Nabeel S 5 years ago committed by GitHub
parent 6e87f7804c
commit 9ed07da9c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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

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

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

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

@ -4,7 +4,7 @@ return [
'themes_path' => resource_path('views/layouts'), // eg: base_path('resources/themes')
'asset_not_found' => 'LOG_ERROR',
'default' => 'default',
'cache' => true,
'cache' => false,
/*
|--------------------------------------------------------------------------

@ -4,6 +4,7 @@ use App\Models\Enums\PirepState;
use App\Models\Enums\PirepStatus;
use App\Models\PirepFare;
use App\Repositories\SettingRepository;
use App\Support\Utils;
/**
* Test API calls and authentication, etc
@ -660,13 +661,22 @@ class AcarsTest extends TestCase
$uri = '/api/pireps/'.$pirep_id.'/acars/position';
// Post an ACARS update
$acars_count = \random_int(2, 10);
$acars = factory(App\Models\Acars::class, $acars_count)->make(['id' => ''])->toArray();
$acars_count = \random_int(5, 10);
$acars = factory(App\Models\Acars::class, $acars_count)->make(['id' => ''])
->map(function ($point) {
$point['id'] = Utils::generateNewId();
return $point;
})
->toArray();
$update = ['positions' => $acars];
$response = $this->post($uri, $update);
$response->assertStatus(200)->assertJson(['count' => $acars_count]);
// Try posting again, should be ignored/not throw any sql errors
$response = $this->post($uri, $update);
$response->assertStatus(200)->assertJson(['count' => $acars_count]);
$response = $this->get($uri);
$response->assertStatus(200)->assertJsonCount($acars_count, 'data');
}
@ -700,11 +710,9 @@ class AcarsTest extends TestCase
$dt = date('c');
$uri = '/api/pireps/'.$pirep_id.'/acars/position';
$acars = factory(App\Models\Acars::class)->make(
[
'sim_time' => $dt,
]
)->toArray();
$acars = factory(App\Models\Acars::class)->make([
'sim_time' => $dt,
])->toArray();
$update = ['positions' => [$acars]];
$response = $this->post($uri, $update);
@ -729,7 +737,13 @@ class AcarsTest extends TestCase
$response->assertStatus(400);
$post_route = [
['order' => 1, 'name' => 'NAVPOINT', 'lat' => 'notanumber', 'lon' => 34.11],
[
'id' => 'NAVPOINT',
'order' => 1,
'name' => 'NAVPOINT',
'lat' => 'notanumber',
'lon' => 34.11,
],
];
$uri = '/api/pireps/'.$pirep_id.'/route';
@ -803,6 +817,10 @@ class AcarsTest extends TestCase
$response = $this->post($uri, ['route' => $post_route]);
$response->assertStatus(200)->assertJson(['count' => $route_count]);
// Try double post to ignore SQL update
$response = $this->post($uri, ['route' => $post_route]);
$response->assertStatus(200)->assertJson(['count' => $route_count]);
/**
* Get
*/

Loading…
Cancel
Save