FastAPI(七十七)實戰開發《線上課程學習系統》介面開發-- 課程編輯和檢視評論
阿新 • • 發佈:2022-04-04
首先來看下課程編輯:
那麼我們編輯就變的簡單了。邏輯如下。
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='')
這樣我們的課程編輯和檢視評論就實現完成了。