@@ -9,13 +9,6 @@ import "strconv"
9
9
import "github.com/go-chi/chi"
10
10
import "github.com/jmoiron/sqlx"
11
11
12
- type Category struct {
13
- Id int `json:"id" db:"id"`
14
- Name string `json:"name" db:"name"`
15
- NameRu *string `json:"name_ru" db:"name_ru"`
16
- Slug string `json:"slug" db:"slug"`
17
- }
18
-
19
12
<%
20
13
// {'columns':
21
14
// [
@@ -87,8 +80,8 @@ function addTypes(props) {
87
80
});
88
81
}
89
82
90
- function query2dto (parser , query ) {
91
- query = removePlaceholders (query);
83
+ function query2dto (parser , method ) {
84
+ const query = removePlaceholders (method . query );
92
85
const queryAst = parser .astify (query);
93
86
const props = extractProperties (queryAst);
94
87
if (props .length === 0 ) {
@@ -98,9 +91,10 @@ function query2dto(parser, query) {
98
91
return null ;
99
92
}
100
93
const propsWithTypes = addTypes (props);
94
+ const hasName = method .dto && method .dto .name && method .dto .name .length > 0 ;
95
+ const name = hasName ? method .dto .name : " Dto" + ++ globalDtoCounter;
101
96
return {
102
- // TODO: assign DTO name dynamically
103
- " name" : " Dto" + ++ globalDtoCounter,
97
+ " name" : name,
104
98
" props" : propsWithTypes,
105
99
// max length is needed for proper formatting
106
100
" maxFieldNameLength" : lengthOfLongestString (props),
@@ -159,8 +153,7 @@ const verbs_with_dto = [ 'get', 'post', 'put' ]
159
153
endpoints .forEach (function (endpoint ) {
160
154
const dtos = endpoint .methods
161
155
.filter (method => verbs_with_dto .includes (method .verb ))
162
- .map (method => method .query )
163
- .map (query => query2dto (sqlParser, query))
156
+ .map (method => query2dto (sqlParser, method))
164
157
.filter (elem => elem) // filter out nulls
165
158
.filter (dto => ! dtoInCache (dto))
166
159
.map (dto => dto2struct (cacheDto (dto)))
@@ -172,7 +165,7 @@ endpoints.forEach(function(endpoint) {
172
165
});
173
166
- %>
174
167
func registerRoutes(r chi.Router, db *sqlx.DB) {
175
- categories := make(map[int]Category )
168
+ categories := make(map[int]CategoryDto )
176
169
cnt := 0
177
170
<%
178
171
endpoints .forEach (function (endpoint ) {
@@ -182,7 +175,7 @@ endpoints.forEach(function(endpoint) {
182
175
const hasGetOne = method .name === ' get' ;
183
176
const hasGetMany = method .name === ' get_list' ;
184
177
if (hasGetOne || hasGetMany) {
185
- const dto = query2dto (sqlParser, method . query );
178
+ const dto = query2dto (sqlParser, method);
186
179
// TODO: do we really need signature and cache?
187
180
const cacheKey = dto ? dto .signature : null ;
188
181
const dataType = hasGetMany ? ' []' + dtoCache[cacheKey] : dtoCache[cacheKey];
@@ -229,10 +222,11 @@ endpoints.forEach(function(endpoint) {
229
222
if (method .name === ' post' ) {
230
223
% >
231
224
r .Post (" <%- path %>" , func (w http .ResponseWriter , r * http .Request ) {
232
- var category Category
225
+ var category CategoryDto
233
226
json .NewDecoder (r .Body ).Decode (& category)
234
227
cnt += 1
235
- category .Id = cnt
228
+ id := strconv .Itoa (cnt)
229
+ category .Id = & id
236
230
categories[cnt] = category
237
231
w .WriteHeader (http .StatusNoContent )
238
232
})
@@ -242,7 +236,7 @@ endpoints.forEach(function(endpoint) {
242
236
% >
243
237
r .Put (" <%- path %>" , func (w http .ResponseWriter , r * http .Request ) {
244
238
id, _ := strconv .Atoi (chi .URLParam (r, " categoryId" ))
245
- var category Category
239
+ var category CategoryDto
246
240
json .NewDecoder (r .Body ).Decode (& category)
247
241
categories[id] = category
248
242
w .WriteHeader (http .StatusNoContent )
0 commit comments