filter by learning_types and course_thematics
This commit is contained in:
@ -179,6 +179,8 @@ func (r *ydbCourseRepository) List(
|
||||
return result, fmt.Errorf("rendering query params: %w", err)
|
||||
}
|
||||
|
||||
xcontext.LogInfo(ctx, r.log, "query prepared", slog.String("query", query), slog.String("args", tableParamOptsToString(opts...)))
|
||||
|
||||
courses := make([]domain.Course, 0, 1_000)
|
||||
readTx := table.TxControl(
|
||||
table.BeginTx(
|
||||
@ -192,7 +194,7 @@ func (r *ydbCourseRepository) List(
|
||||
func(ctx context.Context, s table.Session) error {
|
||||
start := time.Now()
|
||||
defer func() {
|
||||
since := time.Since(start)
|
||||
since := time.Since(start).Truncate(time.Millisecond)
|
||||
xcontext.LogInfo(
|
||||
ctx, r.log,
|
||||
"executed query",
|
||||
@ -242,6 +244,156 @@ func (r *ydbCourseRepository) List(
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (r *ydbCourseRepository) ListLearningTypes(
|
||||
ctx context.Context,
|
||||
) (result domain.ListLearningTypeResult, err error) {
|
||||
const queryName = "list_learning_type"
|
||||
const querySelect = `SELECT DISTINCT learning_type FROM courses;`
|
||||
|
||||
readTx := table.TxControl(
|
||||
table.BeginTx(
|
||||
table.WithOnlineReadOnly(),
|
||||
),
|
||||
table.CommitTx(),
|
||||
)
|
||||
|
||||
err = r.db.Table().Do(
|
||||
ctx,
|
||||
func(ctx context.Context, s table.Session) error {
|
||||
start := time.Now()
|
||||
defer func() {
|
||||
since := time.Since(start).Truncate(time.Millisecond)
|
||||
xcontext.LogInfo(
|
||||
ctx, r.log,
|
||||
"executed query",
|
||||
slog.String("name", queryName),
|
||||
slog.Duration("elapsed", since),
|
||||
)
|
||||
}()
|
||||
|
||||
_, res, err := s.Execute(
|
||||
ctx, readTx, querySelect, table.NewQueryParameters(),
|
||||
options.WithCollectStatsModeNone(),
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("executing query: %w", err)
|
||||
}
|
||||
if !res.NextResultSet(ctx) || !res.HasNextRow() {
|
||||
return nil
|
||||
}
|
||||
|
||||
for res.NextRow() {
|
||||
var learningTypeID string
|
||||
if err = res.Scan(&learningTypeID); err != nil {
|
||||
return fmt.Errorf("scanning row: %w", err)
|
||||
}
|
||||
|
||||
result.LearningTypeIDs = append(result.LearningTypeIDs, learningTypeID)
|
||||
}
|
||||
if err = res.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
xcontext.LogDebug(ctx, r.log, "scanned rows", slog.Int("count", len(result.LearningTypeIDs)))
|
||||
|
||||
return nil
|
||||
},
|
||||
table.WithIdempotent(),
|
||||
)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (r *ydbCourseRepository) ListCourseThematics(
|
||||
ctx context.Context,
|
||||
params domain.ListCourseThematicsParams,
|
||||
) (result domain.ListCourseThematicsResult, err error) {
|
||||
const queryName = "list_course_thematics"
|
||||
|
||||
qtParams := queryTemplateParams{
|
||||
Fields: "DISTINCT course_thematic",
|
||||
Table: "courses",
|
||||
Declares: []queryTemplateDeclaration{},
|
||||
Conditions: []string{},
|
||||
}
|
||||
|
||||
learningTypeValue := types.TextValue(params.LearningTypeID)
|
||||
d := queryTemplateDeclaration{
|
||||
Name: "course_thematic",
|
||||
Type: learningTypeValue.Type().String(),
|
||||
}
|
||||
qtParams.Declares = append(qtParams.Declares, d)
|
||||
qtParams.Conditions = append(qtParams.Conditions, d.Name+"="+d.Arg())
|
||||
|
||||
opts := []table.ParameterOption{
|
||||
table.ValueParam(d.Arg(), learningTypeValue),
|
||||
}
|
||||
|
||||
query, err := qtParams.render()
|
||||
if err != nil {
|
||||
return result, fmt.Errorf("rendering query params: %w", err)
|
||||
}
|
||||
|
||||
readTx := table.TxControl(
|
||||
table.BeginTx(
|
||||
table.WithOnlineReadOnly(),
|
||||
),
|
||||
table.CommitTx(),
|
||||
)
|
||||
|
||||
err = r.db.Table().Do(
|
||||
ctx,
|
||||
func(ctx context.Context, s table.Session) error {
|
||||
start := time.Now()
|
||||
defer func() {
|
||||
since := time.Since(start).Truncate(time.Millisecond)
|
||||
xcontext.LogInfo(
|
||||
ctx, r.log,
|
||||
"executed query",
|
||||
slog.String("name", queryName),
|
||||
slog.Duration("elapsed", since),
|
||||
)
|
||||
}()
|
||||
|
||||
_, res, err := s.Execute(
|
||||
ctx, readTx, query, table.NewQueryParameters(opts...),
|
||||
options.WithCollectStatsModeNone(),
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("executing query: %w", err)
|
||||
}
|
||||
if !res.NextResultSet(ctx) || !res.HasNextRow() {
|
||||
return nil
|
||||
}
|
||||
|
||||
for res.NextRow() {
|
||||
var courseThematicID string
|
||||
if err = res.Scan(&courseThematicID); err != nil {
|
||||
return fmt.Errorf("scanning row: %w", err)
|
||||
}
|
||||
|
||||
result.CourseThematicIDs = append(result.CourseThematicIDs, courseThematicID)
|
||||
}
|
||||
if err = res.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
xcontext.LogDebug(ctx, r.log, "scanned rows", slog.Int("count", len(result.CourseThematicIDs)))
|
||||
|
||||
return nil
|
||||
},
|
||||
table.WithIdempotent(),
|
||||
)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (r *ydbCourseRepository) Get(
|
||||
ctx context.Context,
|
||||
id string,
|
||||
@ -735,15 +887,16 @@ func (p queryTemplateParams) render() (string, error) {
|
||||
const queryTemplateSelect = `{{ range .Declares }}DECLARE ${{.Name}} AS {{.Type}};{{end}}
|
||||
SELECT {{.Fields}}
|
||||
FROM {{.Table}}
|
||||
WHERE {{ range .Conditions }}{{.}}{{end}}
|
||||
WHERE 1=1 {{ range .Conditions }} AND {{.}} {{ end }}
|
||||
{{.Suffix}}`
|
||||
|
||||
var querySelect = template.Must(template.New("").Parse(queryTemplateSelect))
|
||||
|
||||
// func tableParamOptsToString(in ...table.ParameterOption) string {
|
||||
// var sb strings.Builder
|
||||
// for _, opt := range in {
|
||||
// sb.WriteString(opt.Name() + "(" + opt.Value().Type().String() + ");")
|
||||
// }
|
||||
// return sb.String()
|
||||
// }
|
||||
func tableParamOptsToString(in ...table.ParameterOption) string {
|
||||
var sb strings.Builder
|
||||
for _, opt := range in {
|
||||
sb.WriteString(opt.Name() + ":" + opt.Value().Yql() + ";")
|
||||
// sb.WriteString(opt.Name() + " (" + opt.Value().Type().String() + "); ")
|
||||
}
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user