fix question update time and tag check

This commit is contained in:
aichy126 2022-11-28 18:19:22 +08:00
parent 01537f630e
commit 712f918f9c
3 changed files with 64 additions and 7 deletions

View File

@ -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

View File

@ -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{

View File

@ -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