-
Notifications
You must be signed in to change notification settings - Fork 0
/
sirenType.go
59 lines (52 loc) · 1.73 KB
/
sirenType.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
package main
import (
"encoding/json"
"net/http"
"github.com/go-chi/chi"
"github.com/go-chi/render"
"github.com/serbe/edc"
)
func listSirenTypes(w http.ResponseWriter, r *http.Request) {
type context struct {
Title string `json:"title"`
SirenTypes []edc.SirenTypeList `json:"siren_types"`
}
sirenTypes, err := db.GetSirenTypeListAll()
if err != nil {
errmsg("listSirenTypes GetSirenTypeListAll", err)
return
}
ctx := context{Title: "List", SirenTypes: sirenTypes}
render.DefaultResponder(w, r, ctx)
}
func getSirenType(w http.ResponseWriter, r *http.Request) {
type context struct {
Title string `json:"title"`
SirenType edc.SirenType `json:"siren_type"`
}
id := toInt(chi.URLParam(r, "id"))
sirenType, err := db.GetSirenType(id)
if err != nil {
errmsg("getSirenType GetSirenType", err)
return
}
ctx := context{Title: "Create siren type", SirenType: sirenType}
render.DefaultResponder(w, r, ctx)
}
func createSirenType(_ http.ResponseWriter, r *http.Request) {
var sirenType edc.SirenType
errchkmsg("createSirenType Decode", json.NewDecoder(r.Body).Decode(&sirenType))
_, err := db.CreateSirenType(sirenType)
errchkmsg("createSirenType CreateSirenType", err)
errchkmsg("createSirenType CloseBody", r.Body.Close())
}
func updateSirenType(_ http.ResponseWriter, r *http.Request) {
var sirenType edc.SirenType
errchkmsg("updateSirenType Decode", json.NewDecoder(r.Body).Decode(&sirenType))
errchkmsg("updateSirenType UpdateSirenType", db.UpdateSirenType(sirenType))
errchkmsg("updateSirenType CloseBody", r.Body.Close())
}
func deleteSirenType(_ http.ResponseWriter, r *http.Request) {
id := toInt(chi.URLParam(r, "id"))
errchkmsg("deleteSirenType DeleteSirenType", db.DeleteSirenType(id))
}