/
config.go
97 lines (90 loc) · 2.05 KB
/
config.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package routing
import (
"encoding/json"
"io/ioutil"
"net/http"
"github.com/3devo/feconnector/models"
"github.com/3devo/feconnector/routing/responses"
"github.com/3devo/feconnector/utils"
"github.com/julienschmidt/httprouter"
)
// swagger:route GET /Configs/{uuid} Configs GetConfig
//
// Handler to retrieve the config
//
// This will the config data
//
// Produces:
// application/json
//
// Responses:
// 200: ConfigResponse
// 404: ResourceStatusResponse
func GetConfig(env *utils.Env) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
config := models.Config{}
if err := env.Db.One("ID", 1, &config); err != nil {
responses.WriteResourceStatusResponse(
http.StatusNotFound,
"Configs",
"GET",
err.Error(),
w)
return
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(&config)
}
}
// swagger:route PUT /Config/{uuid} Config UpdateConfig
//
// Handler to update an existing Config object
//
// This will update an existing Config object
//
// Produces:
// application/json
//
// Responses:
// 200: ResourceStatusResponse
func UpdateConfig(env *utils.Env) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
validation := responses.ConfigCreationBody{}
body, _ := ioutil.ReadAll(r.Body)
json.Unmarshal(body, &validation.Data)
if err := env.Validator.Struct(validation); err != nil {
responses.WriteResourceStatusResponse(
http.StatusInternalServerError,
"Config",
"UPDATE",
err.Error(),
w)
return
}
if err := env.Db.One("ID", 1, &models.Config{}); err != nil {
responses.WriteResourceStatusResponse(
http.StatusNotFound,
"Config",
"UPDATE",
err.Error(),
w)
return
}
if err := env.Db.Save(&validation.Data); err != nil {
responses.WriteResourceStatusResponse(
http.StatusConflict,
"Config",
"UPDATE",
err.Error(),
w)
} else {
responses.WriteResourceStatusResponse(
http.StatusOK,
"Config",
"UPDATE",
"",
w)
return
}
}
}