daily/Mods/Comment/Router.py

68 lines
3.1 KiB
Python

from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from Utils.SqlAlchemyUtils import get_db, Base
from . import Schemas
from . import Crud
from Utils.SqlAlchemyUtils import QueryParams, query_common
from .Models import Comment
from ..Notice.Utils import DailyNotice, comment_notice_send, daily_notice
router = APIRouter(tags=["评论"])
@router.post("/comment/add", summary="添加评论", response_model=Schemas.CommentAddRes)
def comment_add(req: Schemas.CommentAddReq, db: Session = Depends(get_db)):
item = Crud.comment_add(db, req)
comment = Schemas.CommentAddRes(**item.to_dict(full=True))
comment_user = comment.user
notice_user = comment.daily.user
send = comment_notice_send.format(daily_time=comment.daily.create_time.strftime('%Y年%m月%d%H:%M'),
# daily_name=comment.daily.title,
daily_type=comment.daily.type.name,
daily_content=comment.daily.content[0:100].replace(";", "\n"),
comment_user_name=comment_user.name,
comment_content=comment.content,
comment_time=comment.create_time.strftime('%Y年%m月%d%H:%M')
)
daily_notice.notice_comment(
f'收到反馈:【{comment.daily.type.value}{f"-【{comment.daily.title}" if comment.daily.title else ""}',
notice_user.email, send)
return comment
@router.post("/comment/delete", summary="删除评论")
def comment_delete(req: Schemas.CommentDeleteReq, db: Session = Depends(get_db)):
Crud.comment_delete(db, req.id)
return "删除成功"
@router.post("/comment/update", summary="更新评论", response_model=Schemas.CommentUpdateRes)
def comment_update(req: Schemas.CommentUpdateReq, db: Session = Depends(get_db)):
item = Crud.comment_update(db, req)
return Schemas.CommentUpdateRes(**item.to_dict())
@router.post("/comment/get", summary="获取评论", response_model=Schemas.CommentGetRes)
def comment_get(req: Schemas.CommentGetReq, db: Session = Depends(get_db)):
item = Crud.comment_get(db, req.id)
if not item:
raise HTTPException(detail="未查询到信息", status_code=404)
return Schemas.CommentGetRes(**item.to_dict(full=True))
@router.post("/comment/query", summary="查询评论", response_model=Schemas.CommentQueryRes)
def comment_query(req: Schemas.CommentQueryReq, db: Session = Depends(get_db)):
count, query = Crud.comment_query(db, req)
items = [Schemas.CommentInfo(**item.to_dict()) for item in query]
return Schemas.CommentQueryRes(count=count, items=items)
@router.post("/comment/query_common", summary="通用查询评论",
response_model=Schemas.CommentQueryRes)
def comment_query_common(req: QueryParams, db: Session = Depends(get_db)):
count, query = query_common(db, Comment, req)
items = [Schemas.CommentInfo(**item.to_dict()) for item in query]
return Schemas.CommentQueryRes(count=count, items=items)
#########