Fix deck imports

This commit is contained in:
Correl Roush 2022-08-03 16:49:37 -04:00
parent 5590c6ee54
commit 2040b25434
2 changed files with 9 additions and 6 deletions

View file

@ -125,16 +125,16 @@ def import_deck(ctx, filename, name):
for line in bar:
if match := line_pattern.match(line.strip()):
groups = match.groupdict()
cards = await tutor.database.advanced_search(
copies = await tutor.database.advanced_search(
cursor,
tutor.search.Search(
[tutor.search.Name(text=groups["name"])]
),
limit=1,
)
if cards:
if copies:
await tutor.database.store_deck_card(
cursor, deck_id, cards[0].oracle_id, int(groups["quantity"])
cursor, deck_id, copies[0].card.oracle_id, int(groups["quantity"])
)
await conn.commit()

View file

@ -168,6 +168,8 @@ async def advanced_search(
else:
joins.append("LEFT JOIN copies ON (cards.scryfall_id = copies.scryfall_id)")
constraints.append("copies.id IS NULL")
else:
joins.append("LEFT JOIN copies ON (cards.scryfall_id = copies.scryfall_id)")
joins.append("JOIN sets ON (cards.set_code = sets.set_code)")
joins.append(
"JOIN card_prices ON (cards.scryfall_id = card_prices.scryfall_id "
@ -324,11 +326,12 @@ async def clear_copies(db: psycopg.Cursor, collection: typing.Optional[str] = No
async def store_deck(db: psycopg.Cursor, name: str) -> None:
cursor = await db.execute(
'INSERT INTO "decks" ("name") VALUES (%(name)s)',
await db.execute(
'INSERT INTO "decks" ("name") VALUES (%(name)s) RETURNING "deck_id"',
{"name": name},
)
return cursor.lastrowid
result = await db.fetchone()
return result[0]
async def store_deck_card(