package lesson

import (
	"context"

	"hotgo/internal/dao"
	"hotgo/internal/model/entity"

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

	v1 "hotgo/api/api/lesson/v1"
)

func (c *ControllerV1) LessonSectionList(ctx context.Context, req *v1.LessonSectionListReq) (res *v1.LessonSectionListRes, err error) {
	var list []*entity.LessonSection
	err = dao.LessonSection.Ctx(ctx).Where("lesson_id = ?", req.LessonId).Scan(&list)
	if err != nil {
		return nil, gerror.NewCode(gcode.CodeDbOperationError, "数据库错误")
	}
	total, err := dao.LessonSection.Ctx(ctx).Where("lesson_id = ?", req.LessonId).Count()
	if err != nil {
		return nil, gerror.NewCode(gcode.CodeDbOperationError, "统计总数失败")
	}
	res = &v1.LessonSectionListRes{
		List:  list,
		Total: total,
	}
	return
}
