From 1622403cd187bc66f73fd5e11141dcbfd2e91e32 Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Fri, 3 Mar 2023 11:22:48 +0200 Subject: [PATCH] refactor: stalls use `sign_and_send_to_nostr` --- crud.py | 6 +++--- views_api.py | 30 ++++++++---------------------- 2 files changed, 11 insertions(+), 25 deletions(-) diff --git a/crud.py b/crud.py index f4d85c5..6ad0139 100644 --- a/crud.py +++ b/crud.py @@ -222,7 +222,7 @@ async def update_product(user_id: str, product: Product) -> Product: await db.execute( f""" - UPDATE nostrmarket.products set name = ?, description = ?, images = ?, price = ?, quantity = ?, category_list = ?, meta = ? + UPDATE nostrmarket.products set name = ?, images = ?, price = ?, quantity = ?, category_list = ?, meta = ? WHERE user_id = ? AND id = ? """, ( @@ -230,10 +230,10 @@ async def update_product(user_id: str, product: Product) -> Product: product.image, product.price, product.quantity, + json.dumps(product.categories), + json.dumps(product.config.dict()), user_id, product.id, - json.dumps(product.categories), - json.dumps(product.config), ), ) updated_product = await get_product(user_id, product.id) diff --git a/views_api.py b/views_api.py index 63965d9..e095ee8 100644 --- a/views_api.py +++ b/views_api.py @@ -172,15 +172,9 @@ async def api_create_stall( try: data.validate_stall() - print("### stall", json.dumps(data.dict())) - merchant = await get_merchant_for_user(wallet.wallet.user) - assert merchant, "Cannot find merchat for stall" - stall = await create_stall(wallet.wallet.user, data=data) - event = stall.to_nostr_event(merchant.public_key) - event.sig = merchant.sign_hash(bytes.fromhex(event.id)) - await publish_nostr_event(event) + event = await sign_and_send_to_nostr(wallet.wallet.user, stall) stall.config.event_id = event.id await update_stall(wallet.wallet.user, stall) @@ -207,18 +201,13 @@ async def api_update_stall( try: data.validate_stall() - merchant = await get_merchant_for_user(wallet.wallet.user) - assert merchant, "Cannot find merchat for stall" - - event = data.to_nostr_event(merchant.public_key) - event.sig = merchant.sign_hash(bytes.fromhex(event.id)) - - data.config.event_id = event.id - # data.config.event_created_at = stall = await update_stall(wallet.wallet.user, data) assert stall, "Cannot update stall" - await publish_nostr_event(event) + event = await sign_and_send_to_nostr(wallet.wallet.user, stall) + + stall.config.event_id = event.id + await update_stall(wallet.wallet.user, stall) return stall except HTTPException as ex: @@ -297,15 +286,12 @@ async def api_delete_stall( detail="Stall does not exist.", ) - merchant = await get_merchant_for_user(wallet.wallet.user) - assert merchant, "Cannot find merchat for stall" - await delete_stall(wallet.wallet.user, stall_id) - delete_event = stall.to_nostr_delete_event(merchant.public_key) - delete_event.sig = merchant.sign_hash(bytes.fromhex(delete_event.id)) + event = await sign_and_send_to_nostr(wallet.wallet.user, stall, True) - await publish_nostr_event(delete_event) + stall.config.event_id = event.id + await update_stall(wallet.wallet.user, stall) except HTTPException as ex: raise ex