Skip to content

Commit

Permalink
hotfix
Browse files Browse the repository at this point in the history
  • Loading branch information
arily committed Dec 4, 2023
1 parent 9c30c19 commit 456e162
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
24 changes: 16 additions & 8 deletions app/repositories/maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,19 +183,27 @@ async def fetch_one(
if id is None and md5 is None and filename is None:
raise ValueError("Must provide at least one parameter.")

query = f"""\
SELECT {READ_PARAMS}
FROM maps
WHERE id = COALESCE(:id, id)
AND md5 = COALESCE(:md5, md5)
AND filename = COALESCE(:filename, filename)
"""
queries = [
"SELECT {READ_PARAMS} FROM maps WHERE 1 = 1",
]

if id is not None:
queries.append("AND id = :id")

if md5 is not None:
queries.append("AND md5 = :md5")

if filename is not None:
queries.append("AND filename = :filename")

queries.append(";")

params: dict[str, Any] = {
"id": id,
"md5": md5,
"filename": filename,
}
map = await app.state.services.database.fetch_one(query, params)
map = await app.state.services.database.fetch_one(" ".join(queries), params)

return cast(Map, dict(map._mapping)) if map is not None else None

Expand Down
37 changes: 20 additions & 17 deletions app/repositories/scores.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,31 +213,34 @@ async def fetch_many(
page: int | None = None,
page_size: int | None = None,
) -> list[Score]:
query = f"""\
SELECT {READ_PARAMS}
FROM scores
WHERE map_md5 = COALESCE(:map_md5, map_md5)
AND mods = COALESCE(:mods, mods)
AND status = COALESCE(:status, status)
AND mode = COALESCE(:mode, mode)
AND userid = COALESCE(:userid, userid)
"""
queries = [
"SELECT {READ_PARAMS} FROM scores WHERE 1 = 1",
"AND map_md5 = :map_md5" if map_md5 is not None else None,
"AND mods = :mods" if mods is not None else None,
"AND status = :status" if status is not None else None,
"AND mode = :mode" if mode is not None else None,
"AND userid = :userid" if user_id is not None else None,
"""\
LIMIT :page_size
OFFSET :offset
"""
if page is not None and page_size is not None
else None,
]

params: dict[str, Any] = {
"map_md5": map_md5,
"mods": mods,
"status": status,
"mode": mode,
"userid": user_id,
"page_size": page_size,
"offset": (page - 1) * page_size,
}
if page is not None and page_size is not None:
query += """\
LIMIT :page_size
OFFSET :offset
"""
params["page_size"] = page_size
params["offset"] = (page - 1) * page_size

recs = await app.state.services.database.fetch_all(query, params)
recs = await app.state.services.database.fetch_all(
[q for q in queries if q is not None], params
)
return cast(list[Score], [dict(r._mapping) for r in recs])


Expand Down

0 comments on commit 456e162

Please sign in to comment.