Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.x] add command to update locations tables #526

Open
wants to merge 18 commits into
base: development
Choose a base branch
from
63 changes: 63 additions & 0 deletions app/Console/Commands/UpdateLocationsTablesCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Kanvas\Apps\Models\Apps;
use Kanvas\Locations\Actions\UpdateAllLocationsAction;
use Kanvas\Locations\Actions\UpdateCitiesAction;
use Kanvas\Locations\Actions\UpdateCountriesAction;
use Kanvas\Locations\Actions\UpdateStatesAction;

class UpdateLocationsTablesCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'kanvas:update-locations-tables {app_uuid} {tableNumber?}';

/**
* The console command description.
*
* @var string|null
*/
protected $description = 'Command description';

public function handle(): void
{
$appUid = $this->argument('app_uuid');
$app = Apps::getByUuid($appUid);
$table = $this->argument('tableNumber') ?? null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tiberius19 instead of # use strings so users know what to use


switch ($table) {
case '1':
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tiberius19 use match so you can test how it works

$countries = new UpdateCountriesAction();
$countries->execute($app);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tiberius19 pass the app at the constructor

break;
case '2':
$states = new UpdateStatesAction();
$states->execute($app);
break;
case '3':
$cities = new UpdateCitiesAction();
$cities->execute($app);
break;
case null:
$countries = new UpdateCountriesAction();
$countries->execute($app);

$states = new UpdateStatesAction();
$states->execute($app);

$cities = new UpdateCitiesAction();
$cities->execute($app);
break;
}

$this->info('Tables updated successfully.');
}
}
9 changes: 9 additions & 0 deletions config/locations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

return [
'locationsUrl' => [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tiberius19 this is not needed right?

'countries' => env('COUNTRIES_URL_CSV'),
'cities' => env('CITIES_URL_CSV'),
'states' => env('STATES_URL_CSV'),
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('countries_cities', function (Blueprint $table) {
$table->decimal('latitude', 10, 10)->nullable()->change();
$table->decimal('longitude', 10, 10)->nullable()->change();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
//
}
};
55 changes: 55 additions & 0 deletions src/Kanvas/Locations/Actions/UpdateCitiesAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Kanvas\Locations\Actions;

use Kanvas\Locations\Models\Cities;

class UpdateCitiesAction
{
/**
* __construct.
*
* @return void
*/
public function __construct(
) {
}

/**
* execute.
*
* @return bool
*/
public function execute($app): bool
{
$i = 0;
if (($handle = fopen($app->get('cities_url'), "r")) !== false) {
while (($importData = fgetcsv($handle, 1000, ",")) !== false) {
if ($i === 0) {
$i = 1;
continue;
}
// Remove the first iteration as it's not "real" datas

Cities::updateOrCreate(
[
"id" => $importData[0],
"states_id" => $importData[2],
"countries_id" => $importData[5],
],
[
"name" => $importData[1],
"latitude" => $importData[8],
"longitude" => $importData[9],
]
);
$i++;
}
fclose($handle);
return true;
}
return false;
}
}
52 changes: 52 additions & 0 deletions src/Kanvas/Locations/Actions/UpdateCountriesAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Kanvas\Locations\Actions;

use Kanvas\Locations\Models\Countries;

class UpdateCountriesAction
{
/**
* __construct.
*
* @return void
*/
public function __construct(
) {
}

/**
* execute.
*
* @return bool
*/
public function execute($app): bool
{
$i = 0;
if (($handle = fopen($app->get('countries_url'), "r")) !== false) {
while (($importData = fgetcsv($handle, 1000, ",")) !== false) {
if ($i === 0) {
$i = 1;
continue;
}
// Remove the first iteration as it's not "real" datas

Countries::updateOrCreate(
[
"id" => $importData[0],
"name" => $importData[1],
],
[
"code" => $importData[3],
]
);
$i++;
}
fclose($handle);
return true;
}
return false;
}
}
53 changes: 53 additions & 0 deletions src/Kanvas/Locations/Actions/UpdateStatesAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Kanvas\Locations\Actions;

use Kanvas\Locations\Models\States;

class UpdateStatesAction
{
/**
* __construct.
*
* @return void
*/
public function __construct(
) {
}

/**
* execute.
*
* @return bool
*/
public function execute($app): bool
{
$i = 0;
if (($handle = fopen($app->get('states_url'), "r")) !== false) {
while (($importData = fgetcsv($handle, 1000, ",")) !== false) {
if ($i === 0) {
$i = 1;
continue;
}
// Remove the first iteration as it's not "real" data

States::updateOrCreate(
[
"id" => $importData[0],
"countries_id" => $importData[2],
"name" => $importData[1],
],
[
"code" => $importData[5],
]
);
$i++;
}
fclose($handle);
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Tests\Ecosystem\Integration\Apps;

use Kanvas\Locations\Actions\UpdateCountriesAction;
use Kanvas\Apps\Models\Apps;
use Tests\TestCase;

final class UpdateCountriesActionTest extends TestCase
{
/**
* Test Update Or Create Countries.
*
* @return void
*/
public function UpdateCountriesAction(): void
{
$updateCountries = new UpdateCountriesAction();

$this->assertTrue(
$updateCountries->execute(app(Apps::class))
);
}
}
26 changes: 26 additions & 0 deletions tests/Ecosystem/Integration/Locations/UpdateStatesActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Tests\Ecosystem\Integration\Apps;

use Kanvas\Locations\Actions\UpdateStatesAction;
use Kanvas\Apps\Models\Apps;
use Tests\TestCase;

final class UpdateStatesActionTest extends TestCase
{
/**
* Test Update Or Create States.
*
* @return void
*/
public function UpdateStatesAction(): void
{
$updateStates = new UpdateStatesAction();

$this->assertTrue(
$updateStates->execute(app(Apps::class))
);
}
}
Loading