-
Notifications
You must be signed in to change notification settings - Fork 1
/
crud.py
180 lines (144 loc) · 5.11 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from typing import Optional, Union
from lnbits.db import Database
from lnbits.helpers import urlsafe_short_hash
from .models import CreateDomain, CreateSubdomain, Domains, Subdomains
db = Database("ext_subdomains")
async def create_subdomain(payment_hash, wallet, data: CreateSubdomain) -> Subdomains:
await db.execute(
"""
INSERT INTO subdomains.subdomain
(id, domain, email, subdomain, ip, wallet, sats, duration, paid, record_type)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
payment_hash,
data.domain,
data.email,
data.subdomain,
data.ip,
wallet,
data.sats,
data.duration,
False,
data.record_type,
),
)
new_subdomain = await get_subdomain(payment_hash)
assert new_subdomain, "Newly created subdomain couldn't be retrieved"
return new_subdomain
async def set_subdomain_paid(payment_hash: str) -> Subdomains:
row = await db.fetchone(
"""
SELECT s.*, d.domain as domain_name FROM subdomains.subdomain s
INNER JOIN subdomains.domain d ON (s.domain = d.id) WHERE s.id = ?
""",
(payment_hash,),
)
if row[8] is False:
await db.execute(
"""
UPDATE subdomains.subdomain
SET paid = true
WHERE id = ?
""",
(payment_hash,),
)
domaindata = await get_domain(row[1])
assert domaindata, "Couldn't get domain from paid subdomain"
amount = domaindata.amountmade + row[8]
await db.execute(
"""
UPDATE subdomains.domain
SET amountmade = ?
WHERE id = ?
""",
(amount, row[1]),
)
new_subdomain = await get_subdomain(payment_hash)
assert new_subdomain, "Newly paid subdomain couldn't be retrieved"
return new_subdomain
async def get_subdomain(subdomain_id: str) -> Optional[Subdomains]:
row = await db.fetchone(
"""
SELECT s.*, d.domain as domain_name FROM subdomains.subdomain s
INNER JOIN subdomains.domain d ON (s.domain = d.id) WHERE s.id = ?
""",
(subdomain_id,),
)
return Subdomains(**row) if row else None
async def get_subdomain_by_subdomain(subdomain: str) -> Optional[Subdomains]:
row = await db.fetchone(
"""
SELECT s.*, d.domain as domain_name FROM subdomains.subdomain s
INNER JOIN subdomains.domain d ON (s.domain = d.id) WHERE s.subdomain = ?
""",
(subdomain,),
)
return Subdomains(**row) if row else None
async def get_subdomains(wallet_ids: Union[str, list[str]]) -> list[Subdomains]:
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]
q = ",".join(["?"] * len(wallet_ids))
rows = await db.fetchall(
f"""
SELECT s.*, d.domain as domain_name FROM subdomains.subdomain s
INNER JOIN subdomains.domain d ON (s.domain = d.id) WHERE s.wallet IN ({q})
""",
(*wallet_ids,),
)
return [Subdomains(**row) for row in rows]
async def delete_subdomain(subdomain_id: str) -> None:
await db.execute("DELETE FROM subdomains.subdomain WHERE id = ?", (subdomain_id,))
# Domains
async def create_domain(data: CreateDomain) -> Domains:
domain_id = urlsafe_short_hash()
await db.execute(
"""
INSERT INTO subdomains.domain
(
id, wallet, domain, webhook, cf_token, cf_zone_id,
description, cost, amountmade, allowed_record_types
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
domain_id,
data.wallet,
data.domain,
data.webhook,
data.cf_token,
data.cf_zone_id,
data.description,
data.cost,
0,
data.allowed_record_types,
),
)
new_domain = await get_domain(domain_id)
assert new_domain, "Newly created domain couldn't be retrieved"
return new_domain
async def update_domain(domain_id: str, **kwargs) -> Domains:
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
await db.execute(
f"UPDATE subdomains.domain SET {q} WHERE id = ?", (*kwargs.values(), domain_id)
)
row = await db.fetchone(
"SELECT * FROM subdomains.domain WHERE id = ?", (domain_id,)
)
assert row, "Newly updated domain couldn't be retrieved"
return Domains(**row)
async def get_domain(domain_id: str) -> Optional[Domains]:
row = await db.fetchone(
"SELECT * FROM subdomains.domain WHERE id = ?", (domain_id,)
)
return Domains(**row) if row else None
async def get_domains(wallet_ids: Union[str, list[str]]) -> list[Domains]:
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]
q = ",".join(["?"] * len(wallet_ids))
rows = await db.fetchall(
f"SELECT * FROM subdomains.domain WHERE wallet IN ({q})", (*wallet_ids,)
)
return [Domains(**row) for row in rows]
async def delete_domain(domain_id: str) -> None:
await db.execute("DELETE FROM subdomains.domain WHERE id = ?", (domain_id,))