@@ -5,7 +5,6 @@ import "encoding/json"
5
5
import "fmt"
6
6
import "net/http"
7
7
import "os"
8
- import "strconv"
9
8
import "github.com/go-chi/chi"
10
9
import "github.com/jmoiron/sqlx"
11
10
@@ -169,12 +168,12 @@ endpoints.forEach(function(endpoint) {
169
168
});
170
169
- %>
171
170
func registerRoutes(r chi.Router, db *sqlx.DB) {
172
- categories := make(map[int]CategoryDto)
173
171
<%
174
172
endpoints .forEach (function (endpoint ) {
175
173
const path = convertPathPlaceholders (endpoint .path );
176
174
177
175
endpoint .methods .forEach (function (method ) {
176
+ const params = extractParams (method .query );
178
177
const hasGetOne = method .name === ' get' ;
179
178
const hasGetMany = method .name === ' get_list' ;
180
179
if (hasGetOne || hasGetMany) {
@@ -184,7 +183,6 @@ endpoints.forEach(function(endpoint) {
184
183
const dtoName = dtoInCache (dto) ? dtoCache[cacheKey] : dto .name ;
185
184
const dataType = hasGetMany ? ' []' + dtoName : dtoName;
186
185
187
- const params = extractParams (method .query );
188
186
const queryFunction = hasGetOne ? ' Get' : ' Select' ;
189
187
// TODO: handle only particular method (get/post/put)
190
188
// TODO: include method/path into an error message
@@ -227,9 +225,7 @@ endpoints.forEach(function(endpoint) {
227
225
// TODO: do we really need signature and cache?
228
226
const cacheKey = dto ? dto .signature : null ;
229
227
const dataType = dtoInCache (dto) ? dtoCache[cacheKey] : dto .name ;
230
-
231
228
// TODO: align args properly (like gofmt does)
232
- const params = extractParams (method .query );
233
229
% >
234
230
r .Post (" <%- path %>" , func (w http .ResponseWriter , r * http .Request ) {
235
231
var dto < %- dataType % >
@@ -257,9 +253,7 @@ endpoints.forEach(function(endpoint) {
257
253
// TODO: do we really need signature and cache?
258
254
const cacheKey = dto ? dto .signature : null ;
259
255
const dataType = dtoInCache (dto) ? dtoCache[cacheKey] : dto .name ;
260
-
261
256
// TODO: align args properly (like gofmt does)
262
- const params = extractParams (method .query );
263
257
% >
264
258
r .Put (" <%- path %>" , func (w http .ResponseWriter , r * http .Request ) {
265
259
var dto < %- dataType % >
@@ -285,8 +279,19 @@ endpoints.forEach(function(endpoint) {
285
279
if (method .name === ' delete' ) {
286
280
% >
287
281
r .Delete (" <%- path %>" , func (w http .ResponseWriter , r * http .Request ) {
288
- id, _ := strconv .Atoi (chi .URLParam (r, " categoryId" ))
289
- delete (categories, id)
282
+ args := map[string]interface {}{
283
+ < %- formatParamsAsGolangMap (params) % >
284
+ }
285
+ _, err := db .NamedExec (
286
+ " <%- formatQuery(method.query) %>" ,
287
+ args,
288
+ )
289
+ if err != nil {
290
+ fmt .Fprintf (os .Stderr , " NamedExec failed: %v\n " , err)
291
+ w .WriteHeader (http .StatusInternalServerError )
292
+ return
293
+ }
294
+
290
295
w .WriteHeader (http .StatusNoContent )
291
296
})
292
297
< %
0 commit comments