Sort search results

This commit is contained in:
Correl Roush 2021-07-15 22:00:01 -04:00
parent 77c84fc083
commit a08a2998a3

View file

@ -141,12 +141,19 @@ async def advanced_search(
joins.append("LEFT JOIN copies ON (cards.scryfall_id = copies.scryfall_id)") joins.append("LEFT JOIN copies ON (cards.scryfall_id = copies.scryfall_id)")
constraints.append("copies.id IS NULL") constraints.append("copies.id IS NULL")
joins.append("JOIN sets ON (cards.set_code = sets.set_code)") joins.append("JOIN sets ON (cards.set_code = sets.set_code)")
joins.append("JOIN rarities ON (cards.rarity = rarities.rarity)")
query = " ".join( query = " ".join(
[ [
"SELECT cards.* FROM cards", "SELECT cards.* FROM cards",
" ".join(joins), " ".join(joins),
"WHERE" if constraints else "", "WHERE" if constraints else "",
" AND ".join(constraints), " AND ".join(constraints),
"ORDER BY rarities.rarity_ord DESC",
", length(cards.color_identity) DESC",
", CASE ",
" WHEN length(cards.color_identity) > 0 THEN '0'"
" ELSE cards.color_identity END ASC",
", cards.name ASC",
f"LIMIT {offset},{limit}", f"LIMIT {offset},{limit}",
] ]
) )