feat: init merchant

This commit is contained in:
Vlad Stan 2023-02-28 11:46:40 +02:00
parent 2832ee928c
commit 99492b36c8
6 changed files with 164 additions and 8 deletions

42
crud.py Normal file
View file

@ -0,0 +1,42 @@
import json
from typing import Optional
from lnbits.helpers import urlsafe_short_hash
from . import db
from .models import Merchant, PartialMerchant
async def create_merchant(user_id: str, m: PartialMerchant) -> Merchant:
merchant_id = urlsafe_short_hash()
await db.execute(
"""
INSERT INTO nostrmarket.merchants (user_id, id, private_key, public_key, meta)
VALUES (?, ?, ?, ?, ?)
""",
(user_id, merchant_id, m.private_key, m.public_key, json.dumps(dict(m.config))),
)
merchant = await get_merchant(user_id, merchant_id)
assert merchant, "Created merchant cannot be retrieved"
return merchant
async def get_merchant(user_id: str, merchant_id: str) -> Optional[Merchant]:
row = await db.fetchone(
"""SELECT * FROM nostrmarket.merchants WHERE user_id = ? AND id = ?""",
(
user_id,
merchant_id,
),
)
return Merchant.from_row(row) if row else None
async def get_merchant_for_user(user_id: str) -> Optional[Merchant]:
row = await db.fetchone(
"""SELECT * FROM nostrmarket.merchants WHERE user_id = ? """,
(user_id,),
)
return Merchant.from_row(row) if row else None