-
Notifications
You must be signed in to change notification settings - Fork 3
/
countryCRUD.go
87 lines (73 loc) · 1.81 KB
/
countryCRUD.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
package main
import (
"database/sql"
)
type country struct {
Id int `json:"id"`
Name string `json:"name"`
PopulationCount sql.NullInt64 `json:"population_count"`
Cities []city `json:"cities"`
}
func (c *country) create() error {
_, err := db.Exec("INSERT INTO countries (name) VALUES ($1)", c.Name)
return err
}
func (c *country) read() error {
return db.QueryRow(`
SELECT c.name
FROM countries AS c
LEFT
JOIN country_stats AS cs
ON cs.country_id = c.id
WHERE c.id = $1
`, &c.Id).Scan(&c.Name)
}
func (c *country) update() error {
_, err := db.Exec("UPDATE countries SET name = $2 WHERE id = $1", c.Id, c.Name)
return err
}
func (c *country) delete() error {
_, err := db.Exec("DELETE FROM countries WHERE id = $1", c.Id)
return err
}
func (c *country) stats() error {
return db.QueryRow(`
SELECT c.name
, cs.population_count
FROM countries AS c
LEFT
JOIN country_stats AS cs
ON cs.country_id = c.id
WHERE c.id = $1
`, &c.Id).Scan(&c.Name, &c.PopulationCount)
}
func (c *country) CitiesCount() (int, error) {
var count int
err := db.QueryRow("SELECT COUNT(*) FROM cities WHERE id = $1", &c.Id).Scan(&count)
return count, err
}
func (c *country) indexCities(per, page int) error {
offset := per * (page - 1)
err := db.QueryRow("SELECT name FROM countries WHERE id = $1", &c.Id).Scan(&c.Name)
if err != nil {
return err
}
rows, err := db.Query(`SELECT id, name FROM cities WHERE country_id = $1 LIMIT $2 OFFSET $3`, &c.Id, &per, &offset)
if err != nil {
return err
}
defer rows.Close()
var ct city
for rows.Next() {
err = rows.Scan(&ct.Id, &ct.Name)
if err != nil {
return err
}
c.Cities = append(c.Cities, ct)
}
err = rows.Err()
if err != nil {
return err
}
return nil
}