diff --git a/src/services/__init__.py b/src/services/__init__.py index 3cbcb77..0bd39d3 100644 --- a/src/services/__init__.py +++ b/src/services/__init__.py @@ -4,8 +4,6 @@ update_expense, delete_expense, read_expenses, - read_expenses_by_group_month, - read_expenses_by_group_time_range, ) from .user import ( get_user, diff --git a/src/services/expense.py b/src/services/expense.py index 98c5e03..3a63d03 100644 --- a/src/services/expense.py +++ b/src/services/expense.py @@ -125,7 +125,7 @@ def read_expenses( status_code=status.HTTP_404_NOT_FOUND, detail="Too many arguments! It is necessary to select either a month or a start date and an end date!", ) - if group_id is not None: + if group_id: try: db.query(UserGroup).filter_by( group_id=group_id, @@ -136,87 +136,28 @@ def read_expenses( status_code=status.HTTP_404_NOT_FOUND, detail="You are not a user of this group!", ) - expenses = read_expenses_by_group_all_time(group_id, user_id) - if filter_date: - expenses = read_expenses_by_group_month(group_id, user_id, filter_date) - elif start_date and end_date: - expenses = read_expenses_by_group_time_range( - group_id, user_id, start_date, end_date - ) - return expenses - expenses = read_expenses_all_time(user_id) - if filter_date: - expenses = read_expenses_month(user_id, filter_date) - elif start_date and end_date: - expenses = read_expenses_time_range(user_id, start_date, end_date) - return expenses - - -def read_expenses_by_group_all_time(group_id: int, user_id: int) -> List[UserExpense]: - expenses = select(Expense).filter_by(user_id=user_id, group_id=group_id) - return expenses - - -def read_expenses_by_group_month( - group_id: int, user_id: int, filter_date: date -) -> List[UserExpense]: - expenses = select(Expense).filter( - and_( - Expense.user_id == user_id, - Expense.group_id == group_id, - extract("year", Expense.time) == filter_date.year, - extract("month", Expense.time) == filter_date.month, + expenses = select(Expense).filter_by(user_id=user_id, group_id=group_id) + else: + expenses = select(Expense).filter_by( + user_id=user_id, ) - ) - return expenses - - -def read_expenses_by_group_time_range( - group_id: int, user_id: int, start_date: date, end_date: date -) -> List[UserExpense]: - if start_date > end_date: - raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, - detail="The start date cannot be older than the end date!", + if filter_date: + expenses = expenses.filter( + and_( + Expense.user_id == user_id, + extract("year", Expense.time) == filter_date.year, + extract("month", Expense.time) == filter_date.month, + ) ) - expenses = select(Expense).filter( - Expense.user_id == user_id, - Expense.group_id == group_id, - Expense.time >= start_date, - Expense.time <= end_date, - ) - return expenses - - -def read_expenses_all_time(user_id: int) -> List[UserExpense]: - expenses = select(Expense).filter_by( - user_id=user_id, - ) - return expenses - - -def read_expenses_month(user_id: int, filter_date: date) -> List[UserExpense]: - expenses = select(Expense).filter( - and_( + elif start_date and end_date: + if start_date > end_date: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail="The start date cannot be older than the end date!", + ) + expenses = expenses.filter( Expense.user_id == user_id, - extract("year", Expense.time) == filter_date.year, - extract("month", Expense.time) == filter_date.month, - ) - ) - return expenses - - -def read_expenses_time_range( - user_id: int, start_date: date, end_date: date -) -> List[UserExpense]: - if start_date > end_date: - raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, - detail="The start date cannot be older than the end date!", + Expense.time >= start_date, + Expense.time <= end_date, ) - expenses = select(Expense).filter( - Expense.user_id == user_id, - Expense.time >= start_date, - Expense.time <= end_date, - ) return expenses diff --git a/src/services/replenishment.py b/src/services/replenishment.py index be59cc0..39344bf 100644 --- a/src/services/replenishment.py +++ b/src/services/replenishment.py @@ -79,45 +79,26 @@ def read_replenishments( start_date: Optional[date] = None, end_date: Optional[date] = None, ) -> List[UserReplenishment]: - replenishments = read_replenishments_all_time(user_id) - if filter_date: - replenishments = read_replenishments_month(user_id, filter_date) - elif start_date and end_date: - replenishments = read_replenishments_time_range(user_id, start_date, end_date) - return replenishments - - -def read_replenishments_all_time(user_id: int) -> List[UserReplenishment]: replenishments = select(Replenishment).filter_by( user_id=user_id, ) - return replenishments - - -def read_replenishments_month( - user_id: int, filter_date: date -) -> List[UserReplenishment]: - replenishments = select(Replenishment).filter( - and_( - Replenishment.user_id == user_id, - extract("year", Replenishment.time) == filter_date.year, - extract("month", Replenishment.time) == filter_date.month, + if filter_date: + replenishments = replenishments.filter( + and_( + Replenishment.user_id == user_id, + extract("year", Replenishment.time) == filter_date.year, + extract("month", Replenishment.time) == filter_date.month, + ) ) - ) - return replenishments - - -def read_replenishments_time_range( - user_id: int, start_date: date, end_date: date -) -> List[UserReplenishment]: - if start_date > end_date: - raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, - detail="The start date cannot be older than the end date!", + elif start_date and end_date: + if start_date > end_date: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail="The start date cannot be older than the end date!", + ) + replenishments = replenishments.filter( + Replenishment.user_id == user_id, + Replenishment.time >= start_date, + Replenishment.time <= end_date, ) - replenishments = select(Replenishment).filter( - Replenishment.user_id == user_id, - Replenishment.time >= start_date, - Replenishment.time <= end_date, - ) return replenishments