Skip to content

Commit

Permalink
chore:update endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jingooo5 committed Nov 2, 2024
1 parent 1eb2c98 commit 8bf19fc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
8 changes: 6 additions & 2 deletions Router/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ def get_userItem_all(request : Request, user_id: str, category: str = Query(...)
user = UserService.get_user_by_id(user_id)
if user is None:
return JSONResponse(status_code=HTTP_500_INTERNAL_SERVER_ERROR, content={"message": "User not found"})
user_item_list = UserItemService.get_all_userItem_filtered_by_category(user_id, category)
user_item_list_dict = UserItemService.to_userItem_dict(user_item_list, category)
if category == "ALL":
user_item_list = UserItemService.get_all_userItem(user_id)
user_item_list_dict = UserItemService.to_userItem_dict(user_item_list)
else:
user_item_list = UserItemService.get_all_userItem_filtered_by_category(user_id, category)
user_item_list_dict = UserItemService.to_userItem_dict_with_category(user_item_list, category)
return JSONResponse(status_code=HTTP_200_OK, content={"items": user_item_list_dict})


Expand Down
18 changes: 17 additions & 1 deletion Service/useritem_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def get_all_userItem_filtered_by_date(user_id, year: int, month: int):
extract("month", UserItem.consume_date) == month).all()


def to_userItem_dict(userItemList: List[UserItem], category: str):
def to_userItem_dict_with_category(userItemList: List[UserItem], category: str):
itemlist = []

for userItem in userItemList:
Expand All @@ -51,6 +51,22 @@ def to_userItem_dict(userItemList: List[UserItem], category: str):
})
return itemlist

@staticmethod
def to_userItem_dict(userItemList: List[UserItem]):
itemlist = []

with get_db() as db:
for userItem in userItemList:
item = db.query(Item).filter(Item.item_name == userItem.item_name).first()
itemlist.append({
"user_id": userItem.user_id,
"item_name": userItem.item_name,
"count": userItem.count,
"category": item.item_category,
"consume_date": userItem.consume_date.strftime("%Y-%m-%d") if userItem.consume_date is not None else None,
})
return itemlist

def add_userItem(itemAdd: UserItemAdd):
with get_db() as db:
useritem = db.query(UserItem).filter(UserItem.user_id == itemAdd.user_id, UserItem.item_name == itemAdd.item_name).first()
Expand Down

0 comments on commit 8bf19fc

Please sign in to comment.