Repository navigation
Expand file tree
/
Copy pathcountry.go
More file actions
180 lines (162 loc) · 5.11 KB
/
Copy pathcountry.go
File metadata and controls
180 lines (162 loc) · 5.11 KB
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
172
173
174
175
176
177
178
179
180
package main
import (
"database/sql"
"fmt"
"log"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
type Country struct {
Code string
Name string
CapitalCity *CityLite
Gdp int
Population int
HasRegionIcons bool
Continent *Continent
}
type CountryData struct {
Code string
Name string
CapitalCityId int
Gdp int
Population int
HasRegionIcons bool
ContinentCode string
}
func (c *Country) GdpFormatted() string {
return message.NewPrinter(language.English).Sprint(c.Gdp)
}
func (c *Country) PopulationFormatted() string {
return message.NewPrinter(language.English).Sprint(c.Population)
}
func LoadCountriesByContinentCode(db *sql.DB, continentCode string) ([]Country, error) {
defer trace(traceName(fmt.Sprintf("LoadCountriesByContinentCode(%s)", continentCode)))
rows, err := db.Query(
"SELECT co.code, co.name, co.gdp, co.population, co.has_region_icons,"+
" ci.city_id, ci.city_name, ci.region_id, ci.region_code,"+
" ct.code, ct.name"+
" FROM countries co"+
" INNER JOIN continents ct ON ct.code = co.continent_code"+
" LEFT JOIN city_view ci ON ci.city_id = co.capital_city_id"+
" WHERE co.continent_code=?", continentCode)
if err != nil {
return nil, err
}
defer rows.Close()
return readCountryListFromRows(rows)
}
func LoadCountryByCode(db *sql.DB, code string) (*Country, error) {
defer trace(traceName(fmt.Sprintf("LoadCountryBycode(%s)", code)))
rows, err := db.Query(
"SELECT co.code, co.name, co.gdp, co.population, co.has_region_icons,"+
" ci.city_id, ci.city_name, ci.region_id, ci.region_code,"+
" ct.code, ct.name"+
" FROM countries co"+
" INNER JOIN continents ct ON ct.code = co.continent_code"+
" LEFT JOIN city_view ci ON ci.city_id = co.capital_city_id"+
" WHERE co.code=?", code)
if err != nil {
return nil, err
}
defer rows.Close()
if rows.Next() {
return readCountryFromRows(rows)
} else {
return nil, fmt.Errorf("Country not found. Code: %s", code)
}
}
func LoadCountryList(db *sql.DB) ([]Country, error) {
defer trace(traceName("LoadCountryList"))
rows, err := db.Query(
"SELECT co.code, co.name, co.gdp, co.population, co.has_region_icons," +
" ci.city_id, ci.city_name, ci.region_id, ci.region_code," +
" ct.code, ct.name" +
" FROM countries co" +
" INNER JOIN continents ct ON ct.code = co.continent_code" +
" LEFT JOIN city_view ci ON ci.city_id = co.capital_city_id" +
" ORDER BY co.name")
if err != nil {
return nil, err
}
defer rows.Close()
return readCountryListFromRows(rows)
}
func readCountryListFromRows(rows *sql.Rows) ([]Country, error) {
var countries []Country
for rows.Next() {
item, err := readCountryFromRows(rows)
if err != nil {
return nil, err
}
countries = append(countries, *item)
}
return countries, nil
}
func readCountryFromRows(rows *sql.Rows) (*Country, error) {
country := Country{
Continent: &Continent{},
}
var capitalCityId, capitalCityRegionId sql.NullInt64
var capitalCityName, capitalCityRegionCode sql.NullString
err := rows.Scan(&country.Code, &country.Name, &country.Gdp, &country.Population, &country.HasRegionIcons,
&capitalCityId, &capitalCityName, &capitalCityRegionId, &capitalCityRegionCode,
&country.Continent.Code, &country.Continent.Name)
if capitalCityId.Valid {
country.CapitalCity = &CityLite{
Id: int(capitalCityId.Int64),
Name: capitalCityName.String,
RegionId: int(capitalCityRegionId.Int64),
RegionAbbr: capitalCityRegionCode.String,
}
}
if err != nil {
return nil, err
}
return &country, nil
}
func DeleteCountryByCode(db *sql.DB, code string) error {
defer trace(traceName(fmt.Sprintf("DeleteCountryByCode(%s)", code)))
// Delete the row
res, err := db.Exec("DELETE FROM countries WHERE code=?", code)
if err != nil {
return err
}
numAffected, err := res.RowsAffected()
if err != nil {
return err
}
if numAffected == 0 {
return fmt.Errorf("Country not found, code: %s", code)
}
return nil
}
func InsertCountry(db *sql.DB, data CountryData) error {
log.Printf("Insert country (data: %v)", data)
nullableCapitalCityId := sql.NullInt64{
Int64: int64(data.CapitalCityId),
Valid: data.CapitalCityId > 0,
}
res, err := db.Exec("INSERT INTO countries"+
" (name, code, capital_city_id, gdp, population, has_region_icons, continent_code)"+
" VALUES(?, ?, ?, ?, ?, ?, ?)",
data.Name, data.Code, nullableCapitalCityId, data.Gdp, data.Population, data.HasRegionIcons, data.ContinentCode)
if err != nil {
return err
}
_, err = res.LastInsertId()
return err
}
func UpdateCountry(db *sql.DB, originalCode string, data CountryData) error {
log.Printf("Update country (originalCode: %s, data: %v)", originalCode, data)
nullableCapitalCityId := sql.NullInt64{
Int64: int64(data.CapitalCityId),
Valid: data.CapitalCityId > 0,
}
_, err := db.Exec("UPDATE countries "+
"SET name=?, code=?, capital_city_id=?, gdp=?, population=?, has_region_icons=?, continent_code=? "+
"WHERE code=?",
data.Name, data.Code, nullableCapitalCityId, data.Gdp, data.Population, data.HasRegionIcons, data.ContinentCode,
originalCode)
return err
}