-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
170 lines (142 loc) · 3.63 KB
/
main.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
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/jackc/pgx/v4/pgxpool"
)
type Pharmacy struct {
Code string
Name string
AddrLine1 string
AddrLine2 string
AddrLine3 string
AddrLine4 string
Postcode string
LatLng LatLng
}
type LatLng struct {
Lat float32
Lng float32
}
type PharmacyRepo interface {
FindByCode(code string) (*Pharmacy, error)
FindByPostcode(partial string) ([]*Pharmacy, error)
Insert(p Pharmacy) error
}
type PSQLPharmacyRepo struct {
Conn *pgxpool.Pool
PharmacyRepo
}
func (r PSQLPharmacyRepo) FindByCode(code string) (*Pharmacy, error) {
sql := `select code, name, addr_line_1, addr_line_2, addr_line_3, addr_line_4, postcode
from pharmacy where code = $1`
p := &Pharmacy{}
row := r.Conn.QueryRow(context.Background(), sql, code)
err := row.Scan(&p.Code, &p.Name, &p.AddrLine1, &p.AddrLine2, &p.AddrLine3, &p.AddrLine4, &p.Postcode)
if err != nil {
return nil, err
}
return p, nil
}
func (r PSQLPharmacyRepo) FindByPostcode(postcode string) ([]*Pharmacy, error) {
sql := `select code, name, addr_line_1, addr_line_2, addr_line_3, addr_line_4, postcode
from pharmacy where postcode like concat($1::text, '%')`
rows, err := r.Conn.Query(context.Background(), sql, postcode)
if err != nil {
return nil, err
}
defer rows.Close()
pharmacies := make([]*Pharmacy, 0)
for rows.Next() {
p := &Pharmacy{}
err := rows.Scan(&p.Code, &p.Name, &p.AddrLine1, &p.AddrLine2, &p.AddrLine3, &p.AddrLine4, &p.Postcode)
if err != nil {
return nil, err
}
pharmacies = append(pharmacies, p)
}
return pharmacies, nil
}
func (r PSQLPharmacyRepo) Insert(p Pharmacy) error {
sql := `insert into pharmacy(code, name, addr_line_1, addr_line_2, addr_line_3, addr_line_4,
postcode, lat, lng) values ($1, $2, $3, $4, $5, $6, $7, $8, $9)`
_, err := r.Conn.Exec(
context.Background(),
sql,
p.Code,
p.Name,
p.AddrLine1,
p.AddrLine2,
p.AddrLine3,
p.AddrLine4,
p.Postcode,
p.LatLng.Lat,
p.LatLng.Lng,
)
if err != nil {
return err
}
return nil
}
func main() {
pool, err := openConnPool()
if err != nil {
log.Fatalf("could not get DB connection %s\n", err)
}
defer pool.Close()
repo := PSQLPharmacyRepo{
Conn: pool,
}
// find single
code := "FA512"
p, err := repo.FindByCode(code)
if err != nil {
log.Fatalf("could not find pharmacy with code %s %v", code, err)
}
log.Println(p.Code, p.Name, p.Postcode)
// find multiple
postcode := "LE"
pharmacies, err := repo.FindByPostcode("LE")
if err != nil {
log.Fatalf("could not find pharmacies with postcode %s %v", postcode, err)
}
log.Println(len(pharmacies))
log.Println(pharmacies[0].Code, pharmacies[0].Name, pharmacies[0].Postcode)
log.Println(pharmacies[1].Code, pharmacies[1].Name, pharmacies[1].Postcode)
pharmacy := Pharmacy{
Code: "NEW1",
Name: "A test pharmacy",
AddrLine1: "line1",
AddrLine2: "line2",
AddrLine3: "line3",
AddrLine4: "line4",
Postcode: "postccode",
LatLng: LatLng{Lat: 1.1, Lng: 2.2},
}
// will error on second run due to PK violation :)
err = repo.Insert(pharmacy)
if err != nil {
log.Fatalf("could not insert pharmacy %v", err)
}
}
func openConnPool() (*pgxpool.Pool, error) {
url := getEnvOrDefault("DB_URL", "postgres://root:password@localhost:5432/pharmacy")
pool, err := pgxpool.Connect(context.Background(), url)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
os.Exit(1)
}
err = pool.Ping(context.Background())
if err != nil {
return nil, err
}
return pool, nil
}
func getEnvOrDefault(key string, defValue string) string {
if value := os.Getenv(key); value != "" {
return value
}
return defValue
}