add simple http

This commit is contained in:
Gitea
2023-12-17 10:01:49 +03:00
parent b3e14f8c45
commit f60ebcfb36
9 changed files with 393 additions and 36 deletions

View File

@ -5,6 +5,8 @@ import (
"fmt"
"log/slog"
"path"
"strings"
"text/template"
"time"
"git.loyso.art/frx/kurious/internal/common/config"
@ -79,39 +81,90 @@ type ydbCourseRepository struct {
log *slog.Logger
}
func (r *ydbCourseRepository) List(ctx context.Context, params domain.ListCoursesParams) (courses []domain.Course, err error) {
func (r *ydbCourseRepository) List(
ctx context.Context,
params domain.ListCoursesParams,
) (result domain.ListCoursesResult, err error) {
const limit = 1000
const queryName = "list"
const query = `
DECLARE $limit AS Int32;
DECLARE $id AS Text;
SELECT
id,
external_id,
source_type,
source_name,
course_thematic,
learning_type,
organization_id,
origin_link,
image_link,
name,
description,
full_price,
discount,
duration,
starts_at,
created_at,
updated_at,
deleted_at
FROM
courses
WHERE
id > $id
ORDER BY id
LIMIT $limit;`
// const query = `
// DECLARE $limit AS Int32;
// DECLARE $id AS Text;
// SELECT
// id,
// external_id,
// source_type,
// source_name,
// course_thematic,
// learning_type,
// organization_id,
// origin_link,
// image_link,
// name,
// description,
// full_price,
// discount,
// duration,
// starts_at,
// created_at,
// updated_at,
// deleted_at
// FROM
// courses
// WHERE
// id > $id
// ORDER BY id
// LIMIT $limit;`
//
const fields = `id, external_id, source_type, source_name, course_thematic, learning_type, organization_id, origin_link, image_link, name, description, full_price, discount, duration, starts_at, created_at, updated_at, deleted_at`
courses = make([]domain.Course, 0, 4_000)
if params.Limit == 0 {
params.Limit = limit
}
qtParams := queryTemplateParams{
Fields: fields,
Table: "courses",
Suffix: "ORDER BY id\nLIMIT $limit",
Declares: []queryTemplateDeclaration{{
Name: "limit",
Type: "Int32",
}, {
Name: "id",
Type: "Text",
}},
Conditions: []string{
"id > $id",
},
}
options := make([]table.ParameterOption, 0, 4)
appendParams := func(name string, value string) {
if value == "" {
return
}
ydbvalue := types.TextValue(value)
d := queryTemplateDeclaration{
Name: name,
Type: ydbvalue.Type().String(),
}
qtParams.Declares = append(qtParams.Declares, d)
qtParams.Conditions = append(qtParams.Conditions, d.Name+"="+d.Arg())
options = append(options, table.ValueParam(d.Arg(), ydbvalue))
}
appendParams("course_thematic", params.CourseThematic)
appendParams("learning_type", params.LearningType)
var sb strings.Builder
err = template.Must(template.New("").Parse(queryTemplateSelect)).Execute(&sb, qtParams)
if err != nil {
return result, fmt.Errorf("executing template: %w", err)
}
query := sb.String()
courses := make([]domain.Course, 0, 1_000)
readTx := table.TxControl(
table.BeginTx(
table.WithOnlineReadOnly(),
@ -508,3 +561,27 @@ func mapSlice[T, U any](in []T, f func(T) U) []U {
return out
}
type queryTemplateDeclaration struct {
Name string
Type string
}
func (d queryTemplateDeclaration) Arg() string {
return "$" + d.Name
}
type queryTemplateParams struct {
Declares []queryTemplateDeclaration
Fields string
Table string
Conditions []string
Suffix string
}
const queryTemplateSelect = `
{{ range .Declares }}DECLARE ${{.Name}} AS {{.Type}}\n{{end}}
SELECT {{.Fields}}
FROM {{.Table}}
WHERE {{ range .Conditions }}{{.}}\n{{end}}
{{.Suffix}}`