FastAPI(八十)实战开发《在线课程学习系统》接口开发-- 课程列表

源码见:"fastapi_study_road-learning_system_online_courses: fastapi框架实战之--在线课程学习系统"

查询所有课程列表

逻辑就是返回所有课程,也很简单,直接上代码

复制代码
def get_all_courses(db: Session):
    """查询所有课程列表"""
    try:
        all_courses = get_all(db)
        to_client = []
        if all_courses:
            for _ in all_courses:
                course_detail = CourseDetail(
                    id=_.id,
                    name=_.name,
                    icon=_.icon,
                    desc=_.desc,
                    catalog=_.catalog,
                    onsale=_.onsale,
                    owner=get_by_uid(db, _.owner).username,
                    like_num=_.like_num
                )
                to_client.append(course_detail.dict())
    except:
        logger.warning(f"查询失败:{traceback.format_exc()}")
        return response(code=101701, message="查询失败")
    return response(data=to_client)

接口api

复制代码
@course_router.get("/student_course/all", summary="查询所有课程")
def get_all(db: Session = Depends(create_db)):
    return get_all_courses(db)

测试:

查询学生自己的课程列表

逻辑也比较简单

复制代码
def db_student_like(db: Session, user_id: int):
    """学生加入的所有课程"""
    return db.query(StudentCourse).filter(StudentCourse.student == user_id, StudentCourse.status == False).all()


def get_student_like(user: UsernameRole, db: Session):
    """查询学生加入的所有课程"""
    if user.role == "老师":
        return response(code=101701, message="只有学生可以查看自己加入的课程列表")
    try:
        db_user = get_by_username(db, user.username)
        student_likes = db_student_like(db, db_user.id)
        to_client = []
        if student_likes:
            for _ in student_likes:
                db_course = get_course_by_id(db, _.course)
                course_detail = CourseDetail(
                    id=db_course.id,
                    name=db_course.name,
                    icon=db_course.icon,
                    desc=db_course.desc,
                    catalog=db_course.catalog,
                    onsale=db_course.onsale,
                    owner=get_by_uid(db, db_course.owner).username,
                    like_num=db_course.like_num
                )
                to_client.append(course_detail.dict())
    except:
        logger.warning(f"method get_student_like error: {traceback.format_exc()}")
        return response(code=101702, message="查询失败")
    return response(data=to_client)

接口api

复制代码
@course_router.get("/student_course", summary="查询学生加入的所有课程")
def get_student_like_course(user: UsernameRole = Depends(get_current_user), db: Session = Depends(create_db)):
    return get_student_like(user, db)

测试

以上就是查询所有课程以及学生加入的课程接口

相关推荐
强化实验室2 小时前
如何设计基于吖啶生物素,Acridinium-Biotin的高效生物检测体系?
flask·scala·pyqt·fastapi·web3.py
强化试剂瓶2 小时前
Acridinium-Biotin,吖啶生物素偶联物双功能设计的精妙之处
flask·numpy·fastapi·web3.py·tornado
全栈测试笔记10 小时前
FastAPI系列(02):第一个示例
fastapi
PieroPc2 天前
用FastAPI 和Html+css+js 写的 发票PDF文件管理系统
css·html·fastapi
钱彬 (Qian Bin)3 天前
项目实践17—全球证件智能识别系统(开发基于LabelMe标注的可视化审核接口)
qt·fastapi·全球证件识别
ghgxm5204 天前
Fastapi_00_学习策略与学习计划
python·学习·前端框架·npm·fastapi
我是菜鸟0713号4 天前
Qt + Python 算法集成的一种低耦合实践:FastAPI 服务化方案
python·qt·fastapi
a程序小傲4 天前
蚂蚁Java面试被问:向量数据库的相似度搜索和索引构建
开发语言·后端·python·架构·flask·fastapi
风送雨4 天前
FastAPI 学习教程 · 第6部分
学习·fastapi