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

44 lines
1.2 KiB
Python
Raw Normal View History

2020-04-10 22:05:50 +01:00
from typing import List, Optional, Union
from lnbits.db import open_ext_db
2020-04-16 17:10:53 +02:00
from lnbits.helpers import urlsafe_short_hash
2020-04-10 22:05:50 +01:00
from .models import TPoS
def create_tpos(*, wallet_id: str, name: str, currency: str) -> TPoS:
with open_ext_db("tpos") as db:
2020-04-16 17:10:53 +02:00
tpos_id = urlsafe_short_hash()
2020-04-10 22:05:50 +01:00
db.execute(
"""
INSERT INTO tposs (id, wallet, name, currency)
VALUES (?, ?, ?, ?)
""",
(tpos_id, wallet_id, name, currency),
)
return get_tpos(tpos_id)
def get_tpos(tpos_id: str) -> Optional[TPoS]:
with open_ext_db("tpos") as db:
row = 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
def get_tposs(wallet_ids: Union[str, List[str]]) -> List[TPoS]:
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]
with open_ext_db("tpos") as db:
q = ",".join(["?"] * len(wallet_ids))
rows = db.fetchall(f"SELECT * FROM tposs WHERE wallet IN ({q})", (*wallet_ids,))
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
def delete_tpos(tpos_id: str) -> None:
with open_ext_db("tpos") as db:
db.execute("DELETE FROM tposs WHERE id = ?", (tpos_id,))