Skip to content

Commit

Permalink
chore:add price to get_all
Browse files Browse the repository at this point in the history
  • Loading branch information
jingooo5 committed Nov 2, 2024
1 parent 39d48f4 commit 0d5adba
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions Data/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Item(BaseModel):
class ItemRead(Item):
count : int
category : str
price: float
consume_date: date

class ItemConsume(Item):
Expand Down
24 changes: 23 additions & 1 deletion Service/useritem_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from Database.database import get_db
from Database.models import UserItem, Item
from .item_service import ItemService
from .purchase_service import PurchaseService

"""
useritem crud
Expand Down Expand Up @@ -42,11 +43,22 @@ def to_userItem_dict_with_category(userItemList: List[UserItem], category: str):
itemlist = []

for userItem in userItemList:
purchases = PurchaseService.get_purchase_histories_by_item_id(userItem.item_id)
price = 0

if len(purchases) == 0:
item = ItemService.get_item(userItem.item_name)
price += item.base_price
else:
price = sum([purchase.price for purchase in purchases]) / len(purchases)


itemlist.append({
"user_id": userItem.user_id,
"item_name": userItem.item_name,
"count": userItem.count,
"category": category,
"price" : price,
"consume_date": userItem.consume_date.strftime("%Y-%m-%d") if userItem.consume_date is not None else None,
})
return itemlist
Expand All @@ -58,11 +70,21 @@ def to_userItem_dict(userItemList: List[UserItem]):
with get_db() as db:
for userItem in userItemList:
item = db.query(Item).filter(Item.item_name == userItem.item_name).first()
purchases = PurchaseService.get_purchase_histories_by_item_id(userItem.item_id)
price = 0

if len(purchases) == 0:
item = ItemService.get_item(userItem.item_name)
price += item.base_price
else:
price = sum([purchase.price for purchase in purchases]) / len(purchases)

itemlist.append({
"user_id": userItem.user_id,
"item_name": userItem.item_name,
"count": userItem.count,
"category": item.item_category,
"price" : price,
"consume_date": userItem.consume_date.strftime("%Y-%m-%d") if userItem.consume_date is not None else None,
})
return itemlist
Expand Down Expand Up @@ -96,7 +118,7 @@ def add_userItems(itemAdd : ItemAdd):
db.commit()
return True

def consume_userItem(userItemConsume: UserItemConsume):
def consume_userItem(userItemConsume: UserItemConsume, expectation: Optional[int] = None):
with get_db() as db:
useritem = db.query(UserItem).filter(UserItem.user_id == userItemConsume.user_id, UserItem.item_name == userItemConsume.item_name).first()
if useritem.count - userItemConsume.consume_count < 0:
Expand Down

0 comments on commit 0d5adba

Please sign in to comment.