/
api_designs.go
177 lines (155 loc) · 4.98 KB
/
api_designs.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2022 Cisco Systems, Inc. and its affiliates
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
/*
* Flame REST API
*
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* API version: 1.0.0
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/
package openapi
import (
"encoding/json"
"net/http"
"strings"
"github.com/gorilla/mux"
"github.com/cisco-open/flame/pkg/openapi/constants"
)
// DesignsApiController binds http requests to an api service and writes the service results to the http response
type DesignsApiController struct {
service DesignsApiServicer
errorHandler ErrorHandler
}
// DesignsApiOption for how the controller is set up.
type DesignsApiOption func(*DesignsApiController)
// WithDesignsApiErrorHandler inject ErrorHandler into controller
func WithDesignsApiErrorHandler(h ErrorHandler) DesignsApiOption {
return func(c *DesignsApiController) {
c.errorHandler = h
}
}
// NewDesignsApiController creates a default api controller
func NewDesignsApiController(s DesignsApiServicer, opts ...DesignsApiOption) Router {
controller := &DesignsApiController{
service: s,
errorHandler: DefaultErrorHandler,
}
for _, opt := range opts {
opt(controller)
}
return controller
}
// Routes returns all the api routes for the DesignsApiController
func (c *DesignsApiController) Routes() Routes {
return Routes{
{
"CreateDesign",
strings.ToUpper("Post"),
"/users/{user}/designs",
c.CreateDesign,
},
{
"DeleteDesign",
strings.ToUpper("Delete"),
"/users/{user}/designs/{designId}",
c.DeleteDesign,
},
{
"GetDesign",
strings.ToUpper("Get"),
"/users/{user}/designs/{designId}",
c.GetDesign,
},
{
"GetDesigns",
strings.ToUpper("Get"),
"/users/{user}/designs",
c.GetDesigns,
},
}
}
// CreateDesign - Create a new design template.
func (c *DesignsApiController) CreateDesign(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
userParam := params[constants.ParamUser]
designInfoParam := DesignInfo{}
d := json.NewDecoder(r.Body)
d.DisallowUnknownFields()
if err := d.Decode(&designInfoParam); err != nil {
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
return
}
if err := AssertDesignInfoRequired(designInfoParam); err != nil {
c.errorHandler(w, r, err, nil)
return
}
result, err := c.service.CreateDesign(r.Context(), userParam, designInfoParam)
// If an error occurred, encode the error with the status code
if err != nil {
c.errorHandler(w, r, err, &result)
return
}
// If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, w)
}
// DeleteDesign - Delete design template
func (c *DesignsApiController) DeleteDesign(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
userParam := params[constants.ParamUser]
designIdParam := params[constants.ParamDesignID]
result, err := c.service.DeleteDesign(r.Context(), userParam, designIdParam)
// If an error occurred, encode the error with the status code
if err != nil {
c.errorHandler(w, r, err, &result)
return
}
// If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, w)
}
// GetDesign - Get design template information
func (c *DesignsApiController) GetDesign(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
userParam := params[constants.ParamUser]
designIdParam := params[constants.ParamDesignID]
result, err := c.service.GetDesign(r.Context(), userParam, designIdParam)
// If an error occurred, encode the error with the status code
if err != nil {
c.errorHandler(w, r, err, &result)
return
}
// If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, w)
}
// GetDesigns - Get list of all the designs created by the user.
func (c *DesignsApiController) GetDesigns(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
query := r.URL.Query()
userParam := params[constants.ParamUser]
limitParam, err := parseInt32Parameter(query.Get(constants.ParamLimit), false)
if err != nil {
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
return
}
result, err := c.service.GetDesigns(r.Context(), userParam, limitParam)
// If an error occurred, encode the error with the status code
if err != nil {
c.errorHandler(w, r, err, &result)
return
}
// If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, w)
}