项目改造
This commit is contained in:
@@ -153,14 +153,34 @@ def count_stratagems() -> int:
|
||||
return int(conn.execute("SELECT COUNT(*) AS n FROM stratagems").fetchone()["n"])
|
||||
|
||||
|
||||
def list_stratagems_page(page: int = 1, page_size: int = 20) -> tuple[list[dict[str, Any]], int]:
|
||||
def list_stratagems_page(
|
||||
page: int = 1,
|
||||
page_size: int = 20,
|
||||
keyword: str = "",
|
||||
type: str = "",
|
||||
backpack: str = "",
|
||||
) -> tuple[list[dict[str, Any]], int]:
|
||||
init_db()
|
||||
total = count_stratagems()
|
||||
offset = (page - 1) * page_size
|
||||
where: list[str] = []
|
||||
params: list[Any] = []
|
||||
kw = (keyword or "").strip()
|
||||
if kw:
|
||||
like = f"%{kw}%"
|
||||
where.append("(name LIKE ? OR name_en LIKE ? OR alias LIKE ?)")
|
||||
params.extend([like, like, like])
|
||||
if type:
|
||||
where.append("type = ?")
|
||||
params.append(type)
|
||||
if backpack in ("0", "1"):
|
||||
where.append("backpack = ?")
|
||||
params.append(int(backpack))
|
||||
where_sql = (" WHERE " + " AND ".join(where)) if where else ""
|
||||
with _connect() as conn:
|
||||
total = int(conn.execute(f"SELECT COUNT(*) FROM stratagems{where_sql}", params).fetchone()[0])
|
||||
offset = (page - 1) * page_size
|
||||
rows = conn.execute(
|
||||
"SELECT * FROM stratagems ORDER BY id LIMIT ? OFFSET ?",
|
||||
(page_size, offset),
|
||||
f"SELECT * FROM stratagems{where_sql} ORDER BY id LIMIT ? OFFSET ?",
|
||||
(*params, page_size, offset),
|
||||
).fetchall()
|
||||
return [_row_to_item(r) for r in rows], total
|
||||
|
||||
|
||||
Reference in New Issue
Block a user