-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrud.py
139 lines (110 loc) · 3.34 KB
/
crud.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from typing import Optional
import shortuuid
from lnbits.db import Database
from lnbits.helpers import urlsafe_short_hash
from .models import CreateFossa, Fossa, FossaPayment
db = Database("ext_fossa")
async def create_fossa(data: CreateFossa) -> Fossa:
fossa_id = shortuuid.uuid()[:5]
fossa_key = urlsafe_short_hash()
device = Fossa(
id=fossa_id,
key=fossa_key,
title=data.title,
wallet=data.wallet,
profit=data.profit,
currency=data.currency,
boltz=data.boltz,
)
await db.insert("fossa.fossa", device)
return device
async def update_fossa(fossa: Fossa) -> Fossa:
await db.update("fossa.fossa", fossa)
return fossa
async def get_fossa(fossa_id: str) -> Optional[Fossa]:
return await db.fetchone(
"SELECT * FROM fossa.fossa WHERE id = :id",
{"id": fossa_id},
Fossa,
)
async def get_fossas(wallet_ids: list[str]) -> list[Fossa]:
q = ",".join([f"'{w}'" for w in wallet_ids])
return await db.fetchall(
f"""
SELECT * FROM fossa.fossa WHERE wallet IN ({q}) ORDER BY id
""",
model=Fossa,
)
async def delete_fossa(fossa_id: str) -> None:
await db.execute("DELETE FROM fossa.fossa WHERE id = :id", {"id": fossa_id})
async def create_fossa_payment(
deviceid: str,
payload: str,
pin: int,
payhash: str,
sats: int = 0,
) -> FossaPayment:
fossa_payment_id = urlsafe_short_hash()
payment = FossaPayment(
id=fossa_payment_id,
deviceid=deviceid,
payload=payload,
pin=pin,
payhash=payhash,
sats=sats,
)
await db.insert("fossa.fossa_payment", payment)
return payment
async def update_fossa_payment(
fossa_payment: FossaPayment,
) -> FossaPayment:
await db.update("fossa.fossa_payment", fossa_payment)
return fossa_payment
async def get_fossa_payment(
fossa_payment_id: str,
) -> Optional[FossaPayment]:
return await db.fetchone(
"SELECT * FROM fossa.fossa_payment WHERE id = :id",
{"id": fossa_payment_id},
FossaPayment,
)
async def get_fossa_payments(
fossa_ids: list[str],
) -> list[FossaPayment]:
if len(fossa_ids) == 0:
return []
q = ",".join([f"'{w}'" for w in fossa_ids])
return await db.fetchall(
f"""
SELECT * FROM fossa.fossa_payment WHERE deviceid IN ({q})
ORDER BY id
""",
model=FossaPayment,
)
async def get_fossa_payment_by_payhash(
payhash: str,
) -> Optional[FossaPayment]:
return await db.fetchone(
"SELECT * FROM fossa.fossa_payment WHERE payhash = :payhash",
{"payhash": payhash},
FossaPayment,
)
async def get_fossa_payment_by_payload(
payload: str,
) -> Optional[FossaPayment]:
return await db.fetchone(
"SELECT * FROM fossa.fossa_payment WHERE payload = :payload",
{"payload": payload},
FossaPayment,
)
async def get_recent_fossa_payment(payload: str) -> Optional[FossaPayment]:
return await db.fetchone(
"""
SELECT * FROM fossa.fossa_payment
WHERE payload = :payload ORDER BY timestamp DESC LIMIT 1
""",
{"payload": payload},
FossaPayment,
)
async def delete_atm_payment_link(atm_id: str) -> None:
await db.execute("DELETE FROM fossa.fossa_payment WHERE id = :id", {"id": atm_id})