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

37 lines
1.1 KiB
Python
Raw Normal View History

2021-10-18 12:34:45 +01:00
from typing import List
from . import db
from .models import Target
2022-12-14 00:31:09 +00:00
from lnbits.helpers import urlsafe_short_hash
2021-10-18 12:34:45 +01:00
async def get_targets(source_wallet: str) -> List[Target]:
rows = await db.fetchall(
"SELECT * FROM splitpayments.targets WHERE source = ?", (source_wallet,)
)
return [Target(**dict(row)) for row in rows]
async def set_targets(source_wallet: str, targets: List[Target]):
async with db.connect() as conn:
await conn.execute(
"DELETE FROM splitpayments.targets WHERE source = ?", (source_wallet,)
)
for target in targets:
await conn.execute(
"""
INSERT INTO splitpayments.targets
2022-12-14 00:31:09 +00:00
(id, source, wallet, percent, tag, alias)
VALUES (?, ?, ?, ?, ?, ?)
2021-10-18 12:34:45 +01:00
""",
2022-12-14 00:06:36 +00:00
(
2022-12-14 00:31:09 +00:00
urlsafe_short_hash(),
2022-12-14 00:06:36 +00:00
source_wallet,
target.wallet,
target.percent,
target.tag,
target.alias,
),
2021-10-18 12:34:45 +01:00
)