change create update querys

This commit is contained in:
Pavel 2024-04-16 22:53:12 +03:00
parent f853a933bb
commit 2ed28b0b77
2 changed files with 31 additions and 26 deletions

@ -212,35 +212,39 @@ func (r *QuestionRepository) GetQuestionList(
// test +
// UpdateQuestion set new data for question
func (r *QuestionRepository) UpdateQuestion(ctx context.Context, record model.Question) error {
query := `UPDATE question
SET %s
WHERE id=$1;`
var values []string
query := `UPDATE question SET`
var params []interface{}
if record.Title != "" {
values = append(values, fmt.Sprintf(` title='%s' `, record.Title))
query += ` title = $1,`
params = append(params, record.Title)
}
if record.Description != "" {
values = append(values, fmt.Sprintf(` description='%s' `, record.Description))
query += ` description = $2,`
params = append(params, record.Description)
}
if record.Type != "" {
values = append(values, fmt.Sprintf(` questiontype='%s' `, record.Type))
query += ` questiontype = $3,`
params = append(params, record.Type)
}
values = append(values, fmt.Sprintf(`required=%t `, record.Required), fmt.Sprintf(` version=%d `, record.Version))
if record.Content != "" {
values = append(values, fmt.Sprintf(` content='%s' `, record.Content))
query += ` content = $4,`
params = append(params, record.Content)
}
if record.Page != -1 {
values = append(values, fmt.Sprintf(` page=%d `, record.Page))
query += ` page = $5,`
params = append(params, record.Page)
}
_, err := r.pool.ExecContext(ctx, fmt.Sprintf(query, strings.Join(values, ",")), record.Id)
query += ` required = $6, version = $7 WHERE id = $8`
params = append(params, record.Required, record.Version, record.Id)
_, err := r.pool.ExecContext(ctx, query, params...)
return err
}

@ -385,33 +385,34 @@ func (r *QuizRepository) MoveToHistoryQuiz(ctx context.Context, id uint64, accou
// test +
// UpdateQuiz set new data for quiz
func (r *QuizRepository) UpdateQuiz(ctx context.Context, accountId string, record model.Quiz) error {
query := `UPDATE quiz
SET %s
WHERE id=$1 AND accountid=$2;`
var values []string
query := `UPDATE quiz SET`
var params []interface{}
if record.Name != "" {
values = append(values, fmt.Sprintf(` name='%s' `, record.Name))
query += ` name = $1,`
params = append(params, record.Name)
}
if record.Description != "" {
values = append(values, fmt.Sprintf(` description='%s' `, record.Description))
query += ` description = $2,`
params = append(params, record.Description)
}
if record.Status != "" {
values = append(values, fmt.Sprintf(` status='%s' `, record.Status))
query += ` status = $3,`
params = append(params, record.Status)
}
values = append(values, fmt.Sprintf(`group_id=%d `, record.GroupId), fmt.Sprintf(` version=%d `, record.Version))
if record.Config != "" {
values = append(values, fmt.Sprintf(` config='%s' `, record.Config))
query += ` config = $4,`
params = append(params, record.Config)
}
fmt.Println("UPQUI", fmt.Sprintf(query, strings.Join(values, ",")))
_, err := r.pool.ExecContext(ctx, fmt.Sprintf(query, strings.Join(values, ",")), record.Id, accountId)
query += ` group_id = $5, version = $6 WHERE id = $7 AND accountid = $8`
params = append(params, record.GroupId, record.Version, record.Id, accountId)
_, err := r.pool.ExecContext(ctx, query, params...)
return err
}