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

95 lines
2.8 KiB
Python
Raw Normal View History

from typing import List, Optional, Union
from lnbits.helpers import urlsafe_short_hash
2022-07-16 14:23:03 +02:00
from . import db
2022-07-16 14:23:03 +02:00
from .models import CreatePayLinkData, PayLink
2021-10-17 19:33:29 +02:00
async def create_pay_link(data: CreatePayLinkData, wallet_id: str) -> PayLink:
link_id = urlsafe_short_hash()[:6]
result = await db.execute(
f"""
INSERT INTO lnurlp.pay_links (
id,
wallet,
description,
min,
max,
served_meta,
served_pr,
webhook_url,
webhook_headers,
webhook_body,
success_text,
success_url,
comment_chars,
2022-06-10 16:05:33 +02:00
currency,
fiat_base_multiplier
)
VALUES (?, ?, ?, ?, ?, 0, 0, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
link_id,
wallet_id,
2021-10-05 10:19:21 +02:00
data.description,
data.min,
data.max,
data.webhook_url,
data.webhook_headers,
data.webhook_body,
2021-10-05 10:19:21 +02:00
data.success_text,
data.success_url,
data.comment_chars,
data.currency,
2022-06-10 16:05:33 +02:00
data.fiat_base_multiplier,
),
)
link = await get_pay_link(link_id)
assert link, "Newly created link couldn't be retrieved"
return link
async def get_pay_link(link_id: str) -> Optional[PayLink]:
row = await db.fetchone("SELECT * FROM lnurlp.pay_links WHERE id = ?", (link_id,))
return PayLink.from_row(row) if row else None
async def get_pay_links(wallet_ids: Union[str, List[str]]) -> List[PayLink]:
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]
q = ",".join(["?"] * len(wallet_ids))
rows = await db.fetchall(
f"""
SELECT * FROM lnurlp.pay_links WHERE wallet IN ({q})
ORDER BY Id
""",
(*wallet_ids,),
)
return [PayLink.from_row(row) for row in rows]
async def update_pay_link(link_id: int, **kwargs) -> Optional[PayLink]:
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
await db.execute(
f"UPDATE lnurlp.pay_links SET {q} WHERE id = ?", (*kwargs.values(), link_id)
)
row = await db.fetchone("SELECT * FROM lnurlp.pay_links WHERE id = ?", (link_id,))
return PayLink.from_row(row) if row else None
async def increment_pay_link(link_id: int, **kwargs) -> Optional[PayLink]:
q = ", ".join([f"{field[0]} = {field[0]} + ?" for field in kwargs.items()])
await db.execute(
f"UPDATE lnurlp.pay_links SET {q} WHERE id = ?", (*kwargs.values(), link_id)
)
row = await db.fetchone("SELECT * FROM lnurlp.pay_links WHERE id = ?", (link_id,))
return PayLink.from_row(row) if row else None
async def delete_pay_link(link_id: int) -> None:
await db.execute("DELETE FROM lnurlp.pay_links WHERE id = ?", (link_id,))