FastAPI(七十八)实战开发《在线课程学习系统》接口开发-- 评论

简介: FastAPI(七十八)实战开发《在线课程学习系统》接口开发-- 评论

梳理下思路:


1.判断是否登录


2.判断课程是否存在


3.如果回复查看回复的是否存在


4.校验是否有权限


5.评论成功

 

对应的pydantic类如下


class Coursecomment(BaseModel):
   id: int
   comments: str
   pid: Optional[int]


对应的crud


def createcomments(db: Session, cousecoment: Coursecomment, user: id):
    comments = Commentcourse(**cousecoment.dict())
    comments.users=user
    db.add(comments)
    db.commit()
    db.refresh(comments)
    return comments


 对应的代码实现。


@courseRouter.post(path="/comments")
async def comments(comments: Coursecomment,user: UsernameRole = Depends(get_cure_user),
                   db: Session = Depends(get_db)):
    if comments.comments == '':
        return reponse(code=101402, message='评论内容不能为空', data='')
    users = get_user_username(db, user.username)
    couses = db_get_course_id(db, comments.id)
    if couses:
        if couses.owner == users.id and comments.pid is None:
            return reponse(code=101404, message='自己不能评论自己的课程', data='')
        if comments.pid is not None:
            pid_course = get_cousecomments(db, comments.pid)
            if pid_course:
                createcomments(db, comments, users.id)
                return reponse(code=200, message='成功', data='')
            return reponse(code=101405, message='回复的评论不存在', data='')
        createcomments(db, comments, users.id)
        return reponse(code=200, message='成功', data='')
    return reponse(code=101401, message='课程id不存在', data='')


相关文章
|
NoSQL 测试技术 Redis
FastAPI(八十四)实战开发《在线课程学习系统》--接口测试(下)
FastAPI(八十四)实战开发《在线课程学习系统》--接口测试(下)
FastAPI(八十四)实战开发《在线课程学习系统》--接口测试(下)
|
测试技术 数据安全/隐私保护
FastAPI(八十四)实战开发《在线课程学习系统》--接口测试(上)
FastAPI(八十四)实战开发《在线课程学习系统》--接口测试(上)
|
存储 测试技术 数据安全/隐私保护
FastAPI(八十三)实战开发《在线课程学习系统》--注册接口单元测试
FastAPI(八十三)实战开发《在线课程学习系统》--注册接口单元测试
FastAPI(八十三)实战开发《在线课程学习系统》--注册接口单元测试
FastAPI(八十二)实战开发《在线课程学习系统》接口开发-- 课程上架下架
FastAPI(八十二)实战开发《在线课程学习系统》接口开发-- 课程上架下架
|
NoSQL Redis 数据库
FastAPI(八十一)实战开发《在线课程学习系统》接口开发-- 推荐课程列表与课程点赞
FastAPI(八十一)实战开发《在线课程学习系统》接口开发-- 推荐课程列表与课程点赞
FastAPI(八十)实战开发《在线课程学习系统》接口开发-- 课程列表
FastAPI(八十)实战开发《在线课程学习系统》接口开发-- 课程列表
FastAPI(七十九)实战开发《在线课程学习系统》接口开发-- 加入课程和退出课程
FastAPI(七十九)实战开发《在线课程学习系统》接口开发-- 加入课程和退出课程
FastAPI(七十七)实战开发《在线课程学习系统》接口开发-- 课程编辑和查看评论
FastAPI(七十七)实战开发《在线课程学习系统》接口开发-- 课程编辑和查看评论
FastAPI(七十六)实战开发《在线课程学习系统》接口开发-- 课程详情
FastAPI(七十六)实战开发《在线课程学习系统》接口开发-- 课程详情
FastAPI(七十五)实战开发《在线课程学习系统》接口开发-- 创建课程
FastAPI(七十五)实战开发《在线课程学习系统》接口开发-- 创建课程