-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepos.py
More file actions
58 lines (52 loc) · 2.22 KB
/
repos.py
File metadata and controls
58 lines (52 loc) · 2.22 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
51
52
53
54
55
56
57
58
from typing import Optional, List, Tuple
from sqlalchemy import select, desc
from sqlalchemy.orm import Session
from database import get_session
from models import User, Income, Spend
class UserRepo:
@staticmethod
def get_by_email_password(email: str, password: str) -> Optional[Tuple[int, str, str, str]]:
with get_session() as db: # type: Session
user = db.execute(
select(User).where(User.email == email, User.password == password)
).scalar_one_or_none()
if not user:
return None
return (user.id, user.name, user.surname, user.email)
@staticmethod
def create(name: str, surname: str, email: str, password: str) -> int:
with get_session() as db:
u = User(name=name, surname=surname, email=email, password=password)
db.add(u)
db.flush()
return u.id
class IncomeRepo:
@staticmethod
def list_by_user(user_id: int) -> List[Tuple[int, float, str, str]]:
with get_session() as db:
rows = db.execute(
select(Income).where(Income.user_id == user_id).order_by(desc(Income.id))
).scalars().all()
return [(r.id, r.amount, r.description, r.date.isoformat() if r.date else "") for r in rows]
@staticmethod
def add(user_id: int, amount: float, description: str = "") -> int:
with get_session() as db:
item = Income(user_id=user_id, amount=amount, description=description)
db.add(item)
db.flush()
return item.id
class SpendRepo:
@staticmethod
def list_by_user(user_id: int) -> List[Tuple[int, float, str, str]]:
with get_session() as db:
rows = db.execute(
select(Spend).where(Spend.user_id == user_id).order_by(desc(Spend.id))
).scalars().all()
return [(r.id, r.amount, r.description, r.date.isoformat() if r.date else "") for r in rows]
@staticmethod
def add(user_id: int, amount: float, description: str = "") -> int:
with get_session() as db:
item = Spend(user_id=user_id, amount=amount, description=description)
db.add(item)
db.flush()
return item.id