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)

测试

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

相关推荐
黑金IT4 天前
WebSocket vs. Server-Sent Events:选择最适合你的实时数据流技术
网络·python·websocket·网络协议·fastapi
黑金IT4 天前
FastAPI 应用安全加固:HTTPSRedirectMiddleware 中间件全解析
安全·中间件·fastapi
写bug如流水4 天前
【FastAPI】实现服务器向客户端发送SSE(Server-Sent Events)广播
服务器·python·fastapi
黑金IT4 天前
从单体到微服务:FastAPI ‘挂载’子应用程序的转变
微服务·架构·fastapi
不良人龍木木5 天前
sqlalchemy FastAPI 前端实现数据库增删改查
前端·数据库·fastapi
写bug如流水5 天前
【FastAPI】离线使用Swagger UI 或 国内网络如何快速加载Swagger UI
ui·fastapi·命令模式
布响哒公7 天前
用python fastapi写一个http接口,使ros2机器人开始slam toolbox建图
python·机器人·fastapi
_.Switch9 天前
Python Web 框架篇:Flask、Django、FastAPI介绍及其核心技术
开发语言·前端·后端·python·django·flask·fastapi
黑金IT10 天前
深入FastAPI:掌握使用多个关联模型的高级用法[Union类型]
python·fastapi
黑金IT11 天前
深入理解FastAPI的response_model:自动化数据验证与文档生成
python·fastapi