able to update desc for course
This commit is contained in:
@ -151,12 +151,18 @@ func (r *ydbCourseRepository) List(
|
||||
appendTextParam("course_thematic", params.CourseThematic)
|
||||
appendTextParam("learning_type", params.LearningType)
|
||||
|
||||
opts = append(
|
||||
opts,
|
||||
table.ValueParam("$id", types.TextValue(params.NextPageToken)),
|
||||
table.ValueParam("$limit", types.Int32Value(int32(params.Limit))),
|
||||
)
|
||||
|
||||
query, err := qtParams.render()
|
||||
if err != nil {
|
||||
return result, fmt.Errorf("rendering: %w", err)
|
||||
}
|
||||
|
||||
xcontext.LogInfo(ctx, r.log, "planning to run query", slog.String("query", query), slog.Any("opts", opts))
|
||||
xcontext.LogInfo(ctx, r.log, "planning to run query", slog.String("query", query), slog.String("opts", tableParamOptsToString(opts...)))
|
||||
|
||||
courses := make([]domain.Course, 0, 1_000)
|
||||
readTx := table.TxControl(
|
||||
@ -319,10 +325,31 @@ func (r *ydbCourseRepository) GetByExternalID(ctx context.Context, id string) (d
|
||||
return domain.Course{}, nil
|
||||
}
|
||||
|
||||
func createCourseParamsAsStruct(params domain.CreateCourseParams) types.Value {
|
||||
st := mapSourceTypeFromDomain(params.SourceType)
|
||||
type updateCourseParams struct {
|
||||
domain.CreateCourseParams
|
||||
|
||||
CreatedAt time.Time
|
||||
DeletedAt nullable.Value[time.Time]
|
||||
}
|
||||
|
||||
func updateCourseParamsAsStruct(params updateCourseParams) types.Value {
|
||||
opts := createCourseParamsAsStructValues(params.CreateCourseParams)
|
||||
now := time.Now()
|
||||
return types.StructValue(
|
||||
append(
|
||||
opts[:len(opts)-3],
|
||||
types.StructFieldValue("created_at", types.DatetimeValueFromTime(params.CreatedAt)),
|
||||
types.StructFieldValue("updated_at", types.DatetimeValueFromTime(now)),
|
||||
types.StructFieldValue("deleted_at", types.NullableDatetimeValue(nil)),
|
||||
)...,
|
||||
)
|
||||
}
|
||||
|
||||
func createCourseParamsAsStructValues(params domain.CreateCourseParams) []types.StructValueOption {
|
||||
st := mapSourceTypeFromDomain(params.SourceType)
|
||||
now := time.Now()
|
||||
|
||||
return []types.StructValueOption{
|
||||
types.StructFieldValue("id", types.TextValue(params.ID)),
|
||||
types.StructFieldValue("name", types.TextValue(params.Name)),
|
||||
types.StructFieldValue("source_type", types.TextValue(st)),
|
||||
@ -341,6 +368,12 @@ func createCourseParamsAsStruct(params domain.CreateCourseParams) types.Value {
|
||||
types.StructFieldValue("created_at", types.DatetimeValueFromTime(now)),
|
||||
types.StructFieldValue("updated_at", types.DatetimeValueFromTime(now)),
|
||||
types.StructFieldValue("deleted_at", types.NullableDatetimeValue(nil)),
|
||||
}
|
||||
}
|
||||
|
||||
func createCourseParamsAsStruct(params domain.CreateCourseParams) types.Value {
|
||||
return types.StructValue(
|
||||
createCourseParamsAsStructValues(params)...,
|
||||
)
|
||||
}
|
||||
|
||||
@ -424,6 +457,100 @@ func (r *ydbCourseRepository) Delete(ctx context.Context, id string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ydbCourseRepository) UpdateCourseDescription(ctx context.Context, id, description string) error {
|
||||
course, err := r.Get(ctx, id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("getting course: %w", err)
|
||||
}
|
||||
|
||||
params := updateCourseParams{
|
||||
CreateCourseParams: domain.CreateCourseParams{
|
||||
ID: course.ID,
|
||||
ExternalID: course.ExternalID,
|
||||
Name: course.Name,
|
||||
SourceType: course.SourceType,
|
||||
SourceName: course.SourceName,
|
||||
CourseThematic: course.Thematic,
|
||||
LearningType: course.LearningType,
|
||||
OrganizationID: course.OrganizationID,
|
||||
OriginLink: course.OriginLink,
|
||||
ImageLink: course.ImageLink,
|
||||
Description: description,
|
||||
FullPrice: course.FullPrice,
|
||||
Discount: course.Discount,
|
||||
Duration: course.Duration,
|
||||
StartsAt: course.StartsAt,
|
||||
},
|
||||
CreatedAt: course.CreatedAt,
|
||||
DeletedAt: course.DeletedAt,
|
||||
}
|
||||
|
||||
updateStruct := updateCourseParamsAsStruct(params)
|
||||
|
||||
const upsertQuery = `DECLARE $courseData AS List<Struct<
|
||||
id: Text,
|
||||
external_id: Optional<Text>,
|
||||
name: Text,
|
||||
source_type: Text,
|
||||
source_name: Optional<Text>,
|
||||
course_thematic: Text,
|
||||
learning_type: 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,
|
||||
course_thematic,
|
||||
learning_type,
|
||||
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 {
|
||||
queryParams := table.NewQueryParameters(
|
||||
table.ValueParam("$courseData", types.ListValue(updateStruct)),
|
||||
)
|
||||
_, _, err := s.Execute(ctx, writeTx, upsertQuery, queryParams)
|
||||
if err != nil {
|
||||
return fmt.Errorf("executing query: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *ydbCourseRepository) CreateCourseTable(ctx context.Context) error {
|
||||
return r.db.Table().Do(ctx, func(ctx context.Context, s table.Session) error {
|
||||
return s.CreateTable(
|
||||
@ -595,3 +722,12 @@ WHERE {{ range .Conditions }}{{.}}{{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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user