/
Handlers.go
75 lines (66 loc) · 1.92 KB
/
Handlers.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
package main
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"net/http"
"strconv"
"github.com/flosch/pongo2"
)
var tpl = pongo2.Must(pongo2.FromFile("views/home.html"))
func (repo *Repo) Home(w http.ResponseWriter, r *http.Request) {
err := tpl.ExecuteWriter(pongo2.Context{"query": r.FormValue("query")}, w)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
var tplItem = pongo2.Must(pongo2.FromFile("views/item.html"))
func (repo *Repo) Item(w http.ResponseWriter, r *http.Request){
err := tplItem.ExecuteWriter(pongo2.Context{"Items": r.FormValue("items")}, w)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func (repo *Repo) Index(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Welcome to my go api!")
}
func (repo *Repo) ItemIndex(w http.ResponseWriter, r *http.Request) {
items := repo.database.Items()
js, err := json.Marshal(items)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
func (repo *Repo) ItemCreate(w http.ResponseWriter, r *http.Request) {
item := &Item{}
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&item); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
item.Id = repo.database.NewItemId()
repo.database.SaveItem(item)
}
func (repo *Repo) ItemShow(w http.ResponseWriter, r *http.Request) {
sid := mux.Vars(r)["id"]
id, err := strconv.ParseUint(sid, 10, 64)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
item := repo.database.Item(id)
if item == nil {
http.Error(w, "Not Found", http.StatusNotFound)
return
}
js, err := json.Marshal(item)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}