• 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='')

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

  • 相关阅读:
    django 关于render的返回数据
    关于 eval 的报错 Uncaught ReferenceError: False is not defined
    Unexpected token o in JSON at position 1 at JSON.parse (<anonymous>) SyntaxError: Unexpected token R in JSON at position 0 at JSON.parse (<anonymous>)
    ajax 异步请求返回只刷新一次页面
    线程
    IO
    IO初步,字节输入流和字节输出流
    File、FileFilter、递归初步
    Map、可变参数、静态导入、Collections、Arrays、集合嵌套
    Collection单列集合中的常用实现类
  • 原文地址:https://www.cnblogs.com/leiziv5/p/16098413.html
Copyright © 2020-2023  润新知