-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcrud.py
167 lines (131 loc) · 4.71 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
from datetime import datetime, timedelta
from typing import Optional, Union
from lnbits.db import Database
from lnbits.helpers import urlsafe_short_hash
from .models import (
Appointment,
CreateAppointment,
CreateSchedule,
CreateUnavailableTime,
Schedule,
UnavailableTime,
)
db = Database("ext_lncalendar")
async def create_schedule(wallet_id: str, data: CreateSchedule) -> Schedule:
schedule_id = urlsafe_short_hash()
schedule = Schedule(
id=schedule_id,
wallet=wallet_id,
name=data.name,
start_day=data.start_day,
end_day=data.end_day,
start_time=data.start_time,
end_time=data.end_time,
amount=data.amount,
)
await db.insert("lncalendar.schedule", schedule)
return schedule
async def update_schedule(schedule: Schedule) -> Schedule:
await db.update("lncalendar.schedule", schedule)
return schedule
async def get_schedule(schedule_id: str) -> Optional[Schedule]:
return await db.fetchone(
"SELECT * FROM lncalendar.schedule WHERE id = :id",
{"id": schedule_id},
Schedule,
)
async def get_schedules(wallet_ids: Union[str, list[str]]) -> list[Schedule]:
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]
q = ",".join([f"'{wallet_id}'" for wallet_id in wallet_ids])
return await db.fetchall(
f"SELECT * FROM lncalendar.schedule WHERE wallet IN ({q})", model=Schedule
)
async def delete_schedule(schedule_id: str) -> None:
await db.execute(
"DELETE FROM lncalendar.schedule WHERE id = :id", {"id": schedule_id}
)
async def create_appointment(
schedule_id: str, payment_hash: str, data: CreateAppointment
) -> Appointment:
appointment_id = payment_hash
appointment = Appointment(
id=appointment_id,
name=data.name,
email=data.email,
info=data.info,
start_time=data.start_time,
end_time=data.end_time,
schedule=schedule_id,
paid=False,
)
await db.insert("lncalendar.appointment", appointment)
return appointment
async def get_appointment(appointment_id: str) -> Optional[Appointment]:
return await db.fetchone(
"SELECT * FROM lncalendar.appointment WHERE id = :id",
{"id": appointment_id},
Appointment,
)
async def get_appointments(schedule_id: str) -> list[Appointment]:
return await db.fetchall(
"SELECT * FROM lncalendar.appointment WHERE schedule = :schedule",
{"schedule": schedule_id},
Appointment,
)
async def get_appointments_wallets(
wallet_ids: Union[str, list[str]]
) -> list[Appointment]:
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]
schedules = await get_schedules(wallet_ids)
if not schedules:
return []
schedule_ids = [schedule.id for schedule in schedules]
q = ",".join([f"'{schedule_id}'" for schedule_id in schedule_ids])
return await db.fetchall(
f"SELECT * FROM lncalendar.appointment WHERE schedule IN ({q})",
model=Appointment,
)
async def set_appointment_paid(appointment_id: str) -> None:
await db.execute(
"UPDATE lncalendar.appointment SET paid = true WHERE id = :id",
{"id": appointment_id},
)
async def purge_appointments(schedule_id: str) -> None:
time_diff = datetime.now() - timedelta(hours=24)
tsph = db.timestamp_placeholder("diff")
await db.execute(
f"""
DELETE FROM lncalendar.appointment
WHERE schedule = :schedule AND paid = false AND time < {tsph}
""",
{"schedule": schedule_id, "diff": time_diff.timestamp()},
)
## UnavailableTime CRUD
async def create_unavailable_time(data: CreateUnavailableTime) -> UnavailableTime:
unavailable_time_id = urlsafe_short_hash()
unavailable_time = UnavailableTime(
id=unavailable_time_id,
start_time=data.start_time,
end_time=data.end_time or data.start_time,
schedule=data.schedule,
)
await db.insert("lncalendar.unavailable", unavailable_time)
return unavailable_time
async def get_unavailable_time(unavailable_time_id: str) -> Optional[UnavailableTime]:
return await db.fetchone(
"SELECT * FROM lncalendar.unavailable WHERE id = :id",
{"id": unavailable_time_id},
UnavailableTime,
)
async def get_unavailable_times(schedule_id: str) -> list[UnavailableTime]:
return await db.fetchall(
"SELECT * FROM lncalendar.unavailable WHERE schedule = :schedule",
{"schedule": schedule_id},
UnavailableTime,
)
async def delete_unavailable_time(unavailable_time_id: str) -> None:
await db.execute(
"DELETE FROM lncalendar.unavailable WHERE id = :id", {"id": unavailable_time_id}
)