Skip to content

Commit

Permalink
impl delete duplicate answer
Browse files Browse the repository at this point in the history
  • Loading branch information
kaitoyama committed May 2, 2024
1 parent a4bd5cc commit eb083bd
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions model/respondents_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,19 @@ func (*Respondent) InsertRespondent(ctx context.Context, userID string, question
return 0, fmt.Errorf("failed to get tx: %w", err)
}

var questionnaire Questionnaires
var respondent Respondents

err = db.
Where("id = ?", questionnaireID).
First(&questionnaire).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return 0, ErrRecordNotFound
}
if err != nil {
return 0, fmt.Errorf("failed to get questionnaire: %w", err)
}

if submittedAt.Valid {
respondent = Respondents{
QuestionnaireID: questionnaireID,
Expand All @@ -86,12 +98,28 @@ func (*Respondent) InsertRespondent(ctx context.Context, userID string, question
}
}

if !questionnaire.IsDuplicateAnswerAllowed && submittedAt.Valid {
// delete old answers
err = db.
Where("questionnaire_id = ? AND user_traqid = ?", questionnaireID, userID).
Delete(&Respondents{}).Error
// 既存の回答がなかった場合はそのまま進む
if errors.Is(err, ErrNoRecordDeleted) {
err = nil
}
if err != nil {
return 0, fmt.Errorf("failed to delete old answers: %w", err)
}

}

err = db.Create(&respondent).Error
if err != nil {
return 0, fmt.Errorf("failed to insert a respondent record: %w", err)
}

return respondent.ResponseID, nil

}

// UpdateSubmittedAt 投稿日時更新
Expand Down

0 comments on commit eb083bd

Please sign in to comment.