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

這樣我們的課程編輯和查看評論就實現完成了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章