From 7945ee5954e50cd74ad4175520f30b6873e0746d Mon Sep 17 00:00:00 2001 From: jingooo5 Date: Sun, 3 Nov 2024 01:05:09 +0900 Subject: [PATCH] chore:fix error --- Router/item.py | 4 ++-- Service/item_service.py | 7 ++++++- Service/useritem_service.py | 24 +++++++++++++++--------- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/Router/item.py b/Router/item.py index 918fe20..a215d54 100644 --- a/Router/item.py +++ b/Router/item.py @@ -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 @@ -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) diff --git a/Service/item_service.py b/Service/item_service.py index ea1f169..9d9ef11 100644 --- a/Service/item_service.py +++ b/Service/item_service.py @@ -11,4 +11,9 @@ class ItemService: @staticmethod def get_all(): with get_db() as db: - return db.query(Item).all() \ No newline at end of file + 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() \ No newline at end of file diff --git a/Service/useritem_service.py b/Service/useritem_service.py index ca9e509..dd374ce 100644 --- a/Service/useritem_service.py +++ b/Service/useritem_service.py @@ -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 """ @@ -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: