Skip to content

Commit cb5b941

Browse files
committed
feat(python): implement generation of DELETE endpoints
Part of #16
1 parent 2756738 commit cb5b941

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

examples/python/fastapi/postgres/routes.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -163,5 +163,15 @@ def put_v1_categories_category_id(body: CreateCategoryDto, categoryId, conn=Depe
163163

164164

165165
@router.delete('/v1/categories/{categoryId}', status_code=status.HTTP_204_NO_CONTENT)
166-
def delete_v1_categories_category_id():
167-
pass
166+
def delete_v1_categories_category_id(categoryId, conn=Depends(db_connection)):
167+
try:
168+
with conn:
169+
with conn.cursor() as cur:
170+
cur.execute(
171+
"""
172+
DELETE
173+
FROM categories
174+
WHERE id = %(categoryId)s
175+
""", {"categoryId": categoryId})
176+
finally:
177+
conn.close()

src/templates/routes.py.ejs

+12-2
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,21 @@ def <%- pythonMethodName %>(<%- methodArgs.join(', ') %>):
304304
305305
}
306306
if (method.name === 'delete') {
307+
const methodArgs = [ ...argsFromPath, 'conn=Depends(db_connection)' ]
308+
309+
const sql = convertToPsycopgNamedArguments(formatQueryForPython(method.query, 20))
310+
const params = extractParamsFromQuery(method.query)
311+
const formattedParams = formatParamsAsPythonDict(params)
307312
%>
308313
309314
@router.delete('<%- path %>', status_code=status.HTTP_204_NO_CONTENT)
310-
def <%- pythonMethodName %>():
311-
pass
315+
def <%- pythonMethodName %>(<%- methodArgs.join(', ') %>):
316+
try:
317+
with conn:
318+
with conn.cursor() as cur:
319+
cur.execute(<%- sql %><%- formattedParams %>)
320+
finally:
321+
conn.close()
312322
<%
313323
314324
}

0 commit comments

Comments
 (0)