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.

fix question update time and tag check

aichy126712f918f9cf3
diff --git a/internal/service/answer_service.go b/internal/service/answer_service.go
index f34508aa..bda09447 100644
--- a/internal/service/answer_service.go
+++ b/internal/service/answer_service.go
@@ -217,6 +217,11 @@ func (as *AnswerService) Update(ctx context.Context, req *schema.AnswerUpdateReq
return "", nil
}
+ //If the content is the same, ignore it
+ if answerInfo.OriginalText == req.Content {
+ return "", nil
+ }
+
now := time.Now()
insertData := new(entity.Answer)
insertData.ID = req.ID
diff --git a/internal/service/question_service.go b/internal/service/question_service.go
index 8ae2d1da..08c84b0b 100644
--- a/internal/service/question_service.go
+++ b/internal/service/question_service.go
@@ -120,7 +120,7 @@ func (qs *QuestionService) CloseMsgList(ctx context.Context, lang i18n.Language)
}
// AddQuestion add question
-func (qs *QuestionService) AddQuestion(ctx context.Context, req *schema.QuestionAdd) (questionInfo *schema.QuestionInfo, err error) {
+func (qs *QuestionService) AddQuestion(ctx context.Context, req *schema.QuestionAdd) (questionInfo any, err error) {
recommendExist, err := qs.tagCommon.ExistRecommend(ctx, req.Tags)
if err != nil {
return
@@ -131,6 +131,16 @@ func (qs *QuestionService) AddQuestion(ctx context.Context, req *schema.Question
return
}
+ if !recommendExist {
+ errorlist := make([]*validator.FormErrorField, 0)
+ errorlist = append(errorlist, &validator.FormErrorField{
+ ErrorField: "tags",
+ ErrorMsg: reason.RecommendTagEnter,
+ })
+ err = errors.BadRequest(reason.RecommendTagEnter)
+ return errorlist, err
+ }
+
question := &entity.Question{}
now := time.Now()
question.UserID = req.UserID
@@ -143,7 +153,7 @@ func (qs *QuestionService) AddQuestion(ctx context.Context, req *schema.Question
question.Status = entity.QuestionStatusAvailable
question.RevisionID = "0"
question.CreatedAt = now
- question.UpdatedAt = now
+ // question.UpdatedAt = now
err = qs.questionRepo.AddQuestion(ctx, question)
if err != nil {
return
@@ -270,6 +280,7 @@ func (qs *QuestionService) UpdateQuestion(ctx context.Context, req *schema.Quest
_, existUnreviewed, err := qs.revisionService.ExistUnreviewedByObjectID(ctx, req.ID)
if err != nil {
return
+
}
if existUnreviewed {
err = errors.BadRequest(reason.QuestionCannotUpdate)
@@ -292,10 +303,27 @@ func (qs *QuestionService) UpdateQuestion(ctx context.Context, req *schema.Quest
return
}
+ oldTags, tagerr := qs.tagCommon.GetObjectEntityTag(ctx, question.ID)
+ if tagerr != nil {
+ return questionInfo, tagerr
+ }
+
tagNameList := make([]string, 0)
+ oldtagNameList := make([]string, 0)
for _, tag := range req.Tags {
tagNameList = append(tagNameList, tag.SlugName)
}
+ for _, tag := range oldTags {
+ oldtagNameList = append(oldtagNameList, tag.SlugName)
+ }
+
+ isChange := qs.tagCommon.CheckTagsIsChange(ctx, tagNameList, oldtagNameList)
+
+ //If the content is the same, ignore it
+ if dbinfo.Title == req.Title && dbinfo.OriginalText == req.Content && !isChange {
+ return
+ }
+
Tags, tagerr := qs.tagCommon.GetTagListByNames(ctx, tagNameList)
if tagerr != nil {
return questionInfo, tagerr
@@ -304,14 +332,10 @@ func (qs *QuestionService) UpdateQuestion(ctx context.Context, req *schema.Quest
// If it's not admin
if !req.IsAdmin {
//CheckChangeTag
- oldTags, tagerr := qs.tagCommon.GetObjectEntityTag(ctx, question.ID)
- if tagerr != nil {
- return questionInfo, tagerr
- }
CheckTag, CheckTaglist := qs.CheckChangeReservedTag(ctx, oldTags, Tags)
if !CheckTag {
- errMsg := fmt.Sprintf(`The reserved tag %s must be present.`,
+ errMsg := fmt.Sprintf(`The reserved tag "%s" must be present.`,
strings.Join(CheckTaglist, ","))
errorlist := make([]*validator.FormErrorField, 0)
errorlist = append(errorlist, &validator.FormErrorField{
diff --git a/internal/service/tag_common/tag_common.go b/internal/service/tag_common/tag_common.go
index 557b61d9..5a2d59fe 100644
--- a/internal/service/tag_common/tag_common.go
+++ b/internal/service/tag_common/tag_common.go
@@ -419,6 +419,30 @@ func (ts *TagCommonService) CheckTag(ctx context.Context, tags []string, userID
return nil
}
+// CheckTagsIsChange
+func (ts *TagCommonService) CheckTagsIsChange(ctx context.Context, tagNameList, oldtagNameList []string) bool {
+ check := make(map[string]bool)
+ if len(tagNameList) != len(oldtagNameList) {
+ return true
+ }
+ for _, item := range tagNameList {
+ check[item] = false
+ }
+ for _, item := range oldtagNameList {
+ _, ok := check[item]
+ if !ok {
+ return true
+ }
+ check[item] = true
+ }
+ for _, value := range check {
+ if value == false {
+ return true
+ }
+ }
+ return false
+}
+
func (ts *TagCommonService) CheckChangeReservedTag(ctx context.Context, oldobjectTagData, objectTagData []*entity.Tag) (bool, []string) {
reservedTagsMap := make(map[string]bool)
needTagsMap := make([]string, 0)
@@ -616,6 +640,10 @@ func (ts *TagCommonService) UpdateTag(ctx context.Context, req *schema.UpdateTag
if !exist {
return errors.BadRequest(reason.TagNotFound)
}
+ //If the content is the same, ignore it
+ if tagInfo.OriginalText == req.OriginalText {
+ return nil
+ }
tagInfo.SlugName = req.SlugName
tagInfo.DisplayName = req.DisplayName