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'/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(, item.product)
if dbitem:
dbitem.amount += item.amount # 数量求和
dbitem = db.CarItems.create(**item.dict(),
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(, 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):,User).join(User)
if product!=None:
if user!=None:
return list(q)'/', response_model=api.Comment, description="新增一个评论")
async def add_comment(data:api.CommentCreate, user:User = Depends(get_user)):
order_item:OrderItems = OrderItems.get_or_none(
if not order_item:
return Response("找不带该订单项目", 404)
if order_item.order.user_id !=
return Response("您没有该订单项的评论权限", 403)
dbitem = Comment.create(content = data.content,, 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(
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(
\ No newline at end of file
time = DateTimeField(
class Comment(Model):
user = ForeignKeyField(User, backref='comments')
product = ForeignKeyField(Product, backref='comment')
created_time = DateTimeField(
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")
# 需要注意是,当配置了xhr.withCredentials = true时,必须在后端增加 response 头信息Access-Control-Allow-Origin,且必须指定域名,而不能指定为*。
......@@ -43,7 +43,7 @@ app.include_router(api_user_address.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