Skip to content
This repository has been archived by the owner on Oct 5, 2024. It is now read-only.

Commit

Permalink
Fix for orderBy, pagination, query error
Browse files Browse the repository at this point in the history
  • Loading branch information
emi420 committed Dec 5, 2023
1 parent 8465c75 commit 0d8c808
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 15 deletions.
4 changes: 2 additions & 2 deletions python/dbapi/api/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ def run(self, query, responseType = 'json', singleObject = False):
try:
cur.execute(query)
except Exception as e:
print("*******" + "\n" + query + "\n")
print("\n******* \n" + query + "\n******* \n")
print(e)
cur.close()
return {"Error": "There was an error running the query."}
return None

results = None

Expand Down
23 changes: 12 additions & 11 deletions python/dbapi/api/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ def listFeaturesQuery(
dateTo = None,
status = None,
table = None,
orderBy = "created_at"
):

geoType = getGeoType(table)
Expand All @@ -116,10 +115,12 @@ def listFeaturesQuery(
return query

def queryToJSON(query, orderBy, page):
return "with data AS (" + query + " {0}) , t_features AS ( \
SELECT to_jsonb(data) as feature from data \
) SELECT jsonb_agg(t_features.feature) as result FROM t_features;".format(
"ORDER BY " + orderBy + " DESC LIMIT " + str(RESULTS_PER_PAGE_LIST) + (" OFFSET {0}".format(page * RESULTS_PER_PAGE_LIST) if page else ""),
return "with data AS (" + query + ") , t_features AS ( \
SELECT to_jsonb(data) as feature from data {0} \
) SELECT jsonb_agg(t_features.feature) as result FROM t_features;" \
.format(
"WHERE " + orderBy + " IS NOT NULL ORDER BY " + orderBy + " DESC LIMIT " + str(RESULTS_PER_PAGE_LIST) + (" OFFSET {0}" \
.format(page * RESULTS_PER_PAGE_LIST) if page else ""),
)

class Raw:
Expand Down Expand Up @@ -236,13 +237,13 @@ def getAll(

result = {'type': 'FeatureCollection', 'features': []}

if 'features' in polygons and polygons['features']:
if polygons and polygons['features']:
result['features'] = result['features'] + polygons['features']

if 'features' in lines and lines['features']:
if lines and lines['features']:
result['features'] = result['features'] + lines['features']

elif 'features' in nodes and nodes['features']:
elif nodes and nodes['features']:
result['features'] = result['features'] + nodes['features']

return result
Expand Down Expand Up @@ -291,7 +292,6 @@ def getLinesList(
"ways_line"
), responseType, True)


def getNodesList(
self,
area = None,
Expand Down Expand Up @@ -324,6 +324,7 @@ def getAllList(
dateFrom = None,
dateTo = None,
status = None,
orderBy = None,
page = None
):

Expand Down Expand Up @@ -359,8 +360,8 @@ def getAllList(

query = queryToJSON(
" UNION ".join([queryPolygons, queryLines, queryNodes]),
"id",
page
orderBy or "id",
page or 0,
)

return self.underpassDB.run(query, responseType, True)
5 changes: 4 additions & 1 deletion python/dbapi/api/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ def getCount(
"AND status = '{0}'".format(status) if (status) else "",
)

return self.underpassDB.run(query, True)[0]
result = self.underpassDB.run(query, True)
if result:
return result[0]
return None


3 changes: 2 additions & 1 deletion python/restapi/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def getLines(request: RawRequest):
return results

@app.post("/raw/all")
def getLines(request: RawRequest):
def getAll(request: RawRequest):
results = rawer.getAll(
area = request.area,
tags = request.tags or "",
Expand Down Expand Up @@ -241,6 +241,7 @@ def getAllList(request: RawRequest):
dateFrom = request.dateFrom or "",
dateTo = request.dateTo or "",
status = request.status or "",
orderBy = request.orderBy or None,
page = request.page,
)
return results
Expand Down
1 change: 1 addition & 0 deletions python/restapi/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class RawRequest(BaseModel):
dateFrom: str = None
dateTo: str = None
status: str = None
orderBy: str = None
page: int = None

class StatsRequest(BaseModel):
Expand Down

0 comments on commit 0d8c808

Please sign in to comment.