Commit 5472b8a9 authored by Libcat's avatar Libcat 👷
Browse files

新增商品评论、送货时间接口

parent 934daf13
......@@ -124,6 +124,7 @@ class UserAddressFull(Model):
# 订单中的商品
class OrderItems(Model):
"""订单项目 读取"""
id: int
product: ProductBase
amount: int
price: Decimal
......@@ -144,6 +145,7 @@ class OrderGet(Model):
created_time: datetime
items: List[OrderItems]
total: Decimal
deliver_time: Optional[str]
class OrderCreate(Model):
......@@ -153,11 +155,7 @@ class OrderCreate(Model):
memo: Optional[str]
items: List[OrderItemsCreate]
deleteFromCar: Optional[bool] = True
class OrderResult(Response):
"""下单结果"""
order: Optional[OrderGet]
deliver_time: Optional[str] = ""
# 购物车项目
......@@ -183,8 +181,8 @@ class SigninSignupResult(Response):
class Comment(Model):
id: int
product: int
user: int
product_id: int
user: UserBase
content: str
created_time: datetime
......
......@@ -12,12 +12,14 @@ router = APIRouter(tags=["评价系统"], prefix="/comment")
@router.get('/', response_model=List[api.Comment], description="获取商品的评论列表")
async def get_comments(product: Optional[int]=None,
user: Optional[int]=None):
user: Optional[int]=None,
desc:bool= True):
q=Comment.select(Comment,User).join(User)
if product!=None:
q=q.where(Comment.product_id==product)
if user!=None:
q=q.where(Comment.user_id==user)
q=q.order_by(Comment.id.desc())
return list(q)
@router.post('/', response_model=api.Comment, description="新增一个评论")
......@@ -27,6 +29,6 @@ async def add_comment(data:api.CommentCreate, user:User = Depends(get_user)):
return Response("找不带该订单项目", 404)
if order_item.order.user_id != user.id:
return Response("您没有该订单项的评论权限", 403)
dbitem = Comment.create(content = data.content, user=user.id, product=order_item.product_id)
dbitem = Comment.create(content = data.content, user=user.id, product=order_item.product_id, order_item=order_item.id)
return dbitem
\ No newline at end of file
......@@ -12,6 +12,8 @@ router = APIRouter(tags=["订单系统"], prefix="/order")
@router.get('/', response_model=List[api.OrderGet], description="获取订单列表")
async def get_multiple(user: User = Depends(get_user)):
if user.is_admin:
return list(Order.select().order_by(Order.created_time.desc()))
return list(user.orders.order_by(Order.created_time.desc()))
......
......@@ -11,9 +11,12 @@ router = APIRouter(tags=["产品CRUD"], prefix="/products")
@router.get('/', response_model=List[api.ProductGet], description="获取产品列表")
async def get_list(category: Optional[int] = None,
categoryName: Optional[str] = None,
name: Optional[str] = None):
async def get_list(category: int = None,
categoryName: str = None,
name: str = None,
orderby:str = 'id',
desc:bool = False,
limit: int = None):
q = Product.select(Product, Category).join(Category)
if categoryName:
......@@ -25,6 +28,21 @@ async def get_list(category: Optional[int] = None,
if name:
q = q.where(Product.name.contains(name))
if orderby=='sales':
if desc:
q = q.order_by(Product.sales.desc())
else:
q = q.order_by(Product.sales)
if orderby=='id':
if desc:
q = q.order_by(Product.id.desc())
else:
q = q.order_by(Product.id)
if limit!=None:
q=q.limit(limit)
return list(q)
......
......@@ -24,7 +24,7 @@ dbconnection = PooledMySQLDatabase(config.database,
registed_models = (
Category, Product, User, UserAddress, Order, OrderItems, CarItems,
Session
Session, Comment
)
dbconnection.bind(registed_models)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment