-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcrud.py
More file actions
129 lines (104 loc) · 3.35 KB
/
crud.py
File metadata and controls
129 lines (104 loc) · 3.35 KB
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
from datetime import datetime, timezone
from lnbits.db import Database
from lnbits.helpers import urlsafe_short_hash
from .models import (
Bitcoinswitch,
BitcoinswitchPayment,
CreateBitcoinswitch,
)
db = Database("ext_bitcoinswitch")
async def create_bitcoinswitch(
data: CreateBitcoinswitch,
) -> Bitcoinswitch:
bitcoinswitch_id = urlsafe_short_hash()
device = Bitcoinswitch(
id=bitcoinswitch_id,
title=data.title,
wallet=data.wallet,
currency=data.currency,
switches=data.switches,
password=data.password,
disabled=data.disabled,
disposable=data.disposable,
)
await db.insert("bitcoinswitch.switch", device)
return device
async def update_bitcoinswitch(device: Bitcoinswitch) -> Bitcoinswitch:
device.updated_at = datetime.now(timezone.utc)
await db.update("bitcoinswitch.switch", device)
return device
async def get_bitcoinswitch(bitcoinswitch_id: str) -> Bitcoinswitch | None:
return await db.fetchone(
"SELECT * FROM bitcoinswitch.switch WHERE id = :id",
{"id": bitcoinswitch_id},
Bitcoinswitch,
)
async def get_bitcoinswitches(wallet_ids: list[str]) -> list[Bitcoinswitch]:
q = ",".join([f"'{w}'" for w in wallet_ids])
return await db.fetchall(
f"""
SELECT * FROM bitcoinswitch.switch WHERE wallet IN ({q})
ORDER BY id
""",
model=Bitcoinswitch,
)
async def delete_bitcoinswitch(bitcoinswitch_id: str) -> None:
await db.execute(
"DELETE FROM bitcoinswitch.switch WHERE id = :id",
{"id": bitcoinswitch_id},
)
async def create_switch_payment(
payment_hash: str,
switch_id: str,
pin: int,
amount_msat: int = 0,
) -> BitcoinswitchPayment:
payment_id = urlsafe_short_hash()
payment = BitcoinswitchPayment(
id=payment_id,
payment_hash=payment_hash,
bitcoinswitch_id=switch_id,
pin=pin,
sats=amount_msat,
)
await db.insert("bitcoinswitch.payment", payment)
return payment
async def update_switch_payment(
switch_payment: BitcoinswitchPayment,
) -> BitcoinswitchPayment:
switch_payment.updated_at = datetime.now(timezone.utc)
await db.update("bitcoinswitch.payment", switch_payment)
return switch_payment
async def delete_switch_payment(switch_payment_id: str) -> None:
await db.execute(
"DELETE FROM bitcoinswitch.payment WHERE id = :id",
{"id": switch_payment_id},
)
async def get_switch_payment(
bitcoinswitchpayment_id: str,
) -> BitcoinswitchPayment | None:
return await db.fetchone(
"SELECT * FROM bitcoinswitch.payment WHERE id = :id",
{"id": bitcoinswitchpayment_id},
BitcoinswitchPayment,
)
async def get_switch_payment_by_payment_hash(
payment_hash: str,
) -> BitcoinswitchPayment | None:
return await db.fetchone(
"SELECT * FROM bitcoinswitch.payment WHERE payment_hash = :h",
{"h": payment_hash},
)
async def get_switch_payments(
bitcoinswitch_ids: list[str],
) -> list[BitcoinswitchPayment]:
if len(bitcoinswitch_ids) == 0:
return []
q = ",".join([f"'{w}'" for w in bitcoinswitch_ids])
return await db.fetchall(
f"""
SELECT * FROM bitcoinswitch.payment WHERE deviceid IN ({q})
ORDER BY id
""",
model=BitcoinswitchPayment,
)