Skip to content

Commit

Permalink
feat:add price predict feature
Browse files Browse the repository at this point in the history
  • Loading branch information
jingooo5 committed Nov 2, 2024
1 parent 7945ee5 commit cd0b736
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
1 change: 1 addition & 0 deletions Database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Item(BaseEntity):
item_category = Column(Enum(ItemCategory), nullable=False)
base_consume_expectation = Column(Integer, nullable=False)
base_price = Column(Integer, nullable=False)
base_count = Column(Integer, nullable=False)
embedding = Column(JSON, nullable=True)


Expand Down
31 changes: 29 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 User
from Service.item_service import ItemService
from Service.purchase_service import PurchaseService
from Service.useritem_service import UserItemService
router = APIRouter(tags=["items"], prefix="/items")
Expand Down Expand Up @@ -52,6 +53,32 @@ def consume_item(request : Request, user_id: str, user_item_consume: UserItemCon
@router.post("/addone")
def add_item(request : Request, userItemAdd: UserItemAdd):
UserItemService.add_userItem(userItemAdd)
purchaseHistory = PurchaseService.get_purchase_history(userItemAdd)
purchaseHistory = PurchaseService.purchase_history_db(userItemAdd)
PurchaseService.purchase_history_save(purchaseHistory)
return JSONResponse(status_code=HTTP_200_OK, content={"message": "Item added successfully"})
return JSONResponse(status_code=HTTP_200_OK, content={"message": "Item added successfully"})


@router.get("/{user_id}/price")
def get_expected_price(request : Request, user_id: str, date : str = Query(...)):
try:
year, month = map(int, date.split("-"))

user_item_list = UserItemService.get_all_userItem_filtered_by_date(user_id, year, month)

price = 0
for useritem in user_item_list:
purchases = PurchaseService.get_purchase_histories_by_item_id(useritem.item_id)
if len(purchases) == 0:
item = ItemService.get_item(useritem.item_name)
price += item.base_price * item.base_count
else:
avg_count = round(sum([purchase.count for purchase in purchases]) / len(purchases))
avg_price = sum([purchase.price for purchase in purchases]) / len(purchases)
price += avg_count * avg_price

return JSONResponse(status_code=HTTP_200_OK, content={"price": price})

except ValueError:
return JSONResponse(status_code=HTTP_400_BAD_REQUEST, content={"message": "Invalid date format"})
except Exception as e:
return JSONResponse(status_code=HTTP_500_INTERNAL_SERVER_ERROR, content={"message": str(e)})
9 changes: 7 additions & 2 deletions Service/purchase_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@ def purchase_history_list_save(data_list: List[PurchaseHistory]):
db.commit()
return True

def get_purchase_history(data : UserItemAdd):
def purchase_history_db(data : UserItemAdd):
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
return PurchaseHistory(user_id=data.user_id, item_id=item_id, price=data.price, count=1, date=data.purchase_date)

def purchase_history_save(data : PurchaseHistory):
with get_db() as db:
db.add(data)
db.commit()
db.commit()


def get_purchase_histories_by_item_id(item_id: str):
with get_db() as db:
return db.query(PurchaseHistory).filter(PurchaseHistory.item_id == item_id).all()
7 changes: 7 additions & 0 deletions Service/useritem_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Optional, List, Annotated

from sqlalchemy.orm import Session
from sqlalchemy.sql.expression import extract

from Data.item import UserItemAdd, ItemAdd
from Database.database import get_db
Expand All @@ -29,6 +30,12 @@ def get_all_userItem(user_id: str):
return db.query(UserItem).filter(UserItem.user_id == user_id).all()


def get_all_userItem_filtered_by_date(user_id, year: int, month: int):
with get_db() as db:
return db.query(UserItem).filter(UserItem.user_id == user_id, extract("year", UserItem.consume_date) == year,
extract("month", UserItem.consume_date) == month).all()


def to_userItem_dict(userItemList: List[UserItem]):
itemlist = []
with get_db() as db:
Expand Down

0 comments on commit cd0b736

Please sign in to comment.