let posts be found by primary key id

This commit is contained in:
Luna 2024-02-12 00:08:29 -03:00
parent 834a693282
commit adbb2d532b

View file

@ -150,16 +150,33 @@ async def posts_json():
if len(tags) != 1: if len(tags) != 1:
return "unsupported query (only 1 tag)", 400 return "unsupported query (only 1 tag)", 400
md5_tag = tags[0] first_tag = tags[0]
if not md5_tag.startswith("md5:"): tag_parts = first_tag.split(":")
return "unsupported query (only md5 for now)", 400 if len(tag_parts) != 2:
return "unsupported query (tag must be a:b format)", 400
md5_prefix, md5_hexencoded_hash = md5_tag.split(":") type, value = tag_parts
assert md5_prefix == "md5" if type not in ("md5", "id"):
return (
"unsupported query (tag must be a:b format, and a must be id or md5)",
400,
)
rows = await app.db.execute_fetchall( if type == "md5":
"select * from posts where md5 = ?", (md5_hexencoded_hash,) md5_hexencoded_hash = value
) rows = await app.db.execute_fetchall(
"select * from posts where md5 = ?", (md5_hexencoded_hash,)
)
elif type == "id":
try:
value = int(value)
except ValueError:
return "id:x x must be int", 400
rows = await app.db.execute_fetchall(
"select * from posts where id = ?", (value,)
)
else:
raise AssertionError("must be id or md5")
if not rows: if not rows:
return {"posts": []} return {"posts": []}