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 question delete

aichy126f216536e263d
diff --git a/internal/controller/question_controller.go b/internal/controller/question_controller.go
index 281a7e1b..ff8a6eba 100644
--- a/internal/controller/question_controller.go
+++ b/internal/controller/question_controller.go
@@ -46,6 +46,8 @@ func (qc *QuestionController) RemoveQuestion(ctx *gin.Context) {
handler.HandleResponse(ctx, err, errors.Forbidden(reason.RankFailToMeetTheCondition))
return
}
+ userinfo := middleware.GetUserInfoFromContext(ctx)
+ req.IsAdmin = userinfo.IsAdmin
err := qc.questionService.RemoveQuestion(ctx, req)
handler.HandleResponse(ctx, err, nil)
diff --git a/internal/schema/question_schema.go b/internal/schema/question_schema.go
index 29b85be8..9005f07a 100644
--- a/internal/schema/question_schema.go
+++ b/internal/schema/question_schema.go
@@ -3,8 +3,9 @@ package schema
// RemoveQuestionReq delete question request
type RemoveQuestionReq struct {
// question id
- ID string `validate:"required" comment:"question id" json:"id"`
- UserID string `json:"-" ` // user_id
+ ID string `validate:"required" comment:"question id" json:"id"`
+ UserID string `json:"-" ` // user_id
+ IsAdmin bool `json:"-"`
}
type CloseQuestionReq struct {
@@ -169,7 +170,7 @@ type CmsQuestionSearch struct {
Page int `json:"page" form:"page"` // Query number of pages
PageSize int `json:"page_size" form:"page_size"` // Search page size
Status int `json:"-" form:"-"`
- StatusStr string `json:"status" form:"status"` // Status 1 Available 2 closed 10 UserDeleted
+ StatusStr string `json:"status" form:"status"` // Status 1 Available 2 closed 10 UserDeleted
Query string `validate:"omitempty,gt=0,lte=100" json:"query" form:"query" ` //Query string
}
diff --git a/internal/service/question_service.go b/internal/service/question_service.go
index 4abfe08a..931bb940 100644
--- a/internal/service/question_service.go
+++ b/internal/service/question_service.go
@@ -174,27 +174,29 @@ func (qs *QuestionService) RemoveQuestion(ctx context.Context, req *schema.Remov
if !has {
return nil
}
- if questionInfo.UserID != req.UserID {
- return errors.BadRequest(reason.QuestionCannotDeleted)
- }
-
- if questionInfo.AcceptedAnswerID != "0" {
- return errors.BadRequest(reason.QuestionCannotDeleted)
- }
- if questionInfo.AnswerCount > 0 {
- return errors.BadRequest(reason.QuestionCannotDeleted)
- }
+ if !req.IsAdmin {
+ if questionInfo.UserID != req.UserID {
+ 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
+ if questionInfo.AcceptedAnswerID != "0" {
+ return errors.BadRequest(reason.QuestionCannotDeleted)
+ }
+ if questionInfo.AnswerCount > 0 {
+ return errors.BadRequest(reason.QuestionCannotDeleted)
}
- for _, answer := range answerList {
- if answer.VoteCount > 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)
+ }
}
}
}