-
Notifications
You must be signed in to change notification settings - Fork 0
/
services.py
78 lines (68 loc) · 2.06 KB
/
services.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
from database import get_db
from models import Expense, UserSettings
def add_expense(
description: str, category: str, amount: float, frequency: str, priority: int
) -> Expense:
db = get_db()
cursor = db.cursor()
cursor.execute(
"INSERT INTO expenses (description, category, amount, frequency, priority) VALUES (?, ?, ?, ?, ?)",
(description, category, amount, frequency, priority),
)
db.commit()
return Expense(
id=cursor.lastrowid,
description=description,
category=category,
amount=amount,
frequency=frequency,
priority=priority,
)
def list_expenses() -> list:
db = get_db()
cursor = db.cursor()
cursor.execute("SELECT * FROM expenses")
expenses = cursor.fetchall()
return [
Expense(
id=e["id"],
description=e["description"],
category=e["category"],
amount=e["amount"],
frequency=e["frequency"],
priority=e["priority"],
)
for e in expenses
]
def update_settings(
tax_rate: float, savings_rate: float, current_income: float, current_expenses: float
) -> UserSettings:
db = get_db()
cursor = db.cursor()
cursor.execute(
"""
REPLACE INTO user_settings (id, tax_rate, savings_rate, current_income, current_expenses)
VALUES (1, ?, ?, ?, ?)
""",
(tax_rate, savings_rate, current_income, current_expenses),
)
db.commit()
return UserSettings(
tax_rate=tax_rate,
savings_rate=savings_rate,
current_income=current_income,
current_expenses=current_expenses,
)
def get_settings() -> UserSettings:
db = get_db()
cursor = db.cursor()
cursor.execute("SELECT * FROM user_settings WHERE id = 1")
settings = cursor.fetchone()
if settings:
return UserSettings(**settings)
return None
def delete_expense(expense_id: int) -> None:
db = get_db()
cursor = db.cursor()
cursor.execute("DELETE FROM expenses WHERE id = ?", (expense_id,))
db.commit()