lnbits-legend/lnbits/extensions/tpos/crud.py

41 lines
1.1 KiB
Python
Raw Normal View History

2020-04-10 22:05:50 +01:00
from typing import List, Optional, Union
2020-04-16 17:10:53 +02:00
from lnbits.helpers import urlsafe_short_hash
2020-04-10 22:05:50 +01:00
from . import db
2020-04-10 22:05:50 +01:00
from .models import TPoS
async def create_tpos(*, wallet_id: str, name: str, currency: str) -> TPoS:
tpos_id = urlsafe_short_hash()
await db.execute(
"""
INSERT INTO tposs (id, wallet, name, currency)
VALUES (?, ?, ?, ?)
""",
(tpos_id, wallet_id, name, currency),
)
2020-04-10 22:05:50 +01:00
tpos = await get_tpos(tpos_id)
assert tpos, "Newly created tpos couldn't be retrieved"
return tpos
2020-04-10 22:05:50 +01:00
async def get_tpos(tpos_id: str) -> Optional[TPoS]:
row = await db.fetchone("SELECT * FROM tposs WHERE id = ?", (tpos_id,))
2020-05-08 21:11:59 +02:00
return TPoS.from_row(row) if row else None
2020-04-10 22:05:50 +01:00
async def get_tposs(wallet_ids: Union[str, List[str]]) -> List[TPoS]:
2020-04-10 22:05:50 +01:00
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]
q = ",".join(["?"] * len(wallet_ids))
rows = await db.fetchall(f"SELECT * FROM tposs WHERE wallet IN ({q})", (*wallet_ids,))
2020-04-10 22:05:50 +01:00
2020-05-08 21:11:59 +02:00
return [TPoS.from_row(row) for row in rows]
2020-04-10 22:05:50 +01:00
async def delete_tpos(tpos_id: str) -> None:
await db.execute("DELETE FROM tposs WHERE id = ?", (tpos_id,))