/
server.go
126 lines (111 loc) · 3.17 KB
/
server.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
package main
import (
"html/template"
"log"
"net/http"
"net/url"
"github.com/jmoiron/sqlx"
)
func server(dbURL string) http.Handler {
db := sqlx.MustConnect("postgres", dbURL)
t, err := loadTemplates()
if err != nil {
log.Fatal(err)
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
var departments, cities, districts []string
if err := db.Select(&departments, queries["select_departments.sql"]); err != nil {
log.Print(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := db.Select(&cities, queries["select_cities.sql"]); err != nil {
log.Print(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := db.Select(&districts, queries["select_districts.sql"]); err != nil {
log.Print(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
announces, err := selectAnnounces(db, q)
if err != nil {
log.Print(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := t.ExecuteTemplate(w, "index.html", struct {
Announces []Announce
Departments, Cities, Districts []string
Query url.Values
}{Query: q, Departments: departments, Cities: cities, Districts: districts, Announces: announces}); err != nil {
log.Print(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
}
func selectAnnounces(db *sqlx.DB, q url.Values) ([]Announce, error) {
departments := q["departments"]
cities := q["cities"]
districts := q["districts"]
var (
query string
args []interface{}
err error
)
// seven cases
// 1. departments + cities + districts
// 2. departments + cities
// 3. cities + districts
// 4. departments
// 5. cities
// 6. districts
// 7. all
switch {
case departments != nil && cities != nil && districts != nil:
query, args, err = sqlx.In(queries["select_where_department_or_city_or_district.sql"], departments, cities, districts)
case departments != nil && cities != nil:
query, args, err = sqlx.In(queries["select_where_department_or_city.sql"], departments, cities)
case cities != nil && districts != nil:
query, args, err = sqlx.In(queries["select_where_city_or_district.sql"], cities, districts)
case departments != nil:
query, args, err = sqlx.In(queries["select_where_department.sql"], departments)
case cities != nil:
query, args, err = sqlx.In(queries["select_where_city.sql"], cities)
case districts != nil:
query, args, err = sqlx.In(queries["select_where_district.sql"], districts)
default:
query = queries["select.sql"]
}
if err != nil {
return nil, err
}
var announces []Announce
if err := db.Select(&announces, db.Rebind(query), args...); err != nil {
return nil, err
}
return announces, nil
}
func loadTemplates() (*template.Template, error) {
t := template.New("").Funcs(
template.FuncMap{"contains": contains},
)
for k, v := range templates {
_, err := t.New(k).Parse(v)
if err != nil {
return nil, err
}
}
return t, nil
}
func contains(slice []string, str string) bool {
for _, s := range slice {
if str == s {
return true
}
}
return false
}