-
Notifications
You must be signed in to change notification settings - Fork 21
/
product.go
52 lines (44 loc) · 1.29 KB
/
product.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
package main
import(
"fmt"
"html/template"
"net/http"
"database/sql"
)
func products(w http.ResponseWriter, r *http.Request) {
session, _ := store.Get(r, "session-name")
tplValues := map[string]interface{}{"Header": "Products", "Copyright": "Roman Frołow"}
db, err := sql.Open("sqlite3", "file:./db/app.db?foreign_keys=true")
if err != nil {
serveError(w, err)
return
}
defer db.Close()
sql := "select title, description, price, quantity, filename from products order by title"
rows, err := db.Query(sql)
if err != nil {
fmt.Printf("%q: %s\n", err, sql)
serveError(w, err)
return
}
defer rows.Close()
levels := []map[string]string{}
var title, description, price, quantity, filename string
for rows.Next() {
rows.Scan(&title, &description, &price, &quantity, &filename)
levels = append(levels, map[string]string{"title": title, "description": description, "price": price, "quantity": quantity, "filename": filename})
}
tplValues["levels"] = levels
rows.Close()
pageTemplate, err := template.ParseFiles("tpl/products.html", "tpl/header.html", "tpl/bar.html", "tpl/footer.html")
if err != nil {
serveError(w, err)
}
if i, ok := session.Values["login"]; ok {
tplValues["login"] = i
}
err = pageTemplate.Execute(w, tplValues)
if err != nil {
serveError(w, err)
}
}