Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
rocknroll17 committed Nov 2, 2024
2 parents 4517b0f + cd0b736 commit 87d2343
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 34 deletions.
1 change: 1 addition & 0 deletions Database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,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
5 changes: 3 additions & 2 deletions Router/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
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 ItemCategory
from Service.embedding_service import EmbeddingService
from Service.item_service import ItemService
from Service.purchase_service import PurchaseService
from Service.useritem_service import UserItemService
import numpy as np
Expand All @@ -28,7 +29,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()
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()
31 changes: 22 additions & 9 deletions Service/useritem_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
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
from Database.models import UserItem
from Database.models import UserItem, Item
from .item_service import ItemService

"""
Expand All @@ -27,15 +28,27 @@ 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 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]):
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
41 changes: 21 additions & 20 deletions mock_data.csv

Large diffs are not rendered by default.

0 comments on commit 87d2343

Please sign in to comment.