-
Notifications
You must be signed in to change notification settings - Fork 3
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
base: development
Are you sure you want to change the base?
Changes from 14 commits
e0f651f
0ce9b33
031fbe8
66fff62
2364b0f
a717528
874ee41
9da48cd
cfbd595
3812ed1
a24ce6f
7aabee4
d773306
f4e311f
61045d7
f61f79d
a5e3eed
80f3ce5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
|
||
switch ($table) { | ||
case '1': | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.'); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
return [ | ||
'locationsUrl' => [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
{ | ||
// | ||
} | ||
}; |
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; | ||
} | ||
} |
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; | ||
} | ||
} |
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)) | ||
); | ||
} | ||
} |
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)) | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
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