Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kimtrien committed Jun 16, 2020
1 parent 1bec7db commit 3e386b7
Show file tree
Hide file tree
Showing 12 changed files with 512 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/.idea
/vendor
/node_modules
composer.lock
composer.phar
phpunit.xml
32 changes: 32 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "kjmtrue/vietnam-zone",
"description": "VietNam Zone",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "KimTrien",
"email": "[email protected]"
}
],
"minimum-stability": "dev",
"require": {
"illuminate/support": "^5.8|^6|^7",
"illuminate/database": "^5.8|^6|^7",
"illuminate/console": "^5.8|^6|^7",
"guzzlehttp/guzzle": "^6.3.1|^7.0",
"maatwebsite/excel": "^3.0"
},
"autoload": {
"psr-4": {
"Kjmtrue\\VietnamZone\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Kjmtrue\\VietnamZone\\ServiceProvider"
]
}
}
}
15 changes: 15 additions & 0 deletions config/vietnam-zone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
return [
'tables' => [
'provinces' => 'provinces',
'districts' => 'districts',
'wards' => 'wards',
],

'columns' => [
'name' => 'name',
'gso_id' => 'gso_id',
'province_id' => 'province_id',
'district_id' => 'district_id',
],
];
33 changes: 33 additions & 0 deletions database/migrations/2020_01_01_000001_create_provinces_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

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

class CreateProvincesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create(config('vietnam-zone.tables.provinces'), function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('gso_id');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists(config('vietnam-zone.tables.provinces'));
}
}
39 changes: 39 additions & 0 deletions database/migrations/2020_01_01_000002_create_districts_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

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

class CreateDistrictsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create(config('vietnam-zone.tables.districts'), function (Blueprint $table) {
$table->bigIncrements('id');
$table->string(config('vietnam-zone.columns.name'));
$table->string(config('vietnam-zone.columns.gso_id'));
$table->unsignedBigInteger(config('vietnam-zone.columns.province_id'));
$table->timestamps();

$table->foreign(config('vietnam-zone.columns.province_id'))
->references('id')
->on(config('vietnam-zone.tables.provinces'))
->cascadeOnDelete();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists(config('vietnam-zone.tables.districts'));
}
}
39 changes: 39 additions & 0 deletions database/migrations/2020_01_01_000003_create_wards_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

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

class CreateWardsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create(config('vietnam-zone.tables.wards'), function (Blueprint $table) {
$table->bigIncrements('id');
$table->string(config('vietnam-zone.columns.name'));
$table->string(config('vietnam-zone.columns.gso_id'));
$table->unsignedBigInteger(config('vietnam-zone.columns.district_id'));
$table->timestamps();

$table->foreign(config('vietnam-zone.columns.district_id'))
->references('id')
->on(config('vietnam-zone.tables.districts'))
->cascadeOnDelete();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists(config('vietnam-zone.tables.wards'));
}
}
73 changes: 73 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
## VietNam Zone

Database đơn vị hành chính của Việt Nam

Dữ liệu được lấy trực tiếp từ Tổng Cục Thống Kê Việt Nam.

Đảm bảo luôn luôn là dữ liệu mới nhất và chính xác nhất.

## Cài đặt

```shell
composer require kjmtrue/vietnam-zone
```

#### Copy file config và migration

```shell
php artisan vendor:publish --provider="Kjmtrue\VietnamZone\ServiceProvider"
```

#### Chỉnh sửa config và migration nếu bạn cần tuỳ biến cho dự án

1. Đổi tên bảng

Mở file `config/vietnam-zone.php` chỉnh các cấu hình sau:

```php
'tables' => [
'provinces' => 'provinces',
'districts' => 'districts',
'wards' => 'wards',
],
```

2. Đổi tên column

Mở file `config/vietnam-zone.php` chỉnh các cấu hình sau:

```php
'columns' => [
'name' => 'name',
'gso_id' => 'gso_id',
'province_id' => 'province_id',
'district_id' => 'district_id',
],
```

3. Thêm column

Mở các file migration sau và tuỳ chỉnh theo ý thích

```shell
database/migrations/2020_01_01_000001_create_provinces_table.php
database/migrations/2020_01_01_000002_create_districts_table.php
database/migrations/2020_01_01_000003_create_wards_table.php
```

#### Chạy migration

```shell
php artisan migrate
```

#### Download và import dữ liệu vào database

```shell
php artisan vietnamzone:download
```

#### Todo

- [ ] Command cập nhật dữ liệu
- [ ] Download file trực tiếp từu website tổng cục thống kê
44 changes: 44 additions & 0 deletions src/Console/Commands/DownloadCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Kjmtrue\VietnamZone\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Kjmtrue\VietnamZone\Downloader;
use Kjmtrue\VietnamZone\Imports\VienamZoneImport;
use Maatwebsite\Excel\Facades\Excel;

class DownloadCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'vietnamzone:download';

/**
* The console command description.
*
* @var string
*/
protected $description = 'VietNam Zone Download Data';

/**
* Execute the console command.
*/
public function handle()
{
$this->info('Downloading...');

$tmpFile = app(Downloader::class)->downloadFile();

$this->info('Importing...');

Excel::import(new VienamZoneImport(), $tmpFile);

File::delete($tmpFile);

$this->info('Completed');
}
}
30 changes: 30 additions & 0 deletions src/Console/Commands/UpdateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Kjmtrue\VietnamZone\Console\Commands;

use Illuminate\Console\Command;

class UpdateCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'vietnamzone:update';

/**
* The console command description.
*
* @var string
*/
protected $description = 'VietNam Zone Update Data';

/**
* Execute the console command.
*/
public function handle()
{
$this->info('Updating...');
}
}
29 changes: 29 additions & 0 deletions src/Downloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Kjmtrue\VietnamZone;

use GuzzleHttp\Client;

class Downloader
{
const DOWNLOAD_URL = 'https://github.com/kjmtrue/vietnam-zone/raw/database/vietnam-zone.xls';

/**
* Download database VietNam Zone
*
* @return string|null
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function downloadFile()
{
$client = new Client([
'verify' => false
]);

$res = $client->get(self::DOWNLOAD_URL, [
'save_to' => storage_path('vietnam-zone.xls')
]);

return $res->getStatusCode() == 200 ? storage_path('vietnam-zone.xls') : null;
}
}
Loading

0 comments on commit 3e386b7

Please sign in to comment.