Commit 934daf13 authored by Libcat's avatar Libcat 👷
Browse files

新增商品评价接口

parent 754ad169
{
"python.pythonPath": "/usr/bin/python3"
}
\ No newline at end of file
from decimal import Decimal
from re import L
from typing import List, Optional, Any
from datetime import datetime
......@@ -179,3 +180,14 @@ class SigninSignupResult(Response):
user: Optional[UserBase]
sessionid: str
class Comment(Model):
id: int
product: int
user: int
content: str
created_time: datetime
class CommentCreate(Model):
order_item: int
content: str
\ No newline at end of file
......@@ -33,6 +33,22 @@ async def create_one(item: api.CarItemUpdate,
return dbitem
@router.post('/addmany', response_model=List[api.CarItemBase], description="批量添加产品到购物车")
async def create_many(items: List[api.CarItemUpdate],
user: db.User = Depends(get_user)):
# 先查询购物车中是否已经存在该商品
result = []
for item in items:
dbitem = get_car_item(user.id, item.product)
if dbitem:
dbitem.amount += item.amount # 数量求和
dbitem.save()
else:
dbitem = db.CarItems.create(**item.dict(), user=user.id)
result.append(dbitem)
return result
@router.delete('/', response_model=api.CarItemBase, description="删除购物车项目")
async def delete_one(item: api.CarItemUpdate, user: db.User = Depends(get_user)):
dbitem = get_car_item(user.id, item.product)
......
from typing import List, Optional
from decimal import Decimal
from fastapi import APIRouter, Depends, HTTPException, Response, responses
import apischemas as api
from database import *
from .authorize import get_user
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):
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)
return list(q)
@router.post('/', response_model=api.Comment, description="新增一个评论")
async def add_comment(data:api.CommentCreate, user:User = Depends(get_user)):
order_item:OrderItems = OrderItems.get_or_none(OrderItems.id==data.order_item)
if not order_item:
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)
return dbitem
\ No newline at end of file
......@@ -14,7 +14,7 @@ from peewee import *
from datetime import datetime
__all__ = ('Category', 'Product', 'User', 'UserAddress', 'Order',
'OrderItems', 'CarItems', 'Session')
'OrderItems', 'CarItems', 'Session', 'Comment')
# 商品类型
......@@ -70,6 +70,8 @@ class Order(Model):
memo = CharField(default="")
created_time = DateTimeField(default=datetime.now)
total = DecimalField(10, 2, default=0)
deliver_time = CharField(default="不限")
package = CharField(default="默认")
# 订单中的商品
......@@ -95,4 +97,13 @@ class Session(Model):
"""会话"""
id = CharField(40, primary_key=True) # uuid 不要使用UUID Field,会有奇怪的现象
user = ForeignKeyField(User)
time = DateTimeField(default=datetime.now)
\ No newline at end of file
time = DateTimeField(default=datetime.now)
class Comment(Model):
"""评价"""
user = ForeignKeyField(User, backref='comments')
product = ForeignKeyField(Product, backref='comment')
created_time = DateTimeField(default=datetime.now)
order_item = ForeignKeyField(OrderItems, backref='comments')
content = CharField(default="")
\ No newline at end of file
......@@ -6,14 +6,14 @@ from playhouse.pool import PooledMySQLDatabase
import apischemas as api
import database as db
from controllers import api_products, api_category, api_user_address, api_users, api_orders, model_crud_base, api_file, api_car
from controllers import api_products, api_category, api_user_address, api_users, api_orders, api_comment, api_file, api_car
app = FastAPI(debug=True, title="文苑商城", description="文苑商城后台API系统", version="0.1.0")
app.add_middleware(
CORSMiddleware,
# 需要注意是,当配置了xhr.withCredentials = true时,必须在后端增加 response 头信息Access-Control-Allow-Origin,且必须指定域名,而不能指定为*。
allow_origins=["http://localhost:8080","http://localhost"],
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
......@@ -43,7 +43,7 @@ app.include_router(api_user_address.router)
app.include_router(api_orders.router)
app.include_router(api_car.router)
app.include_router(api_file.router)
app.include_router(api_comment.router)
app.mount("/file", StaticFiles(directory="upload"), name="file")
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