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)

测试

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

相关推荐
曲幽12 小时前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers
曲幽1 天前
FastAPI实战:打造本地文生图接口,ollama+diffusers让AI绘画更听话
python·fastapi·web·cors·diffusers·lcm·ollama·dreamshaper8·txt2img
曲幽3 天前
我用FastAPI接ollama大模型,差点被asyncio整崩溃(附对话窗口实战)
python·fastapi·web·async·httpx·asyncio·ollama
闲云一鹤4 天前
Python 入门(二)- 使用 FastAPI 快速生成后端 API 接口
python·fastapi
曲幽4 天前
FastAPI + Ollama 实战:搭一个能查天气的AI助手
python·ai·lora·torch·fastapi·web·model·ollama·weatherapi
百锦再4 天前
Django实现接口token检测的实现方案
数据库·python·django·sqlite·flask·fastapi·pip
Li emily5 天前
解决了股票实时数据接口延迟问题
人工智能·fastapi
司徒轩宇5 天前
FastAPI + Uvicorn 深度理解与异步模型解析
fastapi
郝学胜-神的一滴6 天前
FastAPI:Python 高性能 Web 框架的优雅之选
开发语言·前端·数据结构·python·算法·fastapi
yaoty7 天前
Python日志存储:从单机同步到分布式异步的7种方案
fastapi·日志·logger