feat: init merchant
This commit is contained in:
parent
2832ee928c
commit
99492b36c8
6 changed files with 164 additions and 8 deletions
42
crud.py
Normal file
42
crud.py
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue