diff --git a/tutor/database.py b/tutor/database.py index 14b5423..868921a 100644 --- a/tutor/database.py +++ b/tutor/database.py @@ -141,12 +141,19 @@ async def advanced_search( joins.append("LEFT JOIN copies ON (cards.scryfall_id = copies.scryfall_id)") constraints.append("copies.id IS NULL") joins.append("JOIN sets ON (cards.set_code = sets.set_code)") + joins.append("JOIN rarities ON (cards.rarity = rarities.rarity)") query = " ".join( [ "SELECT cards.* FROM cards", " ".join(joins), "WHERE" if constraints else "", " 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}", ] )