-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.go
61 lines (46 loc) · 1.52 KB
/
input.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
package api
import (
"encoding/json"
"net/http"
"github.com/jvikstedt/watchful/pkg/model"
)
func (h handler) inputCreate(w http.ResponseWriter, r *http.Request) (interface{}, int, error) {
decoder := json.NewDecoder(r.Body)
input := &model.Input{}
if err := decoder.Decode(input); err != nil {
return EmptyObject, http.StatusUnprocessableEntity, err
}
if err := input.Create(h.db); err != nil {
return EmptyObject, http.StatusUnprocessableEntity, err
}
return input, http.StatusCreated, nil
}
func (h handler) inputUpdate(w http.ResponseWriter, r *http.Request) (interface{}, int, error) {
inputID, err := h.getURLParamInt(r, "inputID")
if err != nil {
return EmptyObject, http.StatusUnprocessableEntity, err
}
input := &model.Input{}
if err := model.InputGetOne(h.db, inputID, input); err != nil {
return EmptyObject, http.StatusNotFound, err
}
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(input); err != nil {
return EmptyObject, http.StatusUnprocessableEntity, err
}
if err := input.Update(h.db); err != nil {
return EmptyObject, http.StatusUnprocessableEntity, err
}
return input, http.StatusOK, nil
}
func (h handler) inputDelete(w http.ResponseWriter, r *http.Request) (interface{}, int, error) {
inputID, err := h.getURLParamInt(r, "inputID")
if err != nil {
return EmptyObject, http.StatusUnprocessableEntity, err
}
input := &model.Input{ID: inputID}
if err := input.Delete(h.db); err != nil {
return EmptyObject, http.StatusNotFound, err
}
return input, http.StatusOK, nil
}