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

108 lines
3.1 KiB
Python
Raw Normal View History

2021-04-12 20:31:39 +01:00
from typing import List, Optional, Union
# from lnbits.db import open_ext_db
from . import db
from .models import Copilots
from lnbits.helpers import urlsafe_short_hash
from quart import jsonify
###############COPILOTS##########################
async def create_copilot(
title: str,
user: str,
2021-06-24 00:33:49 +01:00
lnurl_toggle: Optional[int] = 0,
wallet: Optional[str] = None,
animation1: Optional[str] = None,
animation2: Optional[str] = None,
animation3: Optional[str] = None,
animation1threshold: Optional[int] = None,
animation2threshold: Optional[int] = None,
animation3threshold: Optional[int] = None,
animation1webhook: Optional[str] = None,
animation2webhook: Optional[str] = None,
animation3webhook: Optional[str] = None,
2021-04-12 20:31:39 +01:00
lnurl_title: Optional[str] = None,
2021-06-23 20:05:20 +01:00
show_message: Optional[int] = 0,
show_ack: Optional[int] = 0,
2021-08-13 16:31:07 +01:00
show_price: Optional[str] = None,
amount_made: Optional[int] = None,
2021-04-12 20:31:39 +01:00
) -> Copilots:
copilot_id = urlsafe_short_hash()
await db.execute(
"""
2021-08-13 16:31:07 +01:00
INSERT INTO copilot.copilots (
2021-04-12 20:31:39 +01:00
id,
2021-07-02 17:36:26 -03:00
"user",
lnurl_toggle,
wallet,
2021-04-12 20:31:39 +01:00
title,
animation1,
animation2,
animation3,
animation1threshold,
animation2threshold,
animation3threshold,
animation1webhook,
animation2webhook,
animation3webhook,
lnurl_title,
2021-04-12 20:31:39 +01:00
show_message,
show_ack,
2021-04-16 11:07:14 +01:00
show_price,
amount_made
2021-04-12 20:31:39 +01:00
)
2021-08-13 16:31:07 +01:00
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2021-04-12 20:31:39 +01:00
""",
(
copilot_id,
user,
2021-08-13 16:31:07 +01:00
int(lnurl_toggle),
wallet,
2021-04-12 20:31:39 +01:00
title,
animation1,
animation2,
animation3,
animation1threshold,
animation2threshold,
animation3threshold,
animation1webhook,
animation2webhook,
animation3webhook,
lnurl_title,
2021-08-13 16:31:07 +01:00
int(show_message),
int(show_ack),
2021-04-16 11:07:14 +01:00
show_price,
2021-04-16 12:20:05 +01:00
0,
2021-04-12 20:31:39 +01:00
),
)
return await get_copilot(copilot_id)
async def update_copilot(copilot_id: str, **kwargs) -> Optional[Copilots]:
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
await db.execute(
2021-08-13 16:31:07 +01:00
f"UPDATE copilot.copilots SET {q} WHERE id = ?", (*kwargs.values(), copilot_id)
2021-04-12 20:31:39 +01:00
)
2021-08-13 16:31:07 +01:00
row = await db.fetchone("SELECT * FROM copilot.copilots WHERE id = ?", (copilot_id,))
2021-04-12 20:31:39 +01:00
return Copilots.from_row(row) if row else None
async def get_copilot(copilot_id: str) -> Copilots:
2021-08-13 16:31:07 +01:00
row = await db.fetchone("SELECT * FROM copilot.copilots WHERE id = ?", (copilot_id,))
2021-04-12 20:31:39 +01:00
return Copilots.from_row(row) if row else None
async def get_copilots(user: str) -> List[Copilots]:
2021-08-13 16:31:07 +01:00
rows = await db.fetchall("""SELECT * FROM copilot.copilots WHERE "user" = ?""", (user,))
2021-04-12 20:31:39 +01:00
return [Copilots.from_row(row) for row in rows]
async def delete_copilot(copilot_id: str) -> None:
2021-08-13 16:31:07 +01:00
await db.execute("DELETE FROM copilot.copilots WHERE id = ?", (copilot_id,))