Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

update delete answer or question

aichy1268f38d2a0af43
diff --git a/internal/service/answer_service.go b/internal/service/answer_service.go
index 5d0ba33d..2839e94d 100644
--- a/internal/service/answer_service.go
+++ b/internal/service/answer_service.go
@@ -65,14 +65,20 @@ func NewAnswerService(
}
// RemoveAnswer delete answer
-func (as *AnswerService) RemoveAnswer(ctx context.Context, id string) (err error) {
- answerInfo, exist, err := as.answerRepo.GetByID(ctx, id)
+func (as *AnswerService) RemoveAnswer(ctx context.Context, req *schema.RemoveAnswerReq) (err error) {
+ answerInfo, exist, err := as.answerRepo.GetByID(ctx, req.ID)
if err != nil {
return err
}
if !exist {
return nil
}
+ if answerInfo.UserID != req.UserID {
+ return errors.BadRequest(reason.UnauthorizedError)
+ }
+ if answerInfo.Adopted == schema.AnswerAdoptedEnable {
+ return errors.BadRequest(reason.UnauthorizedError)
+ }
// user add question count
err = as.questionCommon.UpdateAnswerCount(ctx, answerInfo.QuestionID, -1)
@@ -85,7 +91,7 @@ func (as *AnswerService) RemoveAnswer(ctx context.Context, id string) (err error
log.Error("user IncreaseAnswerCount error", err.Error())
}
- err = as.answerRepo.RemoveAnswer(ctx, id)
+ err = as.answerRepo.RemoveAnswer(ctx, req.ID)
if err != nil {
return err
}
diff --git a/internal/service/question_service.go b/internal/service/question_service.go
index 97bceb24..72f885b1 100644
--- a/internal/service/question_service.go
+++ b/internal/service/question_service.go
@@ -163,6 +163,17 @@ func (qs *QuestionService) RemoveQuestion(ctx context.Context, req *schema.Remov
if !has {
return nil
}
+ if questionInfo.UserID != req.UserID {
+ return errors.BadRequest(reason.UnauthorizedError)
+ }
+
+ if questionInfo.AcceptedAnswerID != "" {
+ return errors.BadRequest(reason.UnauthorizedError)
+ }
+ if questionInfo.AnswerCount > 0 {
+ return errors.BadRequest(reason.UnauthorizedError)
+ }
+
questionInfo.Status = entity.QuestionStatusDeleted
err = qs.questionRepo.UpdateQuestionStatus(ctx, questionInfo)
if err != nil {