From 778bdc2c7487629a4afd41e435270f6313da7ad5 Mon Sep 17 00:00:00 2001 From: ewandor Date: Sun, 2 Feb 2025 22:09:00 +0100 Subject: [PATCH] Adding family concept and routes --- api/app/account/account_routes.py | 8 ++++++ api/app/account/category_routes.py | 10 ++++++- api/app/account/models.py | 44 ++++++++++++++++++++++++------ 3 files changed, 53 insertions(+), 9 deletions(-) diff --git a/api/app/account/account_routes.py b/api/app/account/account_routes.py index c2b86f9..ab3a998 100644 --- a/api/app/account/account_routes.py +++ b/api/app/account/account_routes.py @@ -19,6 +19,14 @@ def create_account(account: AccountCreate, session: SessionDep, current_user=Dep def read_accounts(session: SessionDep, current_user=Depends(get_current_user)) -> Page[AccountRead]: return paginate(session, Account.list_accounts()) +@router.get("") +def read_assets(session: SessionDep, current_user=Depends(get_current_user)) -> Page[AccountRead]: + return paginate(session, Account.list_assets()) + +@router.get("") +def read_liabilities(session: SessionDep, current_user=Depends(get_current_user)) -> Page[AccountRead]: + return paginate(session, Account.list_liabilities()) + @router.get("/{account_id}") def read_account(account_id: UUID, session: SessionDep, current_user=Depends(get_current_user)) -> AccountRead: account = Account.get(session, account_id) diff --git a/api/app/account/category_routes.py b/api/app/account/category_routes.py index 8913fe8..2c3c7b7 100644 --- a/api/app/account/category_routes.py +++ b/api/app/account/category_routes.py @@ -16,9 +16,17 @@ def create_category(category: CategoryCreate, session: SessionDep, current_user= return result @router.get("") -def read_categorys(session: SessionDep, current_user=Depends(get_current_user)) -> Page[AccountRead]: +def read_categories(session: SessionDep, current_user=Depends(get_current_user)) -> Page[AccountRead]: return paginate(session, Account.list_categories()) +@router.get("expenses") +def read_expenses(session: SessionDep, current_user=Depends(get_current_user)) -> Page[AccountRead]: + return paginate(session, Account.list_expenses()) + +@router.get("incomes") +def read_incomes(session: SessionDep, current_user=Depends(get_current_user)) -> Page[AccountRead]: + return paginate(session, Account.list_incomes()) + @router.get("/{category_id}") def read_category(category_id: UUID, session: SessionDep, current_user=Depends(get_current_user)) -> AccountRead: category = Account.get(session, category_id) diff --git a/api/app/account/models.py b/api/app/account/models.py index b855d0c..6096d42 100644 --- a/api/app/account/models.py +++ b/api/app/account/models.py @@ -14,7 +14,8 @@ class AccountBase(SQLModel): parent_account_id: Optional[UUID] = Field(default=None, foreign_key="account.id") class AccountBaseId(AccountBase): - id: UUID | None = Field(default=uuid4(), primary_key=True) + id: UUID | None = Field(default_factory=uuid4, primary_key=True) + family: str = Field(index=True) type: str = Field(index=True) path: str = Field(index=True) @@ -30,7 +31,7 @@ class Account(AccountBaseId, table=True): def get_root_path(self): root = "/Categories" if self.is_category() else "/Accounts" - return f"{root}/{self.type}/" + return f"{root}/{self.family}/" def update_children_path(self, session, old_path): request = f"UPDATE {self.__tablename__} SET path=REPLACE(path, '{old_path}', '{self.path}') WHERE path LIKE '{old_path}{self.name }/%'" @@ -46,18 +47,26 @@ class Account(AccountBaseId, table=True): parent = self.get(session, self.parent_account_id) return parent.get_child_path() + def get_family(self): + if self.type in Asset: + return "Asset" + if self.type in Liability: + return "Liability" + return self.type + @classmethod def schema_to_model(cls, session, schema, model=None): - try: if model: model = cls.model_validate(model, update=schema) else: - schema['path'] = "" + schema.path = "" + schema.family = "" model = cls.model_validate(schema) except Exception as e: print(e) + model.family = model.get_family() model.path = model.get_path(session) return model @@ -80,12 +89,28 @@ class Account(AccountBaseId, table=True): Account.type.not_in([v.value for v in CategoryType]) ) + @classmethod + def list_assets(cls): + return cls.list().where(Account.family == "Asset") + + @classmethod + def list_liabilities(cls): + return cls.list().where(Account.family == "Liability") + @classmethod def list_categories(cls): return cls.list().where( Account.type.in_([v.value for v in CategoryType]) ) + @classmethod + def list_expenses(cls): + return cls.list().where(Account.family == "Expense") + + @classmethod + def list_incomes(cls): + return cls.list().where(Account.family == "Income") + @classmethod def get(cls, session, account_id): return session.get(Account, account_id) @@ -94,7 +119,7 @@ class Account(AccountBaseId, table=True): def update(cls, session, account_db, account_data): previous_path = account_db.path account_db.sqlmodel_update(cls.schema_to_model(session, account_data, account_db)) - if previous_path != account_db.path: + if previous_path != account_db.path or account_data['name'] != account_db.name: account_db.update_children_path(session, previous_path) session.add(account_db) session.commit() @@ -142,9 +167,12 @@ class Liability(Enum): CreditCard = "CreditCard" Loan = "Loan" -class AccountWrite(AccountBase): - type: Asset | Liability = Field() +class BaseAccountWrite(AccountBase): path: SkipJsonSchema[str] = Field(default="") + family: SkipJsonSchema[str] = Field(default="") + +class AccountWrite(BaseAccountWrite): + type: Asset | Liability = Field() parent_account_id: UUID | None = PydField(default=None, json_schema_extra={ "foreign_key": { "reference": { @@ -165,7 +193,7 @@ class CategoryType(Enum): Income = "Income" Expense = "Expense" -class CategoryWrite(AccountBase): +class CategoryWrite(BaseAccountWrite): type: CategoryType = Field() class CategoryCreate(CategoryWrite):