check tags

This commit is contained in:
aichy126 2022-11-17 19:09:56 +08:00
parent 0519b558b7
commit 41bb72b110
2 changed files with 63 additions and 3 deletions

View File

@ -3,6 +3,7 @@ package service
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/answerdev/answer/internal/base/constant"
@ -225,6 +226,28 @@ func (qs *QuestionService) UpdateQuestion(ctx context.Context, req *schema.Quest
if dbinfo.UserID != req.UserID {
return
}
//CheckChangeTag
oldTags, err := qs.tagCommon.GetObjectEntityTag(ctx, question.ID)
if err != nil {
return
}
tagNameList := make([]string, 0)
for _, tag := range req.Tags {
tagNameList = append(tagNameList, tag.SlugName)
}
Tags, err := qs.tagCommon.GetTagListByNames(ctx, tagNameList)
if err != nil {
return
}
CheckTag, CheckTaglist := qs.CheckChangeTag(ctx, oldTags, Tags)
if !CheckTag {
err = errors.BadRequest(reason.UnauthorizedError).WithMsg(fmt.Sprintf("tag [%s] cannot be modified",
strings.Join(CheckTaglist, ",")))
return
}
//update question to db
err = qs.questionRepo.UpdateQuestion(ctx, question, []string{"title", "original_text", "parsed_text", "updated_at"})
if err != nil {
return
@ -276,6 +299,10 @@ func (qs *QuestionService) ChangeTag(ctx context.Context, objectTagData *schema.
return qs.tagCommon.ObjectChangeTag(ctx, objectTagData)
}
func (qs *QuestionService) CheckChangeTag(ctx context.Context, oldobjectTagData, objectTagData []*entity.Tag) (bool, []string) {
return qs.tagCommon.ObjectCheckChangeTag(ctx, oldobjectTagData, objectTagData)
}
func (qs *QuestionService) SearchUserList(ctx context.Context, userName, order string, page, pageSize int, loginUserID string) ([]*schema.UserQuestionInfo, int64, error) {
userlist := make([]*schema.UserQuestionInfo, 0)

View File

@ -188,7 +188,11 @@ func (ts *TagCommonService) ExistRecommend(ctx context.Context, tags []*schema.T
// GetObjectTag get object tag
func (ts *TagCommonService) GetObjectTag(ctx context.Context, objectId string) (objTags []*schema.TagResp, err error) {
objTags = make([]*schema.TagResp, 0)
tagsInfoList, err := ts.GetObjectEntityTag(ctx, objectId)
return ts.TagFormat(ctx, tagsInfoList)
}
func (ts *TagCommonService) GetObjectEntityTag(ctx context.Context, objectId string) (objTags []*entity.Tag, err error) {
tagIDList := make([]string, 0)
tagList, err := ts.tagRelRepo.GetObjectTagRelList(ctx, objectId)
if err != nil {
@ -197,11 +201,17 @@ func (ts *TagCommonService) GetObjectTag(ctx context.Context, objectId string) (
for _, tag := range tagList {
tagIDList = append(tagIDList, tag.TagID)
}
tagsInfoList, err := ts.tagRepo.GetTagListByIDs(ctx, tagIDList)
objTags, err = ts.tagRepo.GetTagListByIDs(ctx, tagIDList)
if err != nil {
return nil, err
}
for _, tagInfo := range tagsInfoList {
return objTags, nil
}
func (ts *TagCommonService) TagFormat(ctx context.Context, tags []*entity.Tag) (objTags []*schema.TagResp, err error) {
objTags = make([]*schema.TagResp, 0)
for _, tagInfo := range tags {
objTags = append(objTags, &schema.TagResp{
SlugName: tagInfo.SlugName,
DisplayName: tagInfo.DisplayName,
@ -331,6 +341,29 @@ func (ts *TagCommonService) CheckTag(ctx context.Context, tags []string, userID
return nil
}
func (ts *TagCommonService) ObjectCheckChangeTag(ctx context.Context, oldobjectTagData, objectTagData []*entity.Tag) (bool, []string) {
reservedTagsMap := make(map[string]bool)
needTagsMap := make([]string, 0)
for _, tag := range objectTagData {
if tag.Reserved {
reservedTagsMap[tag.SlugName] = true
}
}
for _, tag := range oldobjectTagData {
if tag.Reserved {
_, ok := reservedTagsMap[tag.SlugName]
if !ok {
needTagsMap = append(needTagsMap, tag.SlugName)
}
}
}
if len(needTagsMap) > 0 {
return false, needTagsMap
}
return true, []string{}
}
// ObjectChangeTag change object tag list
func (ts *TagCommonService) ObjectChangeTag(ctx context.Context, objectTagData *schema.TagChange) (err error) {
if len(objectTagData.Tags) == 0 {