Skip to content

Commit

Permalink
chore:fix error
Browse files Browse the repository at this point in the history
  • Loading branch information
jingooo5 committed Nov 2, 2024
1 parent 7b925e6 commit 7945ee5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Router/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from fastapi import APIRouter
from starlette.status import *
from fastapi import Query, Request
from Data.item import Item, ItemAdd, UserItemAdd, UserItemConsume
from Data.item import Item, ItemAdd, UserItemAdd, UserItemConsume, ItemRead
from Database.models import User
from Service.purchase_service import PurchaseService
from Service.useritem_service import UserItemService
Expand All @@ -26,7 +26,7 @@ def add_items(request: Request, itemadd: ItemAdd):
"""
유저가 보유한 모든 아이템을 가져온다.
"""
@router.get("/{user_id}/item", response_model=List[Item])
@router.get("/{user_id}/item", response_model=List[ItemRead])
def get_userItem_all(request : Request, user_id: str):
user_item_list = UserItemService.get_all_userItem(user_id)
user_item_list_dict = UserItemService.to_userItem_dict(user_item_list)
Expand Down
7 changes: 6 additions & 1 deletion Service/item_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ class ItemService:
@staticmethod
def get_all():
with get_db() as db:
return db.query(Item).all()
return db.query(Item).all()

@staticmethod
def get_item(item_name: str):
with get_db() as db:
return db.query(Item).filter(Item.item_name == item_name).first()
24 changes: 15 additions & 9 deletions Service/useritem_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from Data.item import UserItemAdd, ItemAdd
from Database.database import get_db
from Database.models import UserItem
from Database.models import UserItem, Item
from .item_service import ItemService

"""
Expand All @@ -27,15 +27,21 @@ def init_userItem(user_id: str):
def get_all_userItem(user_id: str):
with get_db() as db:
return db.query(UserItem).filter(UserItem.user_id == user_id).all()



def to_userItem_dict(userItemList: List[UserItem]):
return [{
"user_id": userItem.user_id,
"item_name": userItem.item_name,
"count": userItem.count,
"consume_date": userItem.consume_date,
"consume_expectation": userItem.consume_expectation
} for userItem in userItemList]
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:
Expand Down

0 comments on commit 7945ee5

Please sign in to comment.