-
Notifications
You must be signed in to change notification settings - Fork 0
/
crud.py
173 lines (150 loc) · 5.17 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
from typing import List, Optional, Union
from lnbits.helpers import urlsafe_short_hash
from lnbits.lnurl import encode as lnurl_encode
from . import db
from .models import CreateRaiseNowData, RaiseNow, CreateParticipantData, Participant
from loguru import logger
from fastapi import Request
# from lnurl import encode as lnurl_encode
#######################################
############### RAISES ################
#######################################
async def create_raisenow(
wallet_id: str, data: CreateRaiseNowData, req: Request
) -> RaiseNow:
raisenow_id = urlsafe_short_hash()
await db.execute(
"""
INSERT INTO raisenow.raises (id, wallet, name, description, background_image, header_image, live_dates)
VALUES (?, ?, ?, ?, ?, ?, ?)
""",
(
raisenow_id,
wallet_id,
data.name,
data.description,
data.background_image,
data.header_image,
data.live_dates,
),
)
raisenow = await get_raisenow(raisenow_id, req)
assert raisenow, "Newly created table couldn't be retrieved"
return raisenow
async def get_raisenow(
raisenow_id: str, req: Optional[Request] = None
) -> Optional[RaiseNow]:
row = await db.fetchone(
"SELECT * FROM raisenow.raises WHERE id = ?", (raisenow_id,)
)
if not row:
return None
rowAmended = RaiseNow(**row)
if req:
rowAmended.lnurlpay = lnurl_encode(
req.url_for("raisenow.api_lnurl_pay", record_id=row.id)._url
)
return rowAmended
async def get_raisenows(
wallet_ids: Union[str, List[str]], req: Optional[Request] = None
) -> List[RaiseNow]:
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]
q = ",".join(["?"] * len(wallet_ids))
rows = await db.fetchall(
f"SELECT * FROM raisenow.raises WHERE wallet IN ({q})", (*wallet_ids,)
)
tempRows = [RaiseNow(**row) for row in rows]
if req:
for row in tempRows:
row.lnurlpay = lnurl_encode(
req.url_for("raisenow.api_lnurl_pay", record_id=row.id)._url
)
return tempRows
async def update_raisenow(
raisenow_id: str, req: Optional[Request] = None, **kwargs
) -> RaiseNow:
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
await db.execute(
f"UPDATE raisenow.raises SET {q} WHERE id = ?",
(*kwargs.values(), raisenow_id),
)
raisenow = await get_raisenow(raisenow_id, req)
assert raisenow, "Newly updated raisenow couldn't be retrieved"
return raisenow
async def delete_raisenow(raisenow_id: str) -> None:
await db.execute(
"DELETE FROM raisenow.raises WHERE id = ?", (raisenow_id,)
)
#######################################
########### Particpants ###############
#######################################
async def create_participant(
wallet_id: str, data: CreateParticipantData, req: Request
) -> Participant:
participant_id = urlsafe_short_hash()
await db.execute(
"""
INSERT INTO raisenow.participants (id, name, raisenow, description, profile_image, total, lnaddress)
VALUES (?, ?, ?, ?, ?, ?, ?)
""",
(
participant_id,
data.name,
data.raisenow,
data.description,
data.profile_image,
0,
data.lnaddress,
),
)
participant = await get_participant(participant_id, req)
assert participant, "Newly created participant couldn't be retrieved"
return participant
async def get_participant(
participant_id: str, req: Optional[Request] = None
) -> Optional[Participant]:
row = await db.fetchone(
"SELECT * FROM raisenow.participants WHERE id = ?", (participant_id,)
)
if not row:
return None
rowAmended = Participant(**row)
if req:
rowAmended.lnurlpay = lnurl_encode(
req.url_for("raisenow.api_lnurl_pay", record_id=row.id)._url
)
return rowAmended
async def get_participants(
raisenow_id: str, req: Optional[Request] = None
) -> List[Participant]:
logger.debug(raisenow_id)
rows = await db.fetchall(
"""
SELECT * FROM raisenow.participants WHERE raisenow = ?
ORDER BY total DESC, name ASC
""", (raisenow_id,)
)
tempRows = [Participant(**row) for row in rows]
if req:
for row in tempRows:
row.lnurlpay = lnurl_encode(
req.url_for("raisenow.api_lnurl_pay", record_id=row.id)._url
)
return tempRows
async def update_participant(
participant_id: str, req: Optional[Request] = None, **kwargs
) -> Participant:
logger.debug(participant_id)
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
await db.execute(
f"UPDATE raisenow.participants SET {q} WHERE id = ?",
(*kwargs.values(), participant_id),
)
participant = await get_participant(participant_id, req)
assert participant, "Newly updated participant couldn't be retrieved"
return participant
async def delete_participant(participant_id: str) -> None:
await db.execute(
"DELETE FROM raisenow.participants WHERE id = ?", (participant_id,)
)