Skip to content

Commit 6547d71

Browse files
committed
feat(python): implement generation of PUT endpoints
Part of #16
1 parent 08a9909 commit 6547d71

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

examples/python/fastapi/postgres/routes.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,22 @@ def get_v1_categories_category_id(categoryId, conn=Depends(db_connection)):
144144

145145

146146
@router.put('/v1/categories/{categoryId}', status_code = status.HTTP_204_NO_CONTENT)
147-
def put_v1_categories_category_id():
148-
pass
147+
def put_v1_categories_category_id(body: CreateCategoryDto, categoryId, conn=Depends(db_connection)):
148+
try:
149+
with conn:
150+
with conn.cursor() as cur:
151+
cur.execute(
152+
"""
153+
UPDATE categories
154+
SET name = %(name)s
155+
, name_ru = %(name_ru)s
156+
, slug = %(slug)s
157+
, updated_at = NOW()
158+
, updated_by = %(user_id)s
159+
WHERE id = %(categoryId)s
160+
""", {"name": body.name, "name_ru": body.name_ru, "slug": body.slug, "user_id": body.user_id, "categoryId": categoryId})
161+
finally:
162+
conn.close()
149163

150164

151165
@router.delete('/v1/categories/{categoryId}', status_code = status.HTTP_204_NO_CONTENT)

src/templates/routes.py.ejs

+38-2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ function formatQueryForPython(query, indentLevel) {
4444
return `"${sql}"`
4545
}
4646
47+
// LATER: reduce duplication with routes.go.ejs
4748
// {'values':
4849
// [
4950
// {
@@ -59,11 +60,30 @@ function extractInsertValues(queryAst) {
5960
return Array.from(new Set(values));
6061
}
6162
63+
// LATER: reduce duplication with routes.go.ejs
64+
// {'set':
65+
// [
66+
// {
67+
// column: 'updated_by',
68+
// value: { type: 'param', value: 'user_id' },
69+
// table: null
70+
// }
71+
// ]
72+
// } => [ 'user_id' ]
73+
function extractUpdateValues(queryAst) {
74+
// LATER: distinguish between b.param and q.param and extract only first
75+
return queryAst.set.map(elem => elem.value.type === 'param' ? elem.value.value : null)
76+
.filter(value => value) // filter out nulls
77+
}
78+
6279
// LATER: reduce duplication with routes.go.ejs
6380
function extractProperties(queryAst) {
6481
if (queryAst.type === 'insert') {
6582
return extractInsertValues(queryAst);
6683
}
84+
if (queryAst.type === 'update') {
85+
return extractUpdateValues(queryAst);
86+
}
6787
return [];
6888
}
6989
@@ -259,11 +279,27 @@ def <%- pythonMethodName %>(body: <%- model %>, conn=Depends(db_connection)):
259279
260280
}
261281
if (method.name === 'put') {
282+
// TODO: reduce duplication with POST
283+
const dto = query2dto(sqlParser, method);
284+
// LATER: do we really need signature and cache?
285+
const cacheKey = dto ? dto.signature : null;
286+
const model = dtoInCache(dto) ? dtoCache[cacheKey] : dto.name;
287+
288+
const methodArgs = [`body: ${model}`, ...argsFromPath, 'conn=Depends(db_connection)']
289+
290+
const sql = convertToPsycopgNamedArguments(formatQueryForPython(method.query, 20))
291+
const params = extractParamsFromQuery(method.query);
292+
const formattedParams = formatParamsAsPythonDict(params)
262293
%>
263294
264295
@router.put('<%- path %>', status_code = status.HTTP_204_NO_CONTENT)
265-
def <%- pythonMethodName %>():
266-
pass
296+
def <%- pythonMethodName %>(<%- methodArgs.join(', ') %>):
297+
try:
298+
with conn:
299+
with conn.cursor() as cur:
300+
cur.execute(<%- sql %><%- formattedParams %>)
301+
finally:
302+
conn.close()
267303
<%
268304
269305
}

0 commit comments

Comments
 (0)