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

Posted 北漂的雷子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FastAPI(七十七)实战开发《在线课程学习系统》接口开发-- 课程编辑和查看评论相关的知识,希望对你有一定的参考价值。

首先来看下课程编辑:

那么我们编辑就变的简单了。逻辑如下。

1.判断是否登录
2.判断课程是否存在
3.课程名称是否重复

在基础的pydantic的Courses类,增加一个id

 

class CoursesEdit(Courses):
   id:int

  具体最后的代码

@courseRouter.put(path=\'/edit\')
async  def edit(
                 coursesedit: CoursesEdit,
                 db: Session = Depends(get_db),user: UsernameRole = Depends(get_cure_user)):
    users=get_user_username(db,user.username)
    couses_is=db_get_course_id(db,coursesedit.id)
    if not  couses_is:
        return reponse(code=101201,data=\'\',message=\'课程id不存在\')
    couses_name=db_get_course_name(db,coursesedit.name)
    if couses_name:
        return reponse(code=101203, data=\'\', message=\'课程名称不能重复\')
    if couses_is.owner==users.id:
        couses_is.catalog=coursesedit.catalog
        couses_is.desc=coursesedit.desc
        couses_is.icon=coursesedit.icon
        couses_is.name=coursesedit.name
        db.commit()
        db.refresh(couses_is)
        return reponse(code=200,message=\'成功\',data=couses_is)
    return reponse(code=101202,message=\'权限不足\',data=\'\')

  

接下来看下查看评论。

    主要逻辑

1.判断课程是否存在2.存在返回所有的评论

    对应实现的代码

@courseRouter.get(path=\'/viewcomments/id\')
async  def viewcomments(id:int,
                        db: Session = Depends(get_db)):
    couses=db_get_course_id(db,id)
    if couses:
        allcomments = db_get_coursecomment_id(db, couses.id)
        all_comments_list = []
        if len(allcomments) > 0:
            for item in allcomments:
                detailcomment = Coursescomment(id=item.id,
                                               top=item.top,
                                               users=get_user(db, item.users).username,
                                               pid=item.id, addtime=str(item.addtime),
                                               context=item.context)
                all_comments_list.append(detailcomment)
        return reponse(code=200,message="成功",data=jsonable_encoder(all_comments_list))
    return reponse(code=101301,message=\'课程id不存在\',data=\'\')

这样我们的课程编辑和查看评论就实现完成了。

以上是关于FastAPI(七十七)实战开发《在线课程学习系统》接口开发-- 课程编辑和查看评论的主要内容,如果未能解决你的问题,请参考以下文章

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

FastAPI(七十九)实战开发《在线课程学习系统》接口开发-- 加入课程和退出课程

FastAPI(六十八)实战开发《在线课程学习系统》接口开发--用户 个人信息接口开发

MATLAB应用实战系列(七十七)-图像处理COVID-19 防疫应用口罩检测

深度学习核心技术精讲100篇(七十七)-主流推荐引擎技术及优缺点分析

深度学习核心技术精讲100篇(七十七)-主流推荐引擎技术及优缺点分析