Skip to content

Commit

Permalink
refactor: improve code comprehension
Browse files Browse the repository at this point in the history
* flake8: f-string is missing placeholders

* rename: services and routers

* ref: improve expenses and replenishments fucn

* fix: eternal pending in invitations

* ref: improve queries to db

* ref: update queries and validate input
  • Loading branch information
RezenkovD authored Sep 6, 2023
1 parent c1c7a6e commit 22c83de
Show file tree
Hide file tree
Showing 16 changed files with 297 additions and 767 deletions.
56 changes: 28 additions & 28 deletions src/routers/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def read_group_history(
current_user: User = Depends(get_current_user),
group_id: int,
) -> Page[GroupHistory]:
return paginate(db, services.group_history(db, current_user.id, group_id))
return paginate(db, services.read_group_history(db, current_user.id, group_id))


@router.get("/{group_id}/info/", response_model=GroupInfo)
Expand Down Expand Up @@ -141,17 +141,17 @@ def read_group_total_expenses(
)
elif year_month:
filter_date = transform_date_or_422(year_month)
return services.group_total_expenses(
return services.read_group_total_expenses(
db, current_user.id, group_id, filter_date=filter_date
)
elif start_date and end_date:
start_date = transform_exact_date_or_422(start_date)
end_date = transform_exact_date_or_422(end_date)
return services.group_total_expenses(
return services.read_group_total_expenses(
db, current_user.id, group_id, start_date=start_date, end_date=end_date
)
else:
return services.group_total_expenses(db, current_user.id, group_id)
return services.read_group_total_expenses(db, current_user.id, group_id)


@router.get("/{group_id}/my-total-expenses/", response_model=GroupUserTotalExpenses)
Expand All @@ -176,17 +176,17 @@ def read_group_user_total_expenses(
)
elif year_month:
filter_date = transform_date_or_422(year_month)
return services.group_user_total_expenses(
return services.read_group_user_total_expenses(
db, current_user.id, group_id, filter_date=filter_date
)
elif start_date and end_date:
start_date = transform_exact_date_or_422(start_date)
end_date = transform_exact_date_or_422(end_date)
return services.group_user_total_expenses(
return services.read_group_user_total_expenses(
db, current_user.id, group_id, start_date=start_date, end_date=end_date
)
else:
return services.group_user_total_expenses(db, current_user.id, group_id)
return services.read_group_user_total_expenses(db, current_user.id, group_id)


@router.get("/{group_id}/users-spenders/", response_model=List[UserSpender])
Expand All @@ -211,17 +211,17 @@ def read_group_users_spenders(
)
elif year_month:
filter_date = transform_date_or_422(year_month)
return services.group_users_spenders(
return services.read_group_users_spenders(
db, current_user.id, group_id, filter_date=filter_date
)
elif start_date and end_date:
start_date = transform_exact_date_or_422(start_date)
end_date = transform_exact_date_or_422(end_date)
return services.group_users_spenders(
return services.read_group_users_spenders(
db, current_user.id, group_id, start_date=start_date, end_date=end_date
)
else:
return services.group_users_spenders(db, current_user.id, group_id)
return services.read_group_users_spenders(db, current_user.id, group_id)


@router.get("/{group_id}/category-expenses/", response_model=List[CategoryExpenses])
Expand All @@ -246,17 +246,17 @@ def read_group_category_expenses(
)
elif year_month:
filter_date = transform_date_or_422(year_month)
return services.group_category_expenses(
return services.read_group_category_expenses(
db, current_user.id, group_id, filter_date=filter_date
)
elif start_date and end_date:
start_date = transform_exact_date_or_422(start_date)
end_date = transform_exact_date_or_422(end_date)
return services.group_category_expenses(
return services.read_group_category_expenses(
db, current_user.id, group_id, start_date=start_date, end_date=end_date
)
else:
return services.group_category_expenses(db, current_user.id, group_id)
return services.read_group_category_expenses(db, current_user.id, group_id)


@router.get(
Expand Down Expand Up @@ -368,13 +368,13 @@ def read_group_member_info(
)
elif year_month:
filter_date = transform_date_or_422(year_month)
return services.group_member_info(
return services.read_group_member_info(
db, current_user.id, group_id, member_id, filter_date=filter_date
)
elif start_date and end_date:
start_date = transform_exact_date_or_422(start_date)
end_date = transform_exact_date_or_422(end_date)
return services.group_member_info(
return services.read_group_member_info(
db,
current_user.id,
group_id,
Expand All @@ -383,14 +383,14 @@ def read_group_member_info(
end_date=end_date,
)
else:
return services.group_member_info(db, current_user.id, group_id, member_id)
return services.read_group_member_info(db, current_user.id, group_id, member_id)


@router.get(
"/{group_id}/member/{member_id}/category-expenses/",
response_model=List[CategoryExpenses],
)
def read_group_category_expenses(
def read_group_member_category_expenses(
*,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user),
Expand All @@ -412,13 +412,13 @@ def read_group_category_expenses(
)
elif year_month:
filter_date = transform_date_or_422(year_month)
return services.group_member_category_expenses(
return services.read_group_member_category_expenses(
db, current_user.id, group_id, member_id, filter_date=filter_date
)
elif start_date and end_date:
start_date = transform_exact_date_or_422(start_date)
end_date = transform_exact_date_or_422(end_date)
return services.group_member_category_expenses(
return services.read_group_member_category_expenses(
db,
current_user.id,
group_id,
Expand All @@ -427,7 +427,7 @@ def read_group_category_expenses(
end_date=end_date,
)
else:
return services.group_member_category_expenses(
return services.read_group_member_category_expenses(
db, current_user.id, group_id, member_id
)

Expand Down Expand Up @@ -458,13 +458,13 @@ def read_group_member_daily_expenses(
)
elif year_month:
filter_date = transform_date_or_422(year_month)
return services.group_member_daily_expenses(
return services.read_group_member_daily_expenses(
db, current_user.id, group_id, member_id, filter_date=filter_date
)
elif start_date and end_date:
start_date = transform_exact_date_or_422(start_date)
end_date = transform_exact_date_or_422(end_date)
return services.group_member_daily_expenses(
return services.read_group_member_daily_expenses(
db,
current_user.id,
group_id,
Expand All @@ -473,7 +473,7 @@ def read_group_member_daily_expenses(
end_date=end_date,
)
else:
return services.group_member_daily_expenses(
return services.read_group_member_daily_expenses(
db, current_user.id, group_id, member_id
)

Expand Down Expand Up @@ -504,13 +504,13 @@ def read_group_member_daily_expenses_detail(
)
elif year_month:
filter_date = transform_date_or_422(year_month)
return services.group_member_daily_expenses_detail(
return services.read_group_member_daily_expenses_detail(
db, current_user.id, group_id, member_id, filter_date=filter_date
)
elif start_date and end_date:
start_date = transform_exact_date_or_422(start_date)
end_date = transform_exact_date_or_422(end_date)
return services.group_member_daily_expenses_detail(
return services.read_group_member_daily_expenses_detail(
db,
current_user.id,
group_id,
Expand All @@ -519,21 +519,21 @@ def read_group_member_daily_expenses_detail(
end_date=end_date,
)
else:
return services.group_member_daily_expenses_detail(
return services.read_group_member_daily_expenses_detail(
db, current_user.id, group_id, member_id
)


@router.get(
"/{group_id}/member/{member_id}/history/", response_model=Page[GroupHistory]
)
def read_group_history(
def read_group_member_history(
*,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user),
group_id: int,
member_id: int,
) -> Page[GroupHistory]:
return paginate(
db, services.group_member_history(db, current_user.id, group_id, member_id)
db, services.read_group_member_history(db, current_user.id, group_id, member_id)
)
2 changes: 1 addition & 1 deletion src/routers/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async def auth(*, db: Session = Depends(get_db), request: Request):
except:
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail=f"An error occurred while create category",
detail="An error occurred while create category",
)
return RedirectResponse(url="https://" + settings.DOMAIN_NAME)

Expand Down
16 changes: 8 additions & 8 deletions src/routers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def read_user_balance(
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user),
) -> UserBalance:
return services.calculate_user_balance(db, current_user.id)
return services.read_user_balance(db, current_user.id)


@router.get("/info/", response_model=UserModel)
Expand All @@ -65,7 +65,7 @@ def read_user_history(
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user),
) -> Page[UserHistory]:
return paginate(db, services.user_history(current_user.id))
return paginate(db, services.read_user_history(current_user.id))


@router.get("/{group_id}/expenses/", response_model=UserGroupExpenses)
Expand Down Expand Up @@ -199,20 +199,20 @@ def read_user_total_expenses(
)
elif year_month:
filter_date = transform_date_or_422(year_month)
return services.user_total_expenses(
return services.read_user_total_expenses(
db, current_user.id, filter_date=filter_date
)
elif start_date and end_date:
start_date = transform_exact_date_or_422(start_date)
end_date = transform_exact_date_or_422(end_date)
return services.user_total_expenses(
return services.read_user_total_expenses(
db,
current_user.id,
start_date=start_date,
end_date=end_date,
)
else:
return services.user_total_expenses(db, current_user.id)
return services.read_user_total_expenses(db, current_user.id)


@router.get("/total-replenishments/", response_model=UserTotalReplenishments)
Expand All @@ -235,17 +235,17 @@ def read_user_total_replenishments(
)
elif year_month:
filter_date = transform_date_or_422(year_month)
return services.user_total_replenishments(
return services.read_user_total_replenishments(
db, current_user.id, filter_date=filter_date
)
elif start_date and end_date:
start_date = transform_exact_date_or_422(start_date)
end_date = transform_exact_date_or_422(end_date)
return services.user_total_replenishments(
return services.read_user_total_replenishments(
db,
current_user.id,
start_date=start_date,
end_date=end_date,
)
else:
return services.user_total_replenishments(db, current_user.id)
return services.read_user_total_replenishments(db, current_user.id)
32 changes: 15 additions & 17 deletions src/services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
update_expense,
delete_expense,
read_expenses,
read_expenses_by_group_month,
read_expenses_by_group_time_range,
)
from .user import (
get_user,
calculate_user_balance,
user_total_expenses,
user_total_replenishments,
user_history,
read_user_balance,
read_user_total_expenses,
read_user_total_replenishments,
read_user_history,
read_user_daily_expenses,
read_category_expenses,
read_group_expenses,
Expand All @@ -23,23 +21,23 @@
update_group,
disband_group,
leave_group,
remove_user,
read_categories_group,
read_user_groups,
read_users_group,
remove_user,
read_group_info,
group_history,
group_total_expenses,
group_user_total_expenses,
group_users_spenders,
group_category_expenses,
read_group_history,
read_group_total_expenses,
read_group_user_total_expenses,
read_group_users_spenders,
read_group_category_expenses,
read_group_daily_expenses,
read_group_daily_expenses_detail,
group_member_info,
group_member_category_expenses,
group_member_daily_expenses,
group_member_daily_expenses_detail,
group_member_history,
read_group_member_info,
read_group_member_category_expenses,
read_group_member_daily_expenses,
read_group_member_daily_expenses_detail,
read_group_member_history,
)
from .invitation import create_invitation, read_invitations, response_invitation
from .replenishment import (
Expand Down
4 changes: 2 additions & 2 deletions src/services/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def create_category(
except:
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail=f"An error occurred while create category",
detail="An error occurred while create category",
)
else:
return db_category
Expand Down Expand Up @@ -104,7 +104,7 @@ def update_category(
except:
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail=f"An error occurred while update category",
detail="An error occurred while update category",
)
else:
return db_category_group
Loading

0 comments on commit 22c83de

Please sign in to comment.