package lesson

import (
	"context"

	v1 "hotgo/api/api/lesson/v1"
	"hotgo/internal/dao"
	"hotgo/internal/model/entity"

	"github.com/gogf/gf/v2/errors/gcode"
	"github.com/gogf/gf/v2/errors/gerror"
)

func (c *ControllerV1) LessonList(ctx context.Context, req *v1.LessonListReq) (res *v1.LessonListRes, err error) {
	// 查询课程列表
	var list []*entity.Lesson
	err = dao.Lesson.Ctx(ctx).Scan(&list)
	if err != nil {
		return nil, gerror.NewCode(gcode.CodeDbOperationError, "数据库错误")
	}
	// 统计总数
	total, err := dao.Lesson.Ctx(ctx).Count()
	if err != nil {
		return nil, gerror.NewCode(gcode.CodeDbOperationError, "统计总数失败")
	}
	res = &v1.LessonListRes{
		List:  list,
		Total: total,
	}
	return
}
