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

 

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