From cb5b9412db0be7fffc5ae10894582dd75ce963e5 Mon Sep 17 00:00:00 2001 From: Slava Semushin Date: Sun, 7 Apr 2024 11:55:37 +0700 Subject: [PATCH] feat(python): implement generation of DELETE endpoints Part of #16 --- examples/python/fastapi/postgres/routes.py | 14 ++++++++++++-- src/templates/routes.py.ejs | 14 ++++++++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/examples/python/fastapi/postgres/routes.py b/examples/python/fastapi/postgres/routes.py index 88abc5d..1a4f90e 100644 --- a/examples/python/fastapi/postgres/routes.py +++ b/examples/python/fastapi/postgres/routes.py @@ -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() diff --git a/src/templates/routes.py.ejs b/src/templates/routes.py.ejs index 48c93b6..011335b 100644 --- a/src/templates/routes.py.ejs +++ b/src/templates/routes.py.ejs @@ -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() <% }