/
api_items.go
154 lines (136 loc) · 3.93 KB
/
api_items.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* Hello World Service
*
* Hello World service with support for multiple languages.
*
* API version: 1
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/
package openapi
import (
"encoding/json"
"net/http"
"strings"
"github.com/gorilla/mux"
)
// A ItemsApiController binds http requests to an api service and writes the service results to the http response
type ItemsApiController struct {
service ItemsApiServicer
}
// NewItemsApiController creates a default api controller
func NewItemsApiController(s ItemsApiServicer) Router {
return &ItemsApiController{ service: s }
}
// Routes returns all of the api route for the ItemsApiController
func (c *ItemsApiController) Routes() Routes {
return Routes{
{
"CreateItem",
strings.ToUpper("Post"),
"/helloworld/api/v1/items",
c.CreateItem,
"HELLOWORLD_WRITE_ITEM",
},
{
"DeleteItem",
strings.ToUpper("Delete"),
"/helloworld/api/v1/items/{id}",
c.DeleteItem,
"HELLOWORLD_WRITE_ITEM",
},
{
"GetItem",
strings.ToUpper("Get"),
"/helloworld/api/v1/items/{id}",
c.GetItem,
"HELLOWORLD_READ_ITEM",
},
{
"GetItems",
strings.ToUpper("Get"),
"/helloworld/api/v1/items",
c.GetItems,
"HELLOWORLD_READ_ITEM",
},
{
"UpdateItem",
strings.ToUpper("Put"),
"/helloworld/api/v1/items/{id}",
c.UpdateItem,
"HELLOWORLD_WRITE_ITEM",
},
}
}
// CreateItem - Creates a new Hello World item.
func (c *ItemsApiController) CreateItem(w http.ResponseWriter, r *http.Request) {
item := &Item{}
if err := json.NewDecoder(r.Body).Decode(&item); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
result, err := c.service.CreateItem(r.Context(), *item)
//If an error occured, encode the error with the status code
if err != nil {
EncodeJSONResponse(err.Error(), &result.Code, w)
return
}
//If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, w)
}
// DeleteItem - Deletes a Hello World item.
func (c *ItemsApiController) DeleteItem(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
id := params["id"]
result, err := c.service.DeleteItem(r.Context(), id)
//If an error occured, encode the error with the status code
if err != nil {
EncodeJSONResponse(err.Error(), &result.Code, w)
return
}
//If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, w)
}
// GetItem - Returns a Hello World item.
func (c *ItemsApiController) GetItem(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
id := params["id"]
result, err := c.service.GetItem(r.Context(), id)
//If an error occured, encode the error with the status code
if err != nil {
EncodeJSONResponse(err.Error(), &result.Code, w)
return
}
//If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, w)
}
// GetItems - Returns a list of Hello World items.
func (c *ItemsApiController) GetItems(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
languageId := query.Get("languageId")
result, err := c.service.GetItems(r.Context(), languageId)
//If an error occured, encode the error with the status code
if err != nil {
EncodeJSONResponse(err.Error(), &result.Code, w)
return
}
//If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, w)
}
// UpdateItem - Updates a Hello World item.
func (c *ItemsApiController) UpdateItem(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
id := params["id"]
item := &Item{}
if err := json.NewDecoder(r.Body).Decode(&item); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
result, err := c.service.UpdateItem(r.Context(), id, *item)
//If an error occured, encode the error with the status code
if err != nil {
EncodeJSONResponse(err.Error(), &result.Code, w)
return
}
//If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, w)
}