Skip to content

Commit b9c08ce

Browse files
committed
feat(python): implement get_list
Part of #16
1 parent adb50a2 commit b9c08ce

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

examples/python/routes.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,13 @@ def get_v1_collections_collection_id_categories_count(collectionId, conn = Depen
6161

6262
@router.get('/v1/categories')
6363
def get_list_v1_categories(conn = Depends(db_connection)):
64-
pass
64+
try:
65+
with conn:
66+
with conn.cursor(cursor_factory = psycopg2.extras.RealDictCursor) as cur:
67+
cur.execute("SELECT id , name , name_ru , slug FROM categories")
68+
return cur.fetchall()
69+
finally:
70+
conn.close()
6571

6672
@router.post('/v1/categories')
6773
def post_v1_categories():

src/templates/routes.py.ejs

+11-7
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ endpoints.forEach(function(endpoint) {
6969
%>
7070
@router.get('<%- path %>')
7171
def <%- pythonMethodName %>(<%- methodArgs.join(', ') %>):
72-
<% if (hasGetOne) { -%>
7372
try:
7473
with conn:
75-
<% if (queries.length > 1) { /* we can omit cursor_factory but in this case we might get an unused import */-%>
74+
<% if (hasGetOne) {
75+
if (queries.length > 1) { /* we can omit cursor_factory but in this case we might get an unused import */-%>
7676
with conn.cursor(cursor_factory = psycopg2.extras.DictCursor) as cur:
7777
result = {}
7878
<% queries.forEach(queryInfo => {
@@ -94,15 +94,19 @@ def <%- pythonMethodName %>(<%- methodArgs.join(', ') %>):
9494
if result is None:
9595
raise HTTPException(status_code=404)
9696
return result
97-
<% } -%>
98-
finally:
99-
conn.close()
100-
<%
97+
<% }
10198
} else {
99+
const query = queries[0].result
102100
-%>
103-
pass
101+
with conn.cursor(cursor_factory = psycopg2.extras.RealDictCursor) as cur:
102+
cur.execute("<%- query.sql %>"<%- query.formattedParams %>)
103+
return cur.fetchall()
104104
<%
105105
}
106+
-%>
107+
finally:
108+
conn.close()
109+
<%
106110
}
107111
if (method.name === 'post') {
108112
%>

0 commit comments

Comments
 (0)