Skip to content

Commit 2870482

Browse files
committed
chore(golang): implement logic for PUT requests
Part of #9
1 parent 6a02b2b commit 2870482

File tree

3 files changed

+51
-12
lines changed

3 files changed

+51
-12
lines changed

examples/go/routes.go

+20-4
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,26 @@ func registerRoutes(r chi.Router, db *sqlx.DB) {
141141
})
142142

143143
r.Put("/v1/categories/{categoryId}", func(w http.ResponseWriter, r *http.Request) {
144-
id, _ := strconv.Atoi(chi.URLParam(r, "categoryId"))
145-
var category CategoryDto
146-
json.NewDecoder(r.Body).Decode(&category)
147-
categories[id] = category
144+
var dto CreateCategoryDto
145+
json.NewDecoder(r.Body).Decode(&dto)
146+
147+
args := map[string]interface{}{
148+
"name": dto.Name,
149+
"name_ru": dto.NameRu,
150+
"slug": dto.Slug,
151+
"user_id": dto.UserId,
152+
"categoryId": chi.URLParam(r, "categoryId"),
153+
}
154+
_, err := db.NamedExec(
155+
"UPDATE categories SET name = :name , name_ru = :name_ru , slug = :slug , updated_at = NOW() , updated_by = :user_id WHERE id = :categoryId",
156+
args,
157+
)
158+
if err != nil {
159+
fmt.Fprintf(os.Stderr, "NamedExec failed: %v\n", err)
160+
w.WriteHeader(http.StatusInternalServerError)
161+
return
162+
}
163+
148164
w.WriteHeader(http.StatusNoContent)
149165
})
150166

src/cli.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,12 @@ const createEndpoints = async (destDir, lang, config) => {
118118
'b': 'req.body',
119119
},
120120
'go': {
121-
'p': 'dto',
122-
'b': 'dto',
121+
'p': function(param) {
122+
return `chi.URLParam(r, "${param}")`
123+
},
124+
'b': function(param) {
125+
return 'dto.' + capitalize(snake2camelCase(param));
126+
},
123127
}
124128
}
125129

@@ -165,8 +169,8 @@ const createEndpoints = async (destDir, lang, config) => {
165169
p => {
166170
const bindTarget = p.substring(0, 1);
167171
const paramName = p.substring(2);
168-
const fieldName = capitalize(snake2camelCase(paramName));
169-
return `"${paramName}": ${placeholdersMap['go'][bindTarget]}.${fieldName},`
172+
const formatFunc = placeholdersMap['go'][bindTarget];
173+
return `"${paramName}": ${formatFunc(paramName)},`
170174
}
171175
).join('\n\t\t\t');
172176
},

src/templates/routes.go.ejs

+23-4
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,31 @@ endpoints.forEach(function(endpoint) {
253253
<%
254254
}
255255
if (method.name === 'put') {
256+
const dto = query2dto(sqlParser, method);
257+
// TODO: do we really need signature and cache?
258+
const cacheKey = dto ? dto.signature : null;
259+
const dataType = dtoInCache(dto) ? dtoCache[cacheKey] : dto.name;
260+
261+
// TODO: align args properly (like gofmt does)
262+
const params = extractParams(method.query);
256263
%>
257264
r.Put("<%- path %>", func(w http.ResponseWriter, r *http.Request) {
258-
id, _ := strconv.Atoi(chi.URLParam(r, "categoryId"))
259-
var category CategoryDto
260-
json.NewDecoder(r.Body).Decode(&category)
261-
categories[id] = category
265+
var dto <%- dataType %>
266+
json.NewDecoder(r.Body).Decode(&dto)
267+
268+
args := map[string]interface{}{
269+
<%- formatParamsAsGolangMap(params) %>
270+
}
271+
_, err := db.NamedExec(
272+
"<%- formatQuery(method.query) %>",
273+
args,
274+
)
275+
if err != nil {
276+
fmt.Fprintf(os.Stderr, "NamedExec failed: %v\n", err)
277+
w.WriteHeader(http.StatusInternalServerError)
278+
return
279+
}
280+
262281
w.WriteHeader(http.StatusNoContent)
263282
})
264283
<%

0 commit comments

Comments
 (0)