2023-06-28 14:42:58 +08:00
|
|
|
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
|
2023-06-30 13:39:24 +08:00
|
|
|
from ..Notice.Utils import DailyNotice, comment_notice_send, daily_notice
|
2023-06-28 14:42:58 +08:00
|
|
|
|
|
|
|
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)
|
2023-06-28 16:49:21 +08:00
|
|
|
comment = Schemas.CommentAddRes(**item.to_dict(full=True))
|
|
|
|
comment_user = comment.user
|
|
|
|
notice_user = comment.daily.user
|
2023-06-30 16:06:57 +08:00
|
|
|
send = comment_notice_send.format(daily_time=comment.daily.create_time.strftime('%Y年%m月%d日%H:%M'),
|
|
|
|
# daily_name=comment.daily.title,
|
2023-06-28 16:56:46 +08:00
|
|
|
daily_type=comment.daily.type.name,
|
2023-06-30 18:41:45 +08:00
|
|
|
daily_content=comment.daily.content[0:100].replace(";", "\n"),
|
2023-06-28 16:49:21 +08:00
|
|
|
comment_user_name=comment_user.name,
|
2023-06-30 16:06:57 +08:00
|
|
|
comment_content=comment.content,
|
|
|
|
comment_time=comment.create_time.strftime('%Y年%m月%d日%H:%M')
|
2023-06-28 16:49:21 +08:00
|
|
|
)
|
2023-06-30 18:41:45 +08:00
|
|
|
daily_notice.notice_comment(
|
|
|
|
f'收到反馈:【{comment.daily.type.value}】{f"-【{comment.daily.title}】" if comment.daily.title else ""}',
|
|
|
|
notice_user.email, send)
|
2023-06-28 16:49:21 +08:00
|
|
|
return comment
|
2023-06-28 14:42:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
@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)
|
|
|
|
|
|
|
|
#########
|