Commit 65f91bdb authored by Libcat's avatar Libcat 👷
Browse files

add 公告&公告管理功能

parent 69e82626
......@@ -188,4 +188,13 @@ class Comment(Model):
class CommentCreate(Model):
order_item: int
content: str
class Announcement(Model):
id: int
content: str
created_time: datetime
class AnnouncementCreate(Model):
content: str
\ No newline at end of file
from typing import List, Optional
from fastapi import APIRouter, Depends, Response, Body
import apischemas as api
from database import *
from .authorize import get_user
router = APIRouter(tags=["公告系统"], prefix="/announcement")
@router.get('/', response_model=List[api.Announcement], description="获取公告列表")
async def get_comments(desc: bool = False):
q = Announcement.select()
if desc:
q = q.order_by(Announcement.id.desc())
return list(q)
@router.post('/', response_model=api.Announcement, description="新增公告")
async def add_comment(data: api.AnnouncementCreate, user: User = Depends(get_user)):
if user.is_admin == False:
return Response("您没有管理员权限", 403)
dbitem = Announcement.create(content=data.content)
return dbitem
@router.delete('/{id}', description="删除公告")
async def delete_one(id: int, user: User = Depends(get_user)):
if user.is_admin == False:
return Response("您没有管理员权限", 403)
dbitem: Announcement = Announcement.get_or_none(Announcement.id == id)
if dbitem:
dbitem.delete_instance()
return Response("删除成功")
else:
return Response("该项目不存在", 404)
@router.put('/{id}', response_model=api.Announcement, description="修改公告")
async def modify_one(id:int, data: api.AnnouncementCreate, user: User = Depends(get_user)):
if user.is_admin == False:
return Response("您没有管理员权限", 403)
dbitem: Announcement = Announcement.get_or_none(Announcement.id == id)
if dbitem:
dbitem.content = data.content
dbitem.save()
return dbitem
else:
return Response("该项目不存在", 404)
from typing import List, Optional
from decimal import Decimal
from fastapi import APIRouter, Depends, HTTPException, Response, responses
from fastapi import APIRouter, Depends, Response, Body
import apischemas as api
from database import *
......@@ -26,9 +26,32 @@ async def get_comments(product: Optional[int]=None,
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)
return Response("该项目不存在", 404)
if order_item.order.user_id != user.id:
return Response("您没有该订单项的评论权限", 403)
return Response("您没有该评价的权限", 403)
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
@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)
from typing import List, Optional
from fastapi import APIRouter, UploadFile, File
from pathlib import Path
import apischemas as api
import database as db
router = APIRouter(tags=["文件操作"], prefix="/upload")
@router.post("/")
async def create_upload_file(file: UploadFile = File(...)):
content = await file.read()
with open(Path("upload")/Path(file.filename), 'wb') as f:
f.write(content)
return {"filename": file.filename, "content_type": file.content_type, "size": len(content)}
\ No newline at end of file
from typing import List, Optional
from decimal import Decimal
from fastapi import APIRouter, Depends, HTTPException, Response
from fastapi import APIRouter, Depends, Response
import apischemas as api
from database import *
......@@ -11,8 +11,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:
async def get_multiple(adminShowAll: bool = False, user: User = Depends(get_user)):
if adminShowAll and user.is_admin:
return list(Order.select().order_by(Order.created_time.desc()))
return list(user.orders.order_by(Order.created_time.desc()))
......
......@@ -2,11 +2,6 @@
# -*- encoding: utf-8 -*-
'''
@File : __init__.py
@Time : 2021/05/14 12:29:57
@Author : Runze Tang
@Version : 1.0
@Contact : mf1933084@smail.nju.edu.cn
@License : (C)Copyright 2020, Runze Tang
@Desc : 数据库模块
'''
......
......@@ -2,11 +2,6 @@
# -*- encoding: utf-8 -*-
'''
@File : database.py
@Time : 2021/05/12 21:57:47
@Author : Runze Tang
@Version : 1.0
@Contact : mf1933084@smail.nju.edu.cn
@License : (C)Copyright 2020, Runze Tang
@Desc : 这里定义数据库连接、用户名密码,以及初始化等操作
'''
......@@ -24,7 +19,7 @@ dbconnection = PooledMySQLDatabase(config.database,
registed_models = (
Category, Product, User, UserAddress, Order, OrderItems, CarItems,
Session, Comment
Session, Comment, Announcement
)
dbconnection.bind(registed_models)
......
......@@ -2,11 +2,6 @@
# -*- encoding: utf-8 -*-
'''
@File : models.py
@Time : 2021/05/12 21:58:23
@Author : Runze Tang
@Version : 1.0
@Contact : mf1933084@smail.nju.edu.cn
@License : (C)Copyright 2020, Runze Tang
@Desc : 这里定义各种数据库模型(表结构)
'''
......@@ -14,7 +9,7 @@ from peewee import *
from datetime import datetime
__all__ = ('Category', 'Product', 'User', 'UserAddress', 'Order',
'OrderItems', 'CarItems', 'Session', 'Comment')
'OrderItems', 'CarItems', 'Session', 'Comment', 'Announcement')
# 商品类型
......@@ -106,4 +101,10 @@ class Comment(Model):
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
content = CharField(default="")
class Announcement(Model):
"""公告"""
content = TextField(default="", verbose_name="公告内容")
created_time = DateTimeField(default=datetime.now, verbose_name="发布时间")
\ No newline at end of file
import time
from fastapi import FastAPI, Response, Request, Depends, HTTPException, Cookie
from fastapi import FastAPI, Request
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
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, api_comment, api_file, api_car
from controllers import api_products, api_category, api_user_address, api_users, api_orders, api_comment, api_car, api_announcement
app = FastAPI(debug=True, title="文苑商城", description="文苑商城后台API系统", version="0.1.0")
......@@ -42,8 +40,8 @@ app.include_router(api_category.router)
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.include_router(api_announcement.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