Skip to content

Commit

Permalink
feat:Added comsume history
Browse files Browse the repository at this point in the history
  • Loading branch information
rocknroll17 committed Nov 2, 2024
1 parent 6c30eef commit 814d1c6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Router/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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"})


Expand Down
19 changes: 19 additions & 0 deletions Service/consume_service.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 814d1c6

Please sign in to comment.