/
sources.go
99 lines (86 loc) · 2.98 KB
/
sources.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
98
99
package controllers
import (
"encoding/json"
"net/http"
"strconv"
"github.com/gorilla/mux"
"github.com/squeakycheese75/service-dictionary-go/api/data"
"github.com/squeakycheese75/service-dictionary-go/api/env"
"github.com/squeakycheese75/service-dictionary-go/api/utils"
)
func GetSources(env *env.Env) func(http.ResponseWriter, *http.Request) {
return func(res http.ResponseWriter, req *http.Request) {
var sources []data.Source
if result := env.DB.Find(&sources); result.Error != nil {
utils.RespondWithError(res, http.StatusInternalServerError, result.Error.Error())
return
}
utils.RespondWithJSON(res, http.StatusOK, sources)
}
}
func GetSource(env *env.Env) func(http.ResponseWriter, *http.Request) {
return func(res http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
id := vars["id"]
var source data.Source
if result := env.DB.First(&source, id); result.Error != nil {
utils.RespondWithError(res, http.StatusNotFound, result.Error.Error())
return
}
utils.RespondWithJSON(res, http.StatusOK, source)
}
}
func CreateSource(env *env.Env) func(http.ResponseWriter, *http.Request) {
return func(res http.ResponseWriter, req *http.Request) {
var source data.Source
decoder := json.NewDecoder(req.Body)
if err := decoder.Decode(&source); err != nil {
utils.RespondWithError(res, http.StatusBadRequest, "Invalid request payload")
return
}
defer req.Body.Close()
if result := env.DB.Create(&data.Source{Name: source.Name, Desc: source.Desc, Endpoint: source.Endpoint, SourceTypeID: source.SourceTypeID}); result.Error != nil {
utils.RespondWithError(res, http.StatusInternalServerError, result.Error.Error())
return
}
utils.RespondWithJSON(res, http.StatusCreated, source)
}
}
func UpdateSource(env *env.Env) func(http.ResponseWriter, *http.Request) {
return func(res http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
id, err := strconv.Atoi(vars["id"])
if err != nil {
utils.RespondWithError(res, http.StatusBadRequest, "Invalid source ID")
return
}
var s data.Source
decoder := json.NewDecoder(req.Body)
if err := decoder.Decode(&s); err != nil {
utils.RespondWithError(res, http.StatusBadRequest, "Invalid request payload")
return
}
defer req.Body.Close()
s.ID = uint(id)
if err := env.DB.Save(&s).Error; err != nil {
utils.RespondWithError(res, http.StatusInternalServerError, err.Error())
return
}
utils.RespondWithJSON(res, http.StatusOK, s)
}
}
func DeleteSource(env *env.Env) func(http.ResponseWriter, *http.Request) {
return func(res http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
id, err := strconv.Atoi(vars["id"])
if err != nil {
utils.RespondWithError(res, http.StatusBadRequest, "Invalid Source ID")
return
}
if err := env.DB.Delete(&data.Source{}, id).Error; err != nil {
utils.RespondWithError(res, http.StatusInternalServerError, err.Error())
return
}
utils.RespondWithJSON(res, http.StatusOK, map[string]string{"result": "success"})
}
}