-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
50 lines (40 loc) · 1.31 KB
/
database.py
File metadata and controls
50 lines (40 loc) · 1.31 KB
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
from __future__ import annotations
from pathlib import Path
from derio.bootstrap import create_application
def setup_db(db_path: str | Path | None = None) -> None:
create_application(db_path=db_path)
def add_transaction(
type: str,
category: str,
amount: float,
date: str,
db_path: str | Path | None = None,
) -> int:
app = create_application(db_path=db_path)
transaction = app.transaction_service.add_transaction(
transaction_type=type,
category=category,
amount=amount,
transaction_date=date,
)
return int(transaction.id or 0)
def get_all_transactions(db_path: str | Path | None = None) -> list[tuple]:
app = create_application(db_path=db_path)
transactions = app.transaction_service.list_transactions()
return [
(
transaction.id,
transaction.transaction_type.value,
transaction.category,
transaction.amount,
transaction.transaction_date.isoformat(),
)
for transaction in transactions
]
def get_summary(db_path: str | Path | None = None) -> list[tuple]:
app = create_application(db_path=db_path)
summary_items = app.transaction_service.get_summary()
return [
(item.transaction_type.value, item.total)
for item in summary_items
]