|
| 1 | +from collections.abc import Sequence |
| 2 | +from decimal import Decimal |
| 3 | + |
| 4 | +from sqlmodel import Integer, Session, cast, select |
| 5 | + |
| 6 | +from ..schemas.account import Account, AccountBase, AccountCreate, AccountUpdate |
| 7 | + |
| 8 | + |
| 9 | +def create_account(session: Session, account: AccountCreate) -> AccountBase: |
| 10 | + session.add(account) |
| 11 | + session.commit() |
| 12 | + session.refresh(account) |
| 13 | + return account |
| 14 | + |
| 15 | + |
| 16 | +def read_account(session: Session, account_id: int) -> Account | None: |
| 17 | + return session.get(Account, account_id) |
| 18 | + |
| 19 | + |
| 20 | +def read_accounts( |
| 21 | + session: Session, account_ids: list[int] | None = None, for_update: bool = False |
| 22 | +) -> Sequence[Account]: |
| 23 | + statement = select(Account) |
| 24 | + if account_ids: |
| 25 | + statement = statement.where(cast(Account.id, Integer).in_(account_ids)) |
| 26 | + if for_update: |
| 27 | + statement = statement.with_for_update() |
| 28 | + return session.exec(statement).all() |
| 29 | + |
| 30 | + |
| 31 | +def update_accounts( |
| 32 | + session: Session, account_ids: list[int], accounts: list[AccountUpdate] |
| 33 | +) -> Sequence[Account]: |
| 34 | + # Verify account_ids and accounts have the same length |
| 35 | + if len(account_ids) != len(accounts): |
| 36 | + raise ValueError("account_ids and accounts must have the same length") |
| 37 | + |
| 38 | + # Verify all accounts are valid |
| 39 | + db_accounts = _verify_account_ids(session, account_ids) |
| 40 | + |
| 41 | + # Update accounts |
| 42 | + for db_account, account in zip(db_accounts, accounts, strict=True): |
| 43 | + account_data = account.model_dump(exclude_unset=True) |
| 44 | + db_account.sqlmodel_update(account_data) |
| 45 | + |
| 46 | + session.add_all(db_accounts) |
| 47 | + session.commit() |
| 48 | + |
| 49 | + return read_accounts(session, account_ids) |
| 50 | + |
| 51 | + |
| 52 | +def update_account_balances( |
| 53 | + session: Session, |
| 54 | + account_amounts: dict[int, Decimal], |
| 55 | + commit: bool = True, |
| 56 | +) -> Sequence[Account]: |
| 57 | + account_ids = list(account_amounts.keys()) |
| 58 | + db_accounts = _verify_account_ids(session, account_ids) |
| 59 | + id_to_index = {account.id: index for index, account in enumerate(db_accounts)} |
| 60 | + for account_id, amount in account_amounts.items(): |
| 61 | + db_account = db_accounts[id_to_index[account_id]] |
| 62 | + db_account.balance += amount |
| 63 | + |
| 64 | + session.add_all(db_accounts) |
| 65 | + if commit: |
| 66 | + session.commit() |
| 67 | + for account in db_accounts: |
| 68 | + session.refresh(account) |
| 69 | + else: |
| 70 | + session.flush() |
| 71 | + |
| 72 | + return read_accounts(session, account_ids) |
| 73 | + |
| 74 | + |
| 75 | +def _verify_account_ids(session: Session, account_ids: list[int]) -> Sequence[Account]: |
| 76 | + db_accounts = read_accounts(session, account_ids, for_update=True) |
| 77 | + missing_ids = set(account_ids) - {account.id for account in db_accounts} |
| 78 | + if missing_ids: |
| 79 | + raise ValueError(f"Account id(s) not found: {missing_ids}") |
| 80 | + |
| 81 | + return db_accounts |
0 commit comments