-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest.go
80 lines (65 loc) · 1.52 KB
/
rest.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
package main
import (
"net/http"
"svar-widgets/grid-backend-go/data"
"github.com/go-chi/chi"
)
func initRoutes(r chi.Router, dao *data.DAO) {
r.Get("/films", func(w http.ResponseWriter, r *http.Request) {
data, err := dao.Films.GetAll()
if err != nil {
format.Text(w, 500, err.Error())
} else {
format.JSON(w, 200, data)
}
})
r.Post("/films", func(w http.ResponseWriter, r *http.Request) {
upd := data.FilmUpdate{}
var row *data.Film
err := ParseForm(w, r, &upd)
if err == nil {
row, err = dao.Films.Add(upd)
}
if err != nil {
format.Text(w, 500, err.Error())
} else {
format.JSON(w, 200, row)
}
})
r.Put("/films/{id}", func(w http.ResponseWriter, r *http.Request) {
upd := data.FilmUpdate{}
var row *data.Film
id := NumberParam(r, "id")
err := ParseForm(w, r, &upd)
if err == nil {
row, err = dao.Films.Update(id, upd)
}
if err != nil {
format.Text(w, 500, err.Error())
} else {
format.JSON(w, 200, row)
}
})
r.Patch("/films/{id}", func(w http.ResponseWriter, r *http.Request) {
p := data.Patch{}
id := NumberParam(r, "id")
err := ParseForm(w, r, &p)
if err == nil {
err = dao.Films.Patch(id, p)
}
if err != nil {
format.Text(w, 500, err.Error())
} else {
format.JSON(w, 200, Response{id})
}
})
r.Delete("/films/{id}", func(w http.ResponseWriter, r *http.Request) {
id := NumberParam(r, "id")
err := dao.Films.Delete(id)
if err != nil {
format.Text(w, 500, err.Error())
} else {
format.JSON(w, 200, Response{id})
}
})
}