Commit 40700be1 authored by Libcat's avatar Libcat 👷
Browse files

重构评价系统,修复了一个订单项可以重复评价的bug

parent 65f91bdb
# Created by https://www.toptal.com/developers/gitignore/api/python,visualstudiocode
# Edit at https://www.toptal.com/developers/gitignore?templates=python,visualstudiocode
upload/user-*
upload/product-*
### Python ###
# Byte-compiled / optimized / DLL files
......
......@@ -129,6 +129,8 @@ class OrderItems(Model):
amount: int
price: Decimal
total: Decimal
comment: str = None
comment_time: datetime = None
class OrderItemsCreate(Model):
"""订单项目 创建"""
......@@ -181,10 +183,10 @@ class SigninSignupResult(Response):
class Comment(Model):
id: int
product_id: int
user: UserBase
content: str
created_time: datetime
userid: int
username: str
comment: str = None
comment_time: datetime = None
class CommentCreate(Model):
order_item: int
......
from typing import List, Optional
from decimal import Decimal
from fastapi import APIRouter, Depends, Response, Body
from datetime import datetime
from typing import List
from fastapi import APIRouter, Depends, Response
import apischemas as api
from database import *
......@@ -9,49 +8,69 @@ from .authorize import get_user
router = APIRouter(tags=["评价系统"], prefix="/comment")
# 注意:评价系统只是依托于订单项的一个功能,即数据库中没有单独的评价表,评价id=订单项id
@router.get('/', response_model=List[api.Comment], description="根据商品id获取评价列表")
async def get_comments_by_product(product: int, desc: bool = True):
comments = (OrderItems.select(OrderItems.id, OrderItems.comment,
OrderItems.comment_time, User.id.alias('userid'),
User.username,
User.image).join(Order).join(User).where(
OrderItems.product_id == product,
OrderItems.comment != None)).dicts()
if desc:
comments = comments.order_by(OrderItems.comment_time.desc())
return list(comments)
@router.get('/{orderitemid}', response_model=api.Comment, description="根据订单项id获取评价列表")
async def get_comments_by_orderitem(orderitemid: int):
comments = list(OrderItems.select(OrderItems.id, OrderItems.comment,
OrderItems.comment_time, User.id.alias('userid'),
User.username,
User.image).join(Order).join(User).where(
OrderItems.id == orderitemid).dicts())
if len(comments)==0:
return Response("该项目不存在", 404)
return comments[0]
@router.get('/', response_model=List[api.Comment], description="获取商品的评论列表")
async def get_comments(product: 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="新增一个评论")
async def add_comment(data:api.CommentCreate, user:User = Depends(get_user)):
order_item:OrderItems = OrderItems.get_or_none(OrderItems.id==data.order_item)
@router.post('/', 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, order_item=order_item.id)
return dbitem
order_item.comment = data.content
order_item.comment_time = datetime.now()
order_item.save()
return Response("评价成功")
@router.delete('/{id}', description="删除评价")
async def delete_one(id: int, user: User = Depends(get_user)):
dbitem: Comment = Comment.get_or_none(Comment.id == id)
if dbitem:
if user.id!=dbitem.user_id:
return Response("您没有该评价的权限", 403)
dbitem.delete_instance()
return Response("删除成功")
return Response("该项目不存在", 404)
@router.put('/{id}', response_model=api.Comment, description="修改评价")
async def modify_one(id:int, data:api.CommentCreate, user: User = Depends(get_user)):
dbitem: Comment = Comment.get_or_none(Comment.id == id)
if dbitem:
if user.id!=dbitem.user_id:
return Response("您没有该评价的权限", 403)
dbitem.content = data.content
dbitem.save()
return dbitem
return Response("该项目不存在", 404)
order_item: OrderItems = OrderItems.get_or_none(
OrderItems.id == id)
if not order_item:
return Response("该项目不存在", 404)
if order_item.order.user_id != user.id:
return Response("您没有该评价的权限", 403)
order_item.comment = None
order_item.comment_time = None
order_item.save()
return Response("修改成功")
@router.put('/{id}', description="修改评价")
async def modify_one(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)
order_item.comment = data.content
order_item.comment_time = datetime.now()
order_item.save()
return Response("修改成功")
\ No newline at end of file
......@@ -19,7 +19,7 @@ dbconnection = PooledMySQLDatabase(config.database,
registed_models = (
Category, Product, User, UserAddress, Order, OrderItems, CarItems,
Session, Comment, Announcement
Session, Announcement
)
dbconnection.bind(registed_models)
......
......@@ -9,7 +9,7 @@ from peewee import *
from datetime import datetime
__all__ = ('Category', 'Product', 'User', 'UserAddress', 'Order',
'OrderItems', 'CarItems', 'Session', 'Comment', 'Announcement')
'OrderItems', 'CarItems', 'Session', 'Announcement')
# 商品类型
......@@ -77,6 +77,8 @@ class OrderItems(Model):
amount = IntegerField()
price = DecimalField(10, 2)
total = DecimalField(10, 2)
comment = TextField(null=True)
comment_time = DateTimeField(null=True)
# 购物车项目
......@@ -95,15 +97,6 @@ class Session(Model):
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="")
class Announcement(Model):
"""公告"""
content = TextField(default="", verbose_name="公告内容")
......
from typing import List, Optional, Type, TypeVar, Generic
class A:
def __init__(self) -> None:
print('A')
@staticmethod
def shit():
print('shit')
class G:
def __init__(self,t: Type[A]) -> None:
t.shit()
a = G(A)
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