2021-10-06 14:49:00 +01:00
|
|
|
from typing import List, Optional, Union
|
|
|
|
|
|
|
|
from lnbits.helpers import urlsafe_short_hash
|
|
|
|
|
|
|
|
from . import db
|
2021-10-18 10:58:09 +01:00
|
|
|
from .models import CreateTposData, TPoS
|
2021-10-06 14:49:00 +01:00
|
|
|
|
|
|
|
|
2021-10-06 19:43:57 +01:00
|
|
|
async def create_tpos(wallet_id: str, data: CreateTposData) -> TPoS:
|
2021-10-06 14:49:00 +01:00
|
|
|
tpos_id = urlsafe_short_hash()
|
|
|
|
await db.execute(
|
|
|
|
"""
|
2022-07-02 21:16:27 -06:00
|
|
|
INSERT INTO tpos.tposs (id, wallet, name, currency, tip_options, tip_wallet)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?)
|
2021-10-06 14:49:00 +01:00
|
|
|
""",
|
2022-07-05 17:18:22 +02:00
|
|
|
(
|
|
|
|
tpos_id,
|
|
|
|
wallet_id,
|
|
|
|
data.name,
|
|
|
|
data.currency,
|
|
|
|
data.tip_options,
|
|
|
|
data.tip_wallet,
|
|
|
|
),
|
2021-10-06 14:49:00 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
tpos = await get_tpos(tpos_id)
|
|
|
|
assert tpos, "Newly created tpos couldn't be retrieved"
|
|
|
|
return tpos
|
|
|
|
|
|
|
|
|
|
|
|
async def get_tpos(tpos_id: str) -> Optional[TPoS]:
|
|
|
|
row = await db.fetchone("SELECT * FROM tpos.tposs WHERE id = ?", (tpos_id,))
|
2022-07-25 10:19:03 +01:00
|
|
|
return TPoS(**row) if row else None
|
2021-10-06 14:49:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def get_tposs(wallet_ids: Union[str, List[str]]) -> List[TPoS]:
|
|
|
|
if isinstance(wallet_ids, str):
|
|
|
|
wallet_ids = [wallet_ids]
|
|
|
|
|
|
|
|
q = ",".join(["?"] * len(wallet_ids))
|
|
|
|
rows = await db.fetchall(
|
|
|
|
f"SELECT * FROM tpos.tposs WHERE wallet IN ({q})", (*wallet_ids,)
|
|
|
|
)
|
|
|
|
|
2022-07-25 10:19:03 +01:00
|
|
|
return [TPoS(**row) for row in rows]
|
2021-10-06 14:49:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def delete_tpos(tpos_id: str) -> None:
|
|
|
|
await db.execute("DELETE FROM tpos.tposs WHERE id = ?", (tpos_id,))
|