Skip to content

Commit

Permalink
ref: improve expenses and replenishments fucn
Browse files Browse the repository at this point in the history
  • Loading branch information
RezenkovD committed Sep 6, 2023
1 parent fa488cb commit 6631870
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 118 deletions.
2 changes: 0 additions & 2 deletions src/services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
101 changes: 21 additions & 80 deletions src/services/expense.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
53 changes: 17 additions & 36 deletions src/services/replenishment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 6631870

Please sign in to comment.