From 4f1762f01a24c8280ba38b07a62b1517721a8265 Mon Sep 17 00:00:00 2001 From: aichy126 <16996097+aichy126@users.noreply.github.com> Date: Tue, 22 Nov 2022 16:13:55 +0800 Subject: [PATCH] fix question delete --- i18n/en_US.yaml | 4 ++-- internal/service/question_service.go | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/i18n/en_US.yaml b/i18n/en_US.yaml index 01a89c0a..2c0908ad 100644 --- a/i18n/en_US.yaml +++ b/i18n/en_US.yaml @@ -25,7 +25,7 @@ error: not_found: other: "Answer do not found." cannot_deleted: - other: "Unable to delete answer." + other: "No permission to delete." comment: edit_without_permission: other: "Comment are not allowed to edit." @@ -64,7 +64,7 @@ error: not_found: other: "Question not found." cannot_deleted: - other: "Unable to delete problem." + other: "No permission to delete." rank: fail_to_meet_the_condition: other: "Rank fail to meet the condition." diff --git a/internal/service/question_service.go b/internal/service/question_service.go index 259ae2f2..2f440ca6 100644 --- a/internal/service/question_service.go +++ b/internal/service/question_service.go @@ -167,13 +167,27 @@ func (qs *QuestionService) RemoveQuestion(ctx context.Context, req *schema.Remov return errors.BadRequest(reason.QuestionCannotDeleted) } - if questionInfo.AcceptedAnswerID != "" { + if questionInfo.AcceptedAnswerID != "0" { return errors.BadRequest(reason.QuestionCannotDeleted) } if questionInfo.AnswerCount > 0 { return errors.BadRequest(reason.QuestionCannotDeleted) } + if questionInfo.AnswerCount == 1 { + answersearch := &entity.AnswerSearch{} + answersearch.QuestionID = req.ID + answerList, _, err := qs.questioncommon.AnswerCommon.Search(ctx, answersearch) + if err != nil { + return err + } + for _, answer := range answerList { + if answer.VoteCount > 0 { + return errors.BadRequest(reason.QuestionCannotDeleted) + } + } + } + questionInfo.Status = entity.QuestionStatusDeleted err = qs.questionRepo.UpdateQuestionStatus(ctx, questionInfo) if err != nil {