Skip to content

Commit e4e4669

Browse files
committed
chore(golang): implement logic for DELETE requests
Part of #9
1 parent d85d6f5 commit e4e4669

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

examples/go/routes.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import "encoding/json"
55
import "fmt"
66
import "net/http"
77
import "os"
8-
import "strconv"
98
import "github.com/go-chi/chi"
109
import "github.com/jmoiron/sqlx"
1110

@@ -35,7 +34,6 @@ type CategoryInfoDto struct {
3534
}
3635

3736
func registerRoutes(r chi.Router, db *sqlx.DB) {
38-
categories := make(map[int]CategoryDto)
3937

4038
r.Get("/v1/categories/count", func(w http.ResponseWriter, r *http.Request) {
4139
var result CounterDto
@@ -165,8 +163,19 @@ func registerRoutes(r chi.Router, db *sqlx.DB) {
165163
})
166164

167165
r.Delete("/v1/categories/{categoryId}", func(w http.ResponseWriter, r *http.Request) {
168-
id, _ := strconv.Atoi(chi.URLParam(r, "categoryId"))
169-
delete(categories, id)
166+
args := map[string]interface{}{
167+
"categoryId": chi.URLParam(r, "categoryId"),
168+
}
169+
_, err := db.NamedExec(
170+
"DELETE FROM categories WHERE id = :categoryId",
171+
args,
172+
)
173+
if err != nil {
174+
fmt.Fprintf(os.Stderr, "NamedExec failed: %v\n", err)
175+
w.WriteHeader(http.StatusInternalServerError)
176+
return
177+
}
178+
170179
w.WriteHeader(http.StatusNoContent)
171180
})
172181

src/templates/routes.go.ejs

+14-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import "encoding/json"
55
import "fmt"
66
import "net/http"
77
import "os"
8-
import "strconv"
98
import "github.com/go-chi/chi"
109
import "github.com/jmoiron/sqlx"
1110

@@ -169,12 +168,12 @@ endpoints.forEach(function(endpoint) {
169168
});
170169
-%>
171170
func registerRoutes(r chi.Router, db *sqlx.DB) {
172-
categories := make(map[int]CategoryDto)
173171
<%
174172
endpoints.forEach(function(endpoint) {
175173
const path = convertPathPlaceholders(endpoint.path);
176174
177175
endpoint.methods.forEach(function(method) {
176+
const params = extractParams(method.query);
178177
const hasGetOne = method.name === 'get';
179178
const hasGetMany = method.name === 'get_list';
180179
if (hasGetOne || hasGetMany) {
@@ -184,7 +183,6 @@ endpoints.forEach(function(endpoint) {
184183
const dtoName = dtoInCache(dto) ? dtoCache[cacheKey] : dto.name;
185184
const dataType = hasGetMany ? '[]' + dtoName : dtoName;
186185
187-
const params = extractParams(method.query);
188186
const queryFunction = hasGetOne ? 'Get' : 'Select';
189187
// TODO: handle only particular method (get/post/put)
190188
// TODO: include method/path into an error message
@@ -227,9 +225,7 @@ endpoints.forEach(function(endpoint) {
227225
// TODO: do we really need signature and cache?
228226
const cacheKey = dto ? dto.signature : null;
229227
const dataType = dtoInCache(dto) ? dtoCache[cacheKey] : dto.name;
230-
231228
// TODO: align args properly (like gofmt does)
232-
const params = extractParams(method.query);
233229
%>
234230
r.Post("<%- path %>", func(w http.ResponseWriter, r *http.Request) {
235231
var dto <%- dataType %>
@@ -257,9 +253,7 @@ endpoints.forEach(function(endpoint) {
257253
// TODO: do we really need signature and cache?
258254
const cacheKey = dto ? dto.signature : null;
259255
const dataType = dtoInCache(dto) ? dtoCache[cacheKey] : dto.name;
260-
261256
// TODO: align args properly (like gofmt does)
262-
const params = extractParams(method.query);
263257
%>
264258
r.Put("<%- path %>", func(w http.ResponseWriter, r *http.Request) {
265259
var dto <%- dataType %>
@@ -285,8 +279,19 @@ endpoints.forEach(function(endpoint) {
285279
if (method.name === 'delete') {
286280
%>
287281
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+
290295
w.WriteHeader(http.StatusNoContent)
291296
})
292297
<%

0 commit comments

Comments
 (0)