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

简介: 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='')


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

 

相关文章
|
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(七十五)实战开发《在线课程学习系统》接口开发-- 创建课程