- add a new method 'HasBasicDataToUpdate' to the two inputs: QuestionInput and QualificationInput
- small changes in error handling (qualification.pgRepository.UpdateMany, qualification.pgRepository.Store, profession.pgRepository.UpdateMany, profession.pgRepository.Store)
This commit is contained in:
parent
c7391ec468
commit
12d8fb51f4
|
@ -64,6 +64,14 @@ func (input *QualificationInput) IsEmpty() bool {
|
|||
len(input.DissociateProfession) == 0
|
||||
}
|
||||
|
||||
func (input *QualificationInput) HasBasicDataToUpdate() bool {
|
||||
return input != nil &&
|
||||
(input.Name != nil ||
|
||||
input.Code != nil ||
|
||||
input.Formula != nil ||
|
||||
input.Description != nil)
|
||||
}
|
||||
|
||||
func (input *QualificationInput) Sanitize() *QualificationInput {
|
||||
if input.Name != nil {
|
||||
*input.Name = strings.TrimSpace(*input.Name)
|
||||
|
|
|
@ -81,6 +81,18 @@ func (input *QuestionInput) IsEmpty() bool {
|
|||
input.QualificationID == nil
|
||||
}
|
||||
|
||||
func (input *QuestionInput) HasBasicDataToUpdate() bool {
|
||||
return input != nil &&
|
||||
(input.Content != nil ||
|
||||
input.Explanation != nil ||
|
||||
input.CorrectAnswer != nil ||
|
||||
input.AnswerA != nil ||
|
||||
input.AnswerB != nil ||
|
||||
input.AnswerC != nil ||
|
||||
input.AnswerD != nil ||
|
||||
input.QualificationID != nil)
|
||||
}
|
||||
|
||||
func (input *QuestionInput) Sanitize() *QuestionInput {
|
||||
if input.Content != nil {
|
||||
*input.Content = strings.TrimSpace(*input.Content)
|
||||
|
|
|
@ -36,7 +36,7 @@ func (repo *pgRepository) Store(ctx context.Context, input *models.ProfessionInp
|
|||
Context(ctx).
|
||||
Returning("*").
|
||||
Insert(); err != nil {
|
||||
if strings.Contains(err.Error(), "name") {
|
||||
if strings.Contains(err.Error(), "name") || strings.Contains(err.Error(), "slug") {
|
||||
return nil, errorutils.Wrap(err, messageNameIsAlreadyTaken)
|
||||
}
|
||||
return nil, errorutils.Wrap(err, messageFailedToSaveModel)
|
||||
|
@ -51,7 +51,7 @@ func (repo *pgRepository) UpdateMany(ctx context.Context, f *models.ProfessionFi
|
|||
Apply(input.ApplyUpdate).
|
||||
Apply(f.Where).
|
||||
Update(); err != nil && err != pg.ErrNoRows {
|
||||
if strings.Contains(err.Error(), "name") {
|
||||
if strings.Contains(err.Error(), "name") || strings.Contains(err.Error(), "slug") {
|
||||
return nil, errorutils.Wrap(err, messageNameIsAlreadyTaken)
|
||||
}
|
||||
return nil, errorutils.Wrap(err, messageFailedToSaveModel)
|
||||
|
|
|
@ -43,7 +43,7 @@ func (repo *pgRepository) Store(ctx context.Context, input *models.Qualification
|
|||
Insert(); err != nil {
|
||||
if strings.Contains(err.Error(), "name") {
|
||||
return errorutils.Wrap(err, messageNameIsAlreadyTaken)
|
||||
} else if strings.Contains(err.Error(), "code") {
|
||||
} else if strings.Contains(err.Error(), "code") || strings.Contains(err.Error(), "slug") {
|
||||
return errorutils.Wrap(err, messageCodeIsAlreadyTaken)
|
||||
}
|
||||
return errorutils.Wrap(err, messageFailedToSaveModel)
|
||||
|
@ -70,7 +70,7 @@ func (repo *pgRepository) UpdateMany(
|
|||
) ([]*models.Qualification, error) {
|
||||
items := []*models.Qualification{}
|
||||
err := repo.RunInTransaction(ctx, func(tx *pg.Tx) error {
|
||||
if input.Name != nil || input.Code != nil || input.Description != nil || input.Formula != nil {
|
||||
if input.HasBasicDataToUpdate() {
|
||||
if _, err := tx.
|
||||
Model(&models.Qualification{}).
|
||||
Context(ctx).
|
||||
|
@ -79,7 +79,7 @@ func (repo *pgRepository) UpdateMany(
|
|||
Update(); err != nil && err != pg.ErrNoRows {
|
||||
if strings.Contains(err.Error(), "name") {
|
||||
return errorutils.Wrap(err, messageNameIsAlreadyTaken)
|
||||
} else if strings.Contains(err.Error(), "code") {
|
||||
} else if strings.Contains(err.Error(), "code") || strings.Contains(err.Error(), "slug") {
|
||||
return errorutils.Wrap(err, messageCodeIsAlreadyTaken)
|
||||
}
|
||||
return errorutils.Wrap(err, messageFailedToSaveModel)
|
||||
|
|
Reference in New Issue
Block a user