-
Notifications
You must be signed in to change notification settings - Fork 1
/
adminarea.go
93 lines (77 loc) · 2.03 KB
/
adminarea.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package adminarea
import (
"log"
idres "github.com/ramadani/adminarea/resources/indonesia"
"github.com/ramadani/adminarea/src"
"github.com/ramadani/adminarea/src/indonesia"
)
// AdminArea to get administrative areas of country
type AdminArea struct {
country *src.Country
province src.ProvinceContract
regency src.RegencyContract
district src.DistrictContract
village src.VillageContract
}
// GetCountry of adminarea
func (aa *AdminArea) GetCountry() *src.Country {
return aa.country
}
// GetProvinces get provinces by country
func (aa *AdminArea) GetProvinces() ([]*src.Province, error) {
return aa.province.All()
}
// GetRegencies get regencies by country
func (aa *AdminArea) GetRegencies() ([]*src.Regency, error) {
return aa.regency.All()
}
// GetDistricts get districts by country
func (aa *AdminArea) GetDistricts() ([]*src.District, error) {
return aa.district.All()
}
// GetVillages get villages by country
func (aa *AdminArea) GetVillages() ([]*src.Village, error) {
return aa.village.All()
}
// New instance of AdminArea
func New(countryID string) *AdminArea {
var province src.ProvinceContract
var regency src.RegencyContract
var district src.DistrictContract
var village src.VillageContract
countryRepo := src.NewCountryRepository()
country, err := countryRepo.FindByID(countryID)
if err != nil {
log.Fatal(err)
}
switch country.ID {
case "ID":
prContents, err := idres.Asset("provinces.csv")
if err != nil {
log.Fatal(err)
}
rgContents, err := idres.Asset("regencies.csv")
if err != nil {
log.Fatal(err)
}
dsContents, err := idres.Asset("districts.csv")
if err != nil {
log.Fatal(err)
}
vlContents, err := idres.Asset("villages.csv")
if err != nil {
log.Fatal(err)
}
province = indonesia.NewProvince(country.ID, prContents)
regency = indonesia.NewRegency(country.ID, rgContents)
district = indonesia.NewDistrict(country.ID, dsContents)
village = indonesia.NewVillage(country.ID, vlContents)
}
return &AdminArea{
country,
province,
regency,
district,
village,
}
}