Skip to content

Commit

Permalink
feat(python): implement generation of DELETE endpoints
Browse files Browse the repository at this point in the history
Part of #16
  • Loading branch information
php-coder committed Apr 7, 2024
1 parent 2756738 commit cb5b941
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
14 changes: 12 additions & 2 deletions examples/python/fastapi/postgres/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,5 +163,15 @@ def put_v1_categories_category_id(body: CreateCategoryDto, categoryId, conn=Depe


@router.delete('/v1/categories/{categoryId}', status_code=status.HTTP_204_NO_CONTENT)
def delete_v1_categories_category_id():
pass
def delete_v1_categories_category_id(categoryId, conn=Depends(db_connection)):
try:
with conn:
with conn.cursor() as cur:
cur.execute(
"""
DELETE
FROM categories
WHERE id = %(categoryId)s
""", {"categoryId": categoryId})
finally:
conn.close()
14 changes: 12 additions & 2 deletions src/templates/routes.py.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,21 @@ def <%- pythonMethodName %>(<%- methodArgs.join(', ') %>):
}
if (method.name === 'delete') {
const methodArgs = [ ...argsFromPath, 'conn=Depends(db_connection)' ]
const sql = convertToPsycopgNamedArguments(formatQueryForPython(method.query, 20))
const params = extractParamsFromQuery(method.query)
const formattedParams = formatParamsAsPythonDict(params)
%>
@router.delete('<%- path %>', status_code=status.HTTP_204_NO_CONTENT)
def <%- pythonMethodName %>():
pass
def <%- pythonMethodName %>(<%- methodArgs.join(', ') %>):
try:
with conn:
with conn.cursor() as cur:
cur.execute(<%- sql %><%- formattedParams %>)
finally:
conn.close()
<%
}
Expand Down

0 comments on commit cb5b941

Please sign in to comment.