-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturkish_cities.go
171 lines (141 loc) · 2.82 KB
/
turkish_cities.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package turkish_cities
import (
"encoding/json"
"io"
"os"
)
type (
TurkishCities struct {
Cities []City
Country Country
}
Context interface {
GetCities() []City
GetCityByID(id int) *City
GetTownByID(cityID, townID int) *Town
GetDistrictByID(cityID, townID, districtID int) *District
GetQuarterByID(cityID, townID, districtID, quarterID int) *Quarter
GetCountry() Country
}
BaseEntity struct {
ID int `json:"id"`
Name string `json:"name"`
Location *Location `json:"location"`
}
Location struct {
Latitude *float64 `json:"lat"`
Longitude *float64 `json:"lon"`
}
City struct {
BaseEntity
Towns []Town `json:"townsData"`
}
Town struct {
BaseEntity
Districts []District `json:"districtsData"`
}
District struct {
BaseEntity
Quarters []Quarter `json:"quarters"`
}
Quarter struct {
BaseEntity
}
Country struct {
Name string
PhoneCode string
Alpha2Code string
Alpha3Code string
Abbreviation string
}
)
func Load() Context {
rawJsonFile, err := os.Open("./static/data.json")
if err != nil {
panic(err)
}
defer func(rawJsonFile *os.File) {
err := rawJsonFile.Close()
if err != nil {
panic(err)
}
}(rawJsonFile)
byteValue, err := io.ReadAll(rawJsonFile)
if err != nil {
panic(err)
}
var cities []City
err = json.Unmarshal(byteValue, &cities)
if err != nil {
panic(err)
}
return &TurkishCities{
Cities: cities,
Country: Country{
Name: "Türkiye",
PhoneCode: "+90",
Alpha2Code: "TR",
Alpha3Code: "TUR",
Abbreviation: "TR",
},
}
}
func (t *TurkishCities) GetCities() []City {
return t.Cities
}
func (t *TurkishCities) GetCityByID(id int) *City {
for _, city := range t.Cities {
if city.ID == id {
return &city
}
}
return nil
}
func (t *TurkishCities) GetTownByID(cityID, townID int) *Town {
city := t.GetCityByID(cityID)
if city == nil {
return nil
}
for _, town := range city.Towns {
if town.ID == townID {
return &town
}
}
return nil
}
func (t *TurkishCities) GetDistrictByID(cityID, townID, districtID int) *District {
town := t.GetTownByID(cityID, townID)
if town == nil {
return nil
}
for _, district := range town.Districts {
if district.ID == districtID {
return &district
}
}
return nil
}
func (t *TurkishCities) GetQuarterByID(cityID, townID, districtID, quarterID int) *Quarter {
district := t.GetDistrictByID(cityID, townID, districtID)
if district == nil {
return nil
}
for _, quarter := range district.Quarters {
if quarter.ID == quarterID {
return &quarter
}
}
return nil
}
func (t *TurkishCities) GetCountry() Country {
return t.Country
}
func (c *City) GetTowns() []Town {
return c.Towns
}
func (t *Town) GetDistricts() []District {
return t.Districts
}
func (d *District) GetQuarters() []Quarter {
return d.Quarters
}