-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcrud.py
54 lines (43 loc) · 1.58 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
from typing import Optional
from . import db
from .models import BotSettings, CreateBotSettings, UpdateBotSettings
async def get_discordbot_settings(admin_id: str) -> Optional[BotSettings]:
row = await db.fetchone(
"SELECT * FROM discordbot.bots WHERE admin = ?", (admin_id,)
)
return BotSettings(**row) if row else None
async def get_all_discordbot_settings() -> list[BotSettings]:
rows = await db.fetchall("SELECT * FROM discordbot.bots")
return [BotSettings(**row) for row in rows]
async def create_discordbot_settings(data: CreateBotSettings, admin_id: str):
await db.execute(
f"""
INSERT INTO discordbot.bots (admin, token, standalone)
VALUES (?, ?, ?)
ON CONFLICT (admin) DO
UPDATE SET token = '{data.token}'
""",
(admin_id, data.token, data.standalone),
)
return await get_discordbot_settings(admin_id)
async def update_discordbot_settings(data: UpdateBotSettings, admin_id: str):
updates = []
values = []
for key, val in data.dict(exclude_unset=True).items():
updates.append(f"{key} = ?")
values.append(val)
values.append(admin_id)
await db.execute(
f"""
UPDATE discordbot.bots
SET {", ".join(updates)}
WHERE admin = ?
""",
values,
)
return await get_discordbot_settings(admin_id)
async def delete_discordbot_settings(admin_id: str):
result = await db.execute(
"DELETE FROM discordbot.bots WHERE admin = ?", (admin_id,)
)
assert result.rowcount == 1, "Could not create settings"