setup parser
This commit is contained in:
@ -79,8 +79,78 @@ type ydbCourseRepository struct {
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
func (r *ydbCourseRepository) List(ctx context.Context, params domain.ListCoursesParams) ([]domain.Course, error) {
|
||||
return nil, nil
|
||||
func (r *ydbCourseRepository) List(ctx context.Context, params domain.ListCoursesParams) (courses []domain.Course, err error) {
|
||||
const queryName = "list"
|
||||
|
||||
courses = make([]domain.Course, 0, 4_000)
|
||||
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)
|
||||
xcontext.LogInfo(
|
||||
ctx, r.log,
|
||||
"executed query",
|
||||
slog.String("name", queryName),
|
||||
slog.Duration("elapsed", since),
|
||||
)
|
||||
}()
|
||||
|
||||
_, res, err := s.Execute(
|
||||
ctx,
|
||||
readTx,
|
||||
`SELECT
|
||||
id,
|
||||
external_id,
|
||||
source_type,
|
||||
source_name,
|
||||
organization_id,
|
||||
origin_link,
|
||||
image_link,
|
||||
name,
|
||||
description,
|
||||
full_price,
|
||||
discount,
|
||||
duration,
|
||||
starts_at,
|
||||
created_at,
|
||||
updated_at,
|
||||
deleted_at
|
||||
FROM
|
||||
courses
|
||||
`,
|
||||
table.NewQueryParameters(),
|
||||
options.WithCollectStatsModeBasic(),
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("executing: %w", err)
|
||||
}
|
||||
|
||||
for res.NextResultSet(ctx) {
|
||||
for res.NextRow() {
|
||||
var cdb courseDB
|
||||
_ = res.ScanNamed(cdb.getNamedValues()...)
|
||||
courses = append(courses, mapCourseDB(cdb))
|
||||
}
|
||||
}
|
||||
if err = res.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
},
|
||||
table.WithIdempotent())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return courses, err
|
||||
}
|
||||
|
||||
func (r *ydbCourseRepository) Get(ctx context.Context, id string) (course domain.Course, err error) {
|
||||
@ -194,6 +264,69 @@ func createCourseParamsAsStruct(params domain.CreateCourseParams) types.Value {
|
||||
)
|
||||
}
|
||||
|
||||
func (r *ydbCourseRepository) CreateBatch(ctx context.Context, params ...domain.CreateCourseParams) error {
|
||||
// -- PRAGMA TablePathPrefix("courses");
|
||||
const upsertQuery = `DECLARE $courseData AS List<Struct<
|
||||
id: Text,
|
||||
external_id: Optional<Text>,
|
||||
name: Text,
|
||||
source_type: Text,
|
||||
source_name: Optional<Text>,
|
||||
organization_id: Text,
|
||||
origin_link: Text,
|
||||
image_link: Text,
|
||||
description: Text,
|
||||
full_price: Double,
|
||||
discount: Double,
|
||||
duration: Interval,
|
||||
starts_at: Datetime,
|
||||
created_at: Datetime,
|
||||
updated_at: Datetime,
|
||||
deleted_at: Optional<Datetime>>>;
|
||||
|
||||
REPLACE INTO
|
||||
courses
|
||||
SELECT
|
||||
id,
|
||||
external_id,
|
||||
name,
|
||||
source_type,
|
||||
source_name,
|
||||
organization_id,
|
||||
origin_link,
|
||||
image_link,
|
||||
description,
|
||||
full_price,
|
||||
discount,
|
||||
duration,
|
||||
starts_at,
|
||||
created_at,
|
||||
updated_at,
|
||||
deleted_at
|
||||
FROM AS_TABLE($courseData);`
|
||||
|
||||
writeTx := table.TxControl(
|
||||
table.BeginTx(
|
||||
table.WithSerializableReadWrite(),
|
||||
),
|
||||
table.CommitTx(),
|
||||
)
|
||||
err := r.db.Table().Do(ctx, func(ctx context.Context, s table.Session) error {
|
||||
listValues := mapSlice(params, createCourseParamsAsStruct)
|
||||
queryParams := table.NewQueryParameters(
|
||||
table.ValueParam("$courseData", types.ListValue(listValues...)),
|
||||
)
|
||||
_, _, err := s.Execute(ctx, writeTx, upsertQuery, queryParams)
|
||||
if err != nil {
|
||||
return fmt.Errorf("executing query: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *ydbCourseRepository) Create(ctx context.Context, params domain.CreateCourseParams) (domain.Course, error) {
|
||||
// -- PRAGMA TablePathPrefix("courses");
|
||||
const upsertQuery = `DECLARE $courseData AS List<Struct<
|
||||
@ -381,3 +514,12 @@ func mapCourseDB(cdb courseDB) domain.Course {
|
||||
DeletedAt: nullable.NewValuePtr(cdb.DeletedAt),
|
||||
}
|
||||
}
|
||||
|
||||
func mapSlice[T, U any](in []T, f func(T) U) []U {
|
||||
out := make([]U, len(in))
|
||||
for i, value := range in {
|
||||
out[i] = f(value)
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user