question - pg_repository - add a new method GenerateTest
This commit is contained in:
parent
7ae94f16a0
commit
4746b4c153
|
@ -88,7 +88,7 @@ func createSchema(db *pg.DB) error {
|
|||
}
|
||||
if total == 0 {
|
||||
activated := true
|
||||
pswd, err := password.Generate(16, 4, 4, true, false)
|
||||
pswd, err := password.Generate(15, 4, 2, true, false)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "createSchema")
|
||||
}
|
||||
|
|
|
@ -75,6 +75,8 @@ func (repo *pgRepository) UpdateOneByID(ctx context.Context, id int, input *mode
|
|||
}
|
||||
|
||||
repo.saveImages(item, input)
|
||||
repo.deleteImagesBasedOnInput(item, input)
|
||||
|
||||
if _, err := baseQuery.
|
||||
Clone().
|
||||
Set("image = ?", item.Image).
|
||||
|
@ -85,7 +87,6 @@ func (repo *pgRepository) UpdateOneByID(ctx context.Context, id int, input *mode
|
|||
Update(); err != nil && err != pg.ErrNoRows {
|
||||
return nil, errorutils.Wrap(err, messageFailedToSaveModel)
|
||||
}
|
||||
repo.deleteImagesBasedOnInput(item, input)
|
||||
|
||||
return item, nil
|
||||
}
|
||||
|
@ -127,3 +128,20 @@ func (repo *pgRepository) Fetch(ctx context.Context, cfg *question.FetchConfig)
|
|||
}
|
||||
return items, total, nil
|
||||
}
|
||||
|
||||
func (repo *pgRepository) GenerateTest(ctx context.Context, cfg *question.GenerateTestConfig) ([]*models.Question, error) {
|
||||
subquery := repo.
|
||||
Model(&models.Question{}).
|
||||
Column("id").
|
||||
Where(sqlutils.BuildConditionArray("qualification_id"), cfg.Qualifications).
|
||||
Limit(cfg.Limit)
|
||||
items := []*models.Question{}
|
||||
if err := repo.
|
||||
Model(&items).
|
||||
Context(ctx).
|
||||
Where(sqlutils.BuildConditionIn("id"), subquery).
|
||||
Select(); err != nil && err != pg.ErrNoRows {
|
||||
return nil, errorutils.Wrap(err, messageFailedToFetchModel)
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
|
|
@ -46,6 +46,10 @@ func BuildConditionIEQ(column string) string {
|
|||
return column + " ILIKE ?"
|
||||
}
|
||||
|
||||
func BuildConditionIn(column string) string {
|
||||
return column + " IN (?)"
|
||||
}
|
||||
|
||||
func BuildConditionArray(column string) string {
|
||||
return column + " = ANY(?)"
|
||||
}
|
||||
|
|
Reference in New Issue