From 814d1c6b93acc7672c36c1a23a484f55ec0bf89e Mon Sep 17 00:00:00 2001 From: rocknroll17 Date: Sun, 3 Nov 2024 02:56:22 +0900 Subject: [PATCH] feat:Added comsume history --- Router/item.py | 5 +++-- Service/consume_service.py | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 Service/consume_service.py diff --git a/Router/item.py b/Router/item.py index 410d74c..bb144b8 100644 --- a/Router/item.py +++ b/Router/item.py @@ -9,6 +9,7 @@ from fastapi import Query, Request from Data.item import Item, ItemAdd, UserItemAdd, UserItemConsume, ItemRead from Database.models import ItemCategory +from Service.consume_service import ConsumeService from Service.embedding_service import EmbeddingService from Service.item_service import ItemService from Service.purchase_service import PurchaseService @@ -29,7 +30,6 @@ def add_items(request: Request, itemadd: ItemAdd): """ 유저가 보유한 모든 아이템을 가져온다. """ -# 0개 제외 @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) @@ -39,13 +39,14 @@ def get_userItem_all(request : Request, user_id: str): """ 유저가 아이템 소비 -TODO : 소비 기록 추가 """ @router.post("/consume") def consume_item(request : Request, user_item_consume: UserItemConsume): result = UserItemService.consume_userItem(user_item_consume) if result is False: return JSONResponse(status_code=HTTP_400_BAD_REQUEST, content={"message": "Item consume failed"}) + consume_history = ConsumeService.consume_history_db(user_item_consume) + ConsumeService.purchase_history_save(consume_history) return JSONResponse(status_code=HTTP_200_OK, content={"message": "Item consumed successfully"}) diff --git a/Service/consume_service.py b/Service/consume_service.py new file mode 100644 index 0000000..d10981b --- /dev/null +++ b/Service/consume_service.py @@ -0,0 +1,19 @@ +from typing import List +from Data.item import * +from Database.models import * + +class ConsumeService: + def consume_history_db(data : UserItemConsume): + with get_db() as db: + item_id = db.query(UserItem).filter(UserItem.user_id == data.user_id, UserItem.item_name == data.item_name).first().item_id + print(data.user_id, item_id) + return ConsumeHistory(user_id=data.user_id, item_id=item_id, count=data.consume_count, date=data.consume_date) + + def purchase_history_save(data : ConsumeHistory): + with get_db() as db: + db.add(data) + db.commit() + + def get_consume_histories_by_item_id(item_id: str): + with get_db() as db: + return db.query(ConsumeHistory).filter(ConsumeHistory.item_id == item_id).all() \ No newline at end of file