diff --git a/internal/controller/tag_controller.go b/internal/controller/tag_controller.go index 034bf6e4..33791cb1 100644 --- a/internal/controller/tag_controller.go +++ b/internal/controller/tag_controller.go @@ -7,14 +7,16 @@ import ( "github.com/answerdev/answer/internal/schema" "github.com/answerdev/answer/internal/service/rank" "github.com/answerdev/answer/internal/service/tag" + "github.com/answerdev/answer/internal/service/tag_common" "github.com/gin-gonic/gin" "github.com/segmentfault/pacman/errors" ) // TagController tag controller type TagController struct { - tagService *tag.TagService - rankService *rank.RankService + tagService *tag.TagService + tagCommonService *tag_common.TagCommonService + rankService *rank.RankService } // NewTagController new controller @@ -38,7 +40,7 @@ func (tc *TagController) SearchTagLike(ctx *gin.Context) { } userinfo := middleware.GetUserInfoFromContext(ctx) req.IsAdmin = userinfo.IsAdmin - resp, err := tc.tagService.SearchTagLike(ctx, req) + resp, err := tc.tagCommonService.SearchTagLike(ctx, req) handler.HandleResponse(ctx, err, resp) } diff --git a/internal/repo/provider.go b/internal/repo/provider.go index 54d43c10..dca7a595 100644 --- a/internal/repo/provider.go +++ b/internal/repo/provider.go @@ -22,6 +22,7 @@ import ( "github.com/answerdev/answer/internal/repo/search_common" "github.com/answerdev/answer/internal/repo/site_info" "github.com/answerdev/answer/internal/repo/tag" + "github.com/answerdev/answer/internal/repo/tag_common" "github.com/answerdev/answer/internal/repo/unique" "github.com/answerdev/answer/internal/repo/user" "github.com/google/wire" @@ -53,6 +54,7 @@ var ProviderSetRepo = wire.NewSet( activity.NewQuestionActivityRepo, activity.NewUserActiveActivityRepo, tag.NewTagRepo, + tag_common.NewTagCommonRepo, tag.NewTagRelRepo, collection.NewCollectionRepo, collection.NewCollectionGroupRepo, diff --git a/internal/repo/tag/tag_repo.go b/internal/repo/tag/tag_repo.go index 02ae9c39..36591d45 100644 --- a/internal/repo/tag/tag_repo.go +++ b/internal/repo/tag/tag_repo.go @@ -4,14 +4,12 @@ import ( "context" "github.com/answerdev/answer/internal/base/data" - "github.com/answerdev/answer/internal/base/pager" "github.com/answerdev/answer/internal/base/reason" "github.com/answerdev/answer/internal/entity" "github.com/answerdev/answer/internal/service/siteinfo_common" - tagcommon "github.com/answerdev/answer/internal/service/tag_common" + "github.com/answerdev/answer/internal/service/tag" "github.com/answerdev/answer/internal/service/unique" "github.com/segmentfault/pacman/errors" - "github.com/segmentfault/pacman/log" "xorm.io/builder" ) @@ -27,8 +25,7 @@ func NewTagRepo( data *data.Data, uniqueIDRepo unique.UniqueIDRepo, siteInfoService *siteinfo_common.SiteInfoCommonService, - -) tagcommon.TagRepo { +) tag.TagRepo { return &tagRepo{ data: data, uniqueIDRepo: uniqueIDRepo, @@ -36,152 +33,6 @@ func NewTagRepo( } } -func (tr *tagRepo) tagRecommendStatus(ctx context.Context) bool { - tagconfig, err := tr.siteInfoService.GetSiteWrite(ctx) - if err != nil { - log.Error("siteInfoService.GetSiteWrite error", err) - return false - } - return tagconfig.RequiredTag -} - -// AddTagList add tag -func (tr *tagRepo) AddTagList(ctx context.Context, tagList []*entity.Tag) (err error) { - for _, item := range tagList { - item.ID, err = tr.uniqueIDRepo.GenUniqueIDStr(ctx, item.TableName()) - if err != nil { - return err - } - item.RevisionID = "0" - } - _, err = tr.data.DB.Insert(tagList) - if err != nil { - err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() - } - return -} - -// GetTagListByIDs get tag list all -func (tr *tagRepo) GetTagListByIDs(ctx context.Context, ids []string) (tagList []*entity.Tag, err error) { - tagList = make([]*entity.Tag, 0) - session := tr.data.DB.In("id", ids) - session.Where(builder.Eq{"status": entity.TagStatusAvailable}) - err = session.OrderBy("recommend desc,reserved desc,id desc").Find(&tagList) - if err != nil { - err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() - } - if !tr.tagRecommendStatus(ctx) { - for _, tag := range tagList { - tag.Recommend = false - } - } - return -} - -// GetTagBySlugName get tag by slug name -func (tr *tagRepo) GetTagBySlugName(ctx context.Context, slugName string) (tagInfo *entity.Tag, exist bool, err error) { - tagInfo = &entity.Tag{} - session := tr.data.DB.Where("slug_name = ?", slugName) - session.Where(builder.Eq{"status": entity.TagStatusAvailable}) - exist, err = session.Get(tagInfo) - if err != nil { - return nil, false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() - } - - if !tr.tagRecommendStatus(ctx) { - tagInfo.Recommend = false - } - - return -} - -// GetTagListByName get tag list all like name -func (tr *tagRepo) GetTagListByName(ctx context.Context, name string, limit int, hasReserved bool) (tagList []*entity.Tag, err error) { - tagList = make([]*entity.Tag, 0) - cond := &entity.Tag{} - session := tr.data.DB.Where("") - if name != "" { - session.Where("slug_name LIKE ?", name+"%") - } else { - cond.Recommend = true - } - session.Where(builder.Eq{"status": entity.TagStatusAvailable}) - session.Limit(limit).Asc("slug_name") - if !hasReserved { - cond.Reserved = false - session.UseBool("recommend", "reserved") - } else { - session.UseBool("recommend") - } - err = session.OrderBy("recommend desc,reserved desc,id desc").Find(&tagList, cond) - if err != nil { - err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() - } - if !tr.tagRecommendStatus(ctx) { - for _, tag := range tagList { - tag.Recommend = false - } - } - return -} - -func (tr *tagRepo) GetRecommendTagList(ctx context.Context) (tagList []*entity.Tag, err error) { - tagList = make([]*entity.Tag, 0) - cond := &entity.Tag{} - session := tr.data.DB.Where("") - cond.Recommend = true - // session.Where(builder.Eq{"status": entity.TagStatusAvailable}) - session.Asc("slug_name") - session.UseBool("recommend") - err = session.Find(&tagList, cond) - if err != nil { - err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() - } - if !tr.tagRecommendStatus(ctx) { - for _, tag := range tagList { - tag.Recommend = false - } - } - return -} - -func (tr *tagRepo) GetReservedTagList(ctx context.Context) (tagList []*entity.Tag, err error) { - tagList = make([]*entity.Tag, 0) - cond := &entity.Tag{} - session := tr.data.DB.Where("") - cond.Reserved = true - // session.Where(builder.Eq{"status": entity.TagStatusAvailable}) - session.Asc("slug_name") - session.UseBool("reserved") - err = session.Find(&tagList, cond) - if err != nil { - err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() - } - if !tr.tagRecommendStatus(ctx) { - for _, tag := range tagList { - tag.Recommend = false - } - } - return -} - -// GetTagListByNames get tag list all like name -func (tr *tagRepo) GetTagListByNames(ctx context.Context, names []string) (tagList []*entity.Tag, err error) { - tagList = make([]*entity.Tag, 0) - session := tr.data.DB.In("slug_name", names).UseBool("recommend", "reserved") - // session.Where(builder.Eq{"status": entity.TagStatusAvailable}) - err = session.OrderBy("recommend desc,reserved desc,id desc").Find(&tagList) - if err != nil { - err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() - } - if !tr.tagRecommendStatus(ctx) { - for _, tag := range tagList { - tag.Recommend = false - } - } - return -} - // RemoveTag delete tag func (tr *tagRepo) RemoveTag(ctx context.Context, tagID string) (err error) { session := tr.data.DB.Where(builder.Eq{"id": tagID}) @@ -201,16 +52,6 @@ func (tr *tagRepo) UpdateTag(ctx context.Context, tag *entity.Tag) (err error) { return } -// UpdateTagQuestionCount update tag question count -func (tr *tagRepo) UpdateTagQuestionCount(ctx context.Context, tagID string, questionCount int) (err error) { - cond := &entity.Tag{QuestionCount: questionCount} - _, err = tr.data.DB.Where(builder.Eq{"id": tagID}).MustCols("question_count").Update(cond) - if err != nil { - err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() - } - return -} - // UpdateTagSynonym update synonym tag func (tr *tagRepo) UpdateTagSynonym(ctx context.Context, tagSlugNameList []string, mainTagID int64, mainTagSlugName string, @@ -224,41 +65,6 @@ func (tr *tagRepo) UpdateTagSynonym(ctx context.Context, tagSlugNameList []strin return } -func (tr *tagRepo) UpdateTagsAttribute(ctx context.Context, tags []string, attribute string, value bool) (err error) { - bean := &entity.Tag{} - switch attribute { - case "recommend": - bean.Recommend = value - case "reserved": - bean.Reserved = value - default: - return - } - session := tr.data.DB.In("slug_name", tags).Cols(attribute).UseBool(attribute) - _, err = session.Update(bean) - if err != nil { - err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() - } - return -} - -// GetTagByID get tag one -func (tr *tagRepo) GetTagByID(ctx context.Context, tagID string) ( - tag *entity.Tag, exist bool, err error, -) { - tag = &entity.Tag{} - session := tr.data.DB.Where(builder.Eq{"id": tagID}) - session.Where(builder.Eq{"status": entity.TagStatusAvailable}) - exist, err = session.Get(tag) - if err != nil { - return nil, false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() - } - if !tr.tagRecommendStatus(ctx) { - tag.Recommend = false - } - return -} - // GetTagList get tag list all func (tr *tagRepo) GetTagList(ctx context.Context, tag *entity.Tag) (tagList []*entity.Tag, err error) { tagList = make([]*entity.Tag, 0) @@ -267,45 +73,5 @@ func (tr *tagRepo) GetTagList(ctx context.Context, tag *entity.Tag) (tagList []* if err != nil { err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() } - if !tr.tagRecommendStatus(ctx) { - for _, tag := range tagList { - tag.Recommend = false - } - } - return -} - -// GetTagPage get tag page -func (tr *tagRepo) GetTagPage(ctx context.Context, page, pageSize int, tag *entity.Tag, queryCond string) ( - tagList []*entity.Tag, total int64, err error, -) { - tagList = make([]*entity.Tag, 0) - session := tr.data.DB.NewSession() - - if len(tag.SlugName) > 0 { - session.Where(builder.Or(builder.Like{"slug_name", tag.SlugName}, builder.Like{"display_name", tag.SlugName})) - tag.SlugName = "" - } - session.Where(builder.Eq{"status": entity.TagStatusAvailable}) - session.Where("main_tag_id = 0") // if this tag is synonym, exclude it - - switch queryCond { - case "popular": - session.Desc("question_count") - case "name": - session.Asc("slug_name") - case "newest": - session.Desc("created_at") - } - - total, err = pager.Help(page, pageSize, &tagList, tag, session) - if err != nil { - err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() - } - if !tr.tagRecommendStatus(ctx) { - for _, tag := range tagList { - tag.Recommend = false - } - } return } diff --git a/internal/repo/tag_common/tag_common_repo.go b/internal/repo/tag_common/tag_common_repo.go new file mode 100644 index 00000000..4bdaaf89 --- /dev/null +++ b/internal/repo/tag_common/tag_common_repo.go @@ -0,0 +1,261 @@ +package tag_common + +import ( + "context" + + "github.com/answerdev/answer/internal/base/data" + "github.com/answerdev/answer/internal/base/pager" + "github.com/answerdev/answer/internal/base/reason" + "github.com/answerdev/answer/internal/entity" + "github.com/answerdev/answer/internal/service/siteinfo_common" + tagcommon "github.com/answerdev/answer/internal/service/tag_common" + "github.com/answerdev/answer/internal/service/unique" + "github.com/segmentfault/pacman/errors" + "github.com/segmentfault/pacman/log" + "xorm.io/builder" +) + +// tagCommonRepo tag repository +type tagCommonRepo struct { + data *data.Data + uniqueIDRepo unique.UniqueIDRepo + siteInfoService *siteinfo_common.SiteInfoCommonService +} + +// NewTagCommonRepo new repository +func NewTagCommonRepo( + data *data.Data, + uniqueIDRepo unique.UniqueIDRepo, + siteInfoService *siteinfo_common.SiteInfoCommonService, +) tagcommon.TagCommonRepo { + return &tagCommonRepo{ + data: data, + uniqueIDRepo: uniqueIDRepo, + siteInfoService: siteInfoService, + } +} + +func (tr *tagCommonRepo) tagRecommendStatus(ctx context.Context) bool { + tagconfig, err := tr.siteInfoService.GetSiteWrite(ctx) + if err != nil { + log.Error("siteInfoService.GetSiteWrite error", err) + return false + } + return tagconfig.RequiredTag +} + +// GetTagListByIDs get tag list all +func (tr *tagCommonRepo) GetTagListByIDs(ctx context.Context, ids []string) (tagList []*entity.Tag, err error) { + tagList = make([]*entity.Tag, 0) + session := tr.data.DB.In("id", ids) + session.Where(builder.Eq{"status": entity.TagStatusAvailable}) + err = session.OrderBy("recommend desc,reserved desc,id desc").Find(&tagList) + if err != nil { + err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() + } + if !tr.tagRecommendStatus(ctx) { + for _, tag := range tagList { + tag.Recommend = false + } + } + return +} + +// GetTagBySlugName get tag by slug name +func (tr *tagCommonRepo) GetTagBySlugName(ctx context.Context, slugName string) (tagInfo *entity.Tag, exist bool, err error) { + tagInfo = &entity.Tag{} + session := tr.data.DB.Where("slug_name = ?", slugName) + session.Where(builder.Eq{"status": entity.TagStatusAvailable}) + exist, err = session.Get(tagInfo) + if err != nil { + return nil, false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() + } + + if !tr.tagRecommendStatus(ctx) { + tagInfo.Recommend = false + } + return +} + +// GetTagListByName get tag list all like name +func (tr *tagCommonRepo) GetTagListByName(ctx context.Context, name string, limit int, hasReserved bool) (tagList []*entity.Tag, err error) { + tagList = make([]*entity.Tag, 0) + cond := &entity.Tag{} + session := tr.data.DB.Where("") + if name != "" { + session.Where("slug_name LIKE ?", name+"%") + } else { + cond.Recommend = true + } + session.Where(builder.Eq{"status": entity.TagStatusAvailable}) + session.Limit(limit).Asc("slug_name") + if !hasReserved { + cond.Reserved = false + session.UseBool("recommend", "reserved") + } else { + session.UseBool("recommend") + } + err = session.OrderBy("recommend desc,reserved desc,id desc").Find(&tagList, cond) + if err != nil { + err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() + } + if !tr.tagRecommendStatus(ctx) { + for _, tag := range tagList { + tag.Recommend = false + } + } + return +} + +func (tr *tagCommonRepo) GetRecommendTagList(ctx context.Context) (tagList []*entity.Tag, err error) { + tagList = make([]*entity.Tag, 0) + cond := &entity.Tag{} + session := tr.data.DB.Where("") + cond.Recommend = true + // session.Where(builder.Eq{"status": entity.TagStatusAvailable}) + session.Asc("slug_name") + session.UseBool("recommend") + err = session.Find(&tagList, cond) + if err != nil { + err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() + } + if !tr.tagRecommendStatus(ctx) { + for _, tag := range tagList { + tag.Recommend = false + } + } + return +} + +func (tr *tagCommonRepo) GetReservedTagList(ctx context.Context) (tagList []*entity.Tag, err error) { + tagList = make([]*entity.Tag, 0) + cond := &entity.Tag{} + session := tr.data.DB.Where("") + cond.Reserved = true + // session.Where(builder.Eq{"status": entity.TagStatusAvailable}) + session.Asc("slug_name") + session.UseBool("reserved") + err = session.Find(&tagList, cond) + if err != nil { + err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() + } + if !tr.tagRecommendStatus(ctx) { + for _, tag := range tagList { + tag.Recommend = false + } + } + return +} + +// GetTagListByNames get tag list all like name +func (tr *tagCommonRepo) GetTagListByNames(ctx context.Context, names []string) (tagList []*entity.Tag, err error) { + tagList = make([]*entity.Tag, 0) + session := tr.data.DB.In("slug_name", names).UseBool("recommend", "reserved") + // session.Where(builder.Eq{"status": entity.TagStatusAvailable}) + err = session.OrderBy("recommend desc,reserved desc,id desc").Find(&tagList) + if err != nil { + err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() + } + if !tr.tagRecommendStatus(ctx) { + for _, tag := range tagList { + tag.Recommend = false + } + } + return +} + +// GetTagByID get tag one +func (tr *tagCommonRepo) GetTagByID(ctx context.Context, tagID string) ( + tag *entity.Tag, exist bool, err error, +) { + tag = &entity.Tag{} + session := tr.data.DB.Where(builder.Eq{"id": tagID}) + session.Where(builder.Eq{"status": entity.TagStatusAvailable}) + exist, err = session.Get(tag) + if err != nil { + return nil, false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() + } + if !tr.tagRecommendStatus(ctx) { + tag.Recommend = false + } + return +} + +// GetTagPage get tag page +func (tr *tagCommonRepo) GetTagPage(ctx context.Context, page, pageSize int, tag *entity.Tag, queryCond string) ( + tagList []*entity.Tag, total int64, err error, +) { + tagList = make([]*entity.Tag, 0) + session := tr.data.DB.NewSession() + + if len(tag.SlugName) > 0 { + session.Where(builder.Or(builder.Like{"slug_name", tag.SlugName}, builder.Like{"display_name", tag.SlugName})) + tag.SlugName = "" + } + session.Where(builder.Eq{"status": entity.TagStatusAvailable}) + session.Where("main_tag_id = 0") // if this tag is synonym, exclude it + + switch queryCond { + case "popular": + session.Desc("question_count") + case "name": + session.Asc("slug_name") + case "newest": + session.Desc("created_at") + } + + total, err = pager.Help(page, pageSize, &tagList, tag, session) + if err != nil { + err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() + } + if !tr.tagRecommendStatus(ctx) { + for _, tag := range tagList { + tag.Recommend = false + } + } + return +} + +// AddTagList add tag +func (tr *tagCommonRepo) AddTagList(ctx context.Context, tagList []*entity.Tag) (err error) { + for _, item := range tagList { + item.ID, err = tr.uniqueIDRepo.GenUniqueIDStr(ctx, item.TableName()) + if err != nil { + return err + } + item.RevisionID = "0" + } + _, err = tr.data.DB.Insert(tagList) + if err != nil { + err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() + } + return +} + +// UpdateTagQuestionCount update tag question count +func (tr *tagCommonRepo) UpdateTagQuestionCount(ctx context.Context, tagID string, questionCount int) (err error) { + cond := &entity.Tag{QuestionCount: questionCount} + _, err = tr.data.DB.Where(builder.Eq{"id": tagID}).MustCols("question_count").Update(cond) + if err != nil { + err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() + } + return +} + +func (tr *tagCommonRepo) UpdateTagsAttribute(ctx context.Context, tags []string, attribute string, value bool) (err error) { + bean := &entity.Tag{} + switch attribute { + case "recommend": + bean.Recommend = value + case "reserved": + bean.Reserved = value + default: + return + } + session := tr.data.DB.In("slug_name", tags).Cols(attribute).UseBool(attribute) + _, err = session.Update(bean) + if err != nil { + err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() + } + return +} diff --git a/internal/schema/tag_schema.go b/internal/schema/tag_schema.go index 26f0656e..8e1b6da0 100644 --- a/internal/schema/tag_schema.go +++ b/internal/schema/tag_schema.go @@ -194,8 +194,6 @@ type GetTagSynonymsResp struct { DisplayName string `json:"display_name"` // if main tag slug name is not empty, this tag is synonymous with the main tag MainTagSlugName string `json:"main_tag_slug_name"` - Recommend bool `json:"recommend"` - Reserved bool `json:"reserved"` } // UpdateTagSynonymReq update tag request diff --git a/internal/service/follow/follow_service.go b/internal/service/follow/follow_service.go index e383d119..67e8221b 100644 --- a/internal/service/follow/follow_service.go +++ b/internal/service/follow/follow_service.go @@ -15,7 +15,7 @@ type FollowRepo interface { } type FollowService struct { - tagRepo tagcommon.TagRepo + tagRepo tagcommon.TagCommonRepo followRepo FollowRepo followCommonRepo activity_common.FollowRepo } @@ -23,7 +23,7 @@ type FollowService struct { func NewFollowService( followRepo FollowRepo, followCommonRepo activity_common.FollowRepo, - tagRepo tagcommon.TagRepo, + tagRepo tagcommon.TagCommonRepo, ) *FollowService { return &FollowService{ followRepo: followRepo, diff --git a/internal/service/object_info/object_info.go b/internal/service/object_info/object_info.go index d0749612..aea2208d 100644 --- a/internal/service/object_info/object_info.go +++ b/internal/service/object_info/object_info.go @@ -19,7 +19,7 @@ type ObjService struct { answerRepo answercommon.AnswerRepo questionRepo questioncommon.QuestionRepo commentRepo comment_common.CommentCommonRepo - tagRepo tagcommon.TagRepo + tagRepo tagcommon.TagCommonRepo } // NewObjService new object service @@ -27,7 +27,7 @@ func NewObjService( answerRepo answercommon.AnswerRepo, questionRepo questioncommon.QuestionRepo, commentRepo comment_common.CommentCommonRepo, - tagRepo tagcommon.TagRepo) *ObjService { + tagRepo tagcommon.TagCommonRepo) *ObjService { return &ObjService{ answerRepo: answerRepo, questionRepo: questionRepo, diff --git a/internal/service/search/tag.go b/internal/service/search/tag.go index 6fa59936..2335faa5 100644 --- a/internal/service/search/tag.go +++ b/internal/service/search/tag.go @@ -14,7 +14,7 @@ import ( type TagSearch struct { repo search_common.SearchRepo - tagRepo tagcommon.TagRepo + tagRepo tagcommon.TagCommonRepo followCommon activity_common.FollowRepo page int size int @@ -25,7 +25,7 @@ type TagSearch struct { order string } -func NewTagSearch(repo search_common.SearchRepo, tagRepo tagcommon.TagRepo, followCommon activity_common.FollowRepo) *TagSearch { +func NewTagSearch(repo search_common.SearchRepo, tagRepo tagcommon.TagCommonRepo, followCommon activity_common.FollowRepo) *TagSearch { return &TagSearch{ repo: repo, tagRepo: tagRepo, diff --git a/internal/service/tag/tag_service.go b/internal/service/tag/tag_service.go index 641717c2..dc5729d5 100644 --- a/internal/service/tag/tag_service.go +++ b/internal/service/tag/tag_service.go @@ -21,9 +21,17 @@ import ( "github.com/segmentfault/pacman/log" ) +type TagRepo interface { + RemoveTag(ctx context.Context, tagID string) (err error) + UpdateTag(ctx context.Context, tag *entity.Tag) (err error) + UpdateTagSynonym(ctx context.Context, tagSlugNameList []string, mainTagID int64, mainTagSlugName string) (err error) + GetTagList(ctx context.Context, tag *entity.Tag) (tagList []*entity.Tag, err error) +} + // TagService user service type TagService struct { - tagRepo tagcommon.TagRepo + tagRepo TagRepo + tagCommonRepo tagcommon.TagCommonRepo revisionService *revision_common.RevisionService followCommon activity_common.FollowRepo siteInfoService *siteinfo_common.SiteInfoCommonService @@ -31,34 +39,20 @@ type TagService struct { // NewTagService new tag service func NewTagService( - tagRepo tagcommon.TagRepo, + tagRepo TagRepo, + tagCommonRepo tagcommon.TagCommonRepo, revisionService *revision_common.RevisionService, followCommon activity_common.FollowRepo, siteInfoService *siteinfo_common.SiteInfoCommonService) *TagService { return &TagService{ tagRepo: tagRepo, + tagCommonRepo: tagCommonRepo, revisionService: revisionService, followCommon: followCommon, siteInfoService: siteInfoService, } } -// SearchTagLike get tag list all -func (ts *TagService) SearchTagLike(ctx context.Context, req *schema.SearchTagLikeReq) (resp []schema.SearchTagLikeResp, err error) { - tags, err := ts.tagRepo.GetTagListByName(ctx, req.Tag, 5, req.IsAdmin) - if err != nil { - return - } - for _, tag := range tags { - item := schema.SearchTagLikeResp{} - item.SlugName = tag.SlugName - item.Recommend = tag.Recommend - item.Reserved = tag.Reserved - resp = append(resp, item) - } - return resp, nil -} - // RemoveTag delete tag func (ts *TagService) RemoveTag(ctx context.Context, tagID string) (err error) { // TODO permission @@ -80,7 +74,7 @@ func (ts *TagService) UpdateTag(ctx context.Context, req *schema.UpdateTagReq) ( return err } - tagInfo, exist, err := ts.tagRepo.GetTagByID(ctx, req.TagID) + tagInfo, exist, err := ts.tagCommonRepo.GetTagByID(ctx, req.TagID) if err != nil { return err } @@ -125,9 +119,9 @@ func (ts *TagService) GetTagInfo(ctx context.Context, req *schema.GetTagInfoReq) exist bool ) if len(req.ID) > 0 { - tagInfo, exist, err = ts.tagRepo.GetTagByID(ctx, req.ID) + tagInfo, exist, err = ts.tagCommonRepo.GetTagByID(ctx, req.ID) } else { - tagInfo, exist, err = ts.tagRepo.GetTagBySlugName(ctx, req.Name) + tagInfo, exist, err = ts.tagCommonRepo.GetTagBySlugName(ctx, req.Name) } if err != nil { return nil, err @@ -139,7 +133,7 @@ func (ts *TagService) GetTagInfo(ctx context.Context, req *schema.GetTagInfoReq) resp = &schema.GetTagResp{} // if tag is synonyms get original tag info if tagInfo.MainTagID > 0 { - tagInfo, exist, err = ts.tagRepo.GetTagByID(ctx, converter.IntToString(tagInfo.MainTagID)) + tagInfo, exist, err = ts.tagCommonRepo.GetTagByID(ctx, converter.IntToString(tagInfo.MainTagID)) if err != nil { return nil, err } @@ -176,7 +170,7 @@ func (ts *TagService) GetFollowingTags(ctx context.Context, userID string) ( if err != nil { return nil, err } - tagList, err := ts.tagRepo.GetTagListByIDs(ctx, objIDs) + tagList, err := ts.tagCommonRepo.GetTagListByIDs(ctx, objIDs) if err != nil { return nil, err } @@ -189,7 +183,7 @@ func (ts *TagService) GetFollowingTags(ctx context.Context, userID string) ( Reserved: t.Reserved, } if t.MainTagID > 0 { - mainTag, exist, err := ts.tagRepo.GetTagByID(ctx, converter.IntToString(t.MainTagID)) + mainTag, exist, err := ts.tagCommonRepo.GetTagByID(ctx, converter.IntToString(t.MainTagID)) if err != nil { return nil, err } @@ -205,7 +199,7 @@ func (ts *TagService) GetFollowingTags(ctx context.Context, userID string) ( // GetTagSynonyms get tag synonyms func (ts *TagService) GetTagSynonyms(ctx context.Context, req *schema.GetTagSynonymsReq) ( resp []*schema.GetTagSynonymsResp, err error) { - tag, exist, err := ts.tagRepo.GetTagByID(ctx, req.TagID) + tag, exist, err := ts.tagCommonRepo.GetTagByID(ctx, req.TagID) if err != nil { return } @@ -243,8 +237,6 @@ func (ts *TagService) GetTagSynonyms(ctx context.Context, req *schema.GetTagSyno SlugName: t.SlugName, DisplayName: t.DisplayName, MainTagSlugName: mainTagSlugName, - Recommend: t.Recommend, - Reserved: t.Reserved, }) } return @@ -256,7 +248,7 @@ func (ts *TagService) UpdateTagSynonym(ctx context.Context, req *schema.UpdateTa req.Format() addSynonymTagList := make([]string, 0) removeSynonymTagList := make([]string, 0) - mainTagInfo, exist, err := ts.tagRepo.GetTagByID(ctx, req.TagID) + mainTagInfo, exist, err := ts.tagCommonRepo.GetTagByID(ctx, req.TagID) if err != nil { return err } @@ -268,7 +260,7 @@ func (ts *TagService) UpdateTagSynonym(ctx context.Context, req *schema.UpdateTa for _, item := range req.SynonymTagList { addSynonymTagList = append(addSynonymTagList, item.SlugName) } - tagListInDB, err := ts.tagRepo.GetTagListByNames(ctx, addSynonymTagList) + tagListInDB, err := ts.tagCommonRepo.GetTagListByNames(ctx, addSynonymTagList) if err != nil { return err } @@ -293,7 +285,7 @@ func (ts *TagService) UpdateTagSynonym(ctx context.Context, req *schema.UpdateTa } if len(needAddTagList) > 0 { - err = ts.tagRepo.AddTagList(ctx, needAddTagList) + err = ts.tagCommonRepo.AddTagList(ctx, needAddTagList) if err != nil { return err } @@ -351,7 +343,7 @@ func (ts *TagService) GetTagWithPage(ctx context.Context, req *schema.GetTagWith page := req.Page pageSize := req.PageSize - tags, total, err := ts.tagRepo.GetTagPage(ctx, page, pageSize, tag, req.QueryCond) + tags, total, err := ts.tagCommonRepo.GetTagPage(ctx, page, pageSize, tag, req.QueryCond) if err != nil { return } diff --git a/internal/service/tag_common/tag_common.go b/internal/service/tag_common/tag_common.go index b37e2a83..b14b9476 100644 --- a/internal/service/tag_common/tag_common.go +++ b/internal/service/tag_common/tag_common.go @@ -1,4 +1,4 @@ -package tagcommon +package tag_common import ( "context" @@ -17,22 +17,18 @@ import ( "github.com/segmentfault/pacman/log" ) -type TagRepo interface { +type TagCommonRepo interface { AddTagList(ctx context.Context, tagList []*entity.Tag) (err error) GetTagListByIDs(ctx context.Context, ids []string) (tagList []*entity.Tag, err error) GetTagBySlugName(ctx context.Context, slugName string) (tagInfo *entity.Tag, exist bool, err error) GetTagListByName(ctx context.Context, name string, limit int, hasReserved bool) (tagList []*entity.Tag, err error) GetTagListByNames(ctx context.Context, names []string) (tagList []*entity.Tag, err error) - RemoveTag(ctx context.Context, tagID string) (err error) - UpdateTag(ctx context.Context, tag *entity.Tag) (err error) - UpdateTagQuestionCount(ctx context.Context, tagID string, questionCount int) (err error) - UpdateTagSynonym(ctx context.Context, tagSlugNameList []string, mainTagID int64, mainTagSlugName string) (err error) GetTagByID(ctx context.Context, tagID string) (tag *entity.Tag, exist bool, err error) - GetTagList(ctx context.Context, tag *entity.Tag) (tagList []*entity.Tag, err error) GetTagPage(ctx context.Context, page, pageSize int, tag *entity.Tag, queryCond string) (tagList []*entity.Tag, total int64, err error) GetRecommendTagList(ctx context.Context) (tagList []*entity.Tag, err error) GetReservedTagList(ctx context.Context) (tagList []*entity.Tag, err error) UpdateTagsAttribute(ctx context.Context, tags []string, attribute string, value bool) (err error) + UpdateTagQuestionCount(ctx context.Context, tagID string, questionCount int) (err error) } type TagRelRepo interface { @@ -48,27 +44,43 @@ type TagRelRepo interface { // TagCommonService user service type TagCommonService struct { revisionService *revision_common.RevisionService - tagRepo TagRepo + tagCommonRepo TagCommonRepo tagRelRepo TagRelRepo siteInfoService *siteinfo_common.SiteInfoCommonService } // NewTagCommonService new tag service -func NewTagCommonService(tagRepo TagRepo, tagRelRepo TagRelRepo, +func NewTagCommonService(tagCommonRepo TagCommonRepo, tagRelRepo TagRelRepo, revisionService *revision_common.RevisionService, siteInfoService *siteinfo_common.SiteInfoCommonService, ) *TagCommonService { return &TagCommonService{ - tagRepo: tagRepo, + tagCommonRepo: tagCommonRepo, tagRelRepo: tagRelRepo, revisionService: revisionService, siteInfoService: siteInfoService, } } +// SearchTagLike get tag list all +func (ts *TagCommonService) SearchTagLike(ctx context.Context, req *schema.SearchTagLikeReq) (resp []schema.SearchTagLikeResp, err error) { + tags, err := ts.tagCommonRepo.GetTagListByName(ctx, req.Tag, 5, req.IsAdmin) + if err != nil { + return + } + for _, tag := range tags { + item := schema.SearchTagLikeResp{} + item.SlugName = tag.SlugName + item.Recommend = tag.Recommend + item.Reserved = tag.Reserved + resp = append(resp, item) + } + return resp, nil +} + func (ts *TagCommonService) GetSiteWriteRecommendTag(ctx context.Context) (tags []string, err error) { tags = make([]string, 0) - list, err := ts.tagRepo.GetRecommendTagList(ctx) + list, err := ts.tagCommonRepo.GetRecommendTagList(ctx) for _, item := range list { tags = append(tags, item.SlugName) } @@ -108,40 +120,15 @@ func (ts *TagCommonService) SetSiteWriteTag(ctx context.Context, recommendTags, return nil, nil } -// func (ts *TagCommonService) SetSiteWriteRecommendTag(ctx context.Context, tags []string, userID string) (msg string, err error) { -// err = ts.UpdateTag(ctx, tags, userID) -// if err != nil { -// return err.Error(), err -// } -// err = ts.SetTagsAttribute(ctx, tags, "recommend") -// if err != nil { -// return "", err -// } - -// return "", nil -// } - func (ts *TagCommonService) GetSiteWriteReservedTag(ctx context.Context) (tags []string, err error) { tags = make([]string, 0) - list, err := ts.tagRepo.GetReservedTagList(ctx) + list, err := ts.tagCommonRepo.GetReservedTagList(ctx) for _, item := range list { tags = append(tags, item.SlugName) } return tags, nil } -// func (ts *TagCommonService) SetSiteWriteReservedTag(ctx context.Context, tags []string, userID string) (msg string, err error) { -// err = ts.UpdateTag(ctx, tags, userID) -// if err != nil { -// return err.Error(), err -// } -// err = ts.SetTagsAttribute(ctx, tags, "reserved") -// if err != nil { -// return "", err -// } -// return "", nil -// } - // SetTagsAttribute func (ts *TagCommonService) SetTagsAttribute(ctx context.Context, tags []string, attribute string) (err error) { var tagslist []string @@ -153,11 +140,11 @@ func (ts *TagCommonService) SetTagsAttribute(ctx context.Context, tags []string, default: return } - err = ts.tagRepo.UpdateTagsAttribute(ctx, tagslist, attribute, false) + err = ts.tagCommonRepo.UpdateTagsAttribute(ctx, tagslist, attribute, false) if err != nil { return err } - err = ts.tagRepo.UpdateTagsAttribute(ctx, tags, attribute, true) + err = ts.tagCommonRepo.UpdateTagsAttribute(ctx, tags, attribute, true) if err != nil { return err } @@ -167,14 +154,14 @@ func (ts *TagCommonService) SetTagsAttribute(ctx context.Context, tags []string, // GetTagListByName func (ts *TagCommonService) GetTagListByName(ctx context.Context, tagName string) (tagInfo *entity.Tag, exist bool, err error) { tagName = strings.ToLower(tagName) - return ts.tagRepo.GetTagBySlugName(ctx, tagName) + return ts.tagCommonRepo.GetTagBySlugName(ctx, tagName) } func (ts *TagCommonService) GetTagListByNames(ctx context.Context, tagNames []string) ([]*entity.Tag, error) { for k, tagname := range tagNames { tagNames[k] = strings.ToLower(tagname) } - return ts.tagRepo.GetTagListByNames(ctx, tagNames) + return ts.tagCommonRepo.GetTagListByNames(ctx, tagNames) } func (ts *TagCommonService) ExistRecommend(ctx context.Context, tags []*schema.TagItem) (bool, error) { @@ -201,8 +188,6 @@ func (ts *TagCommonService) ExistRecommend(ctx context.Context, tags []*schema.T return false, nil } -// - // GetObjectTag get object tag func (ts *TagCommonService) GetObjectTag(ctx context.Context, objectId string) (objTags []*schema.TagResp, err error) { tagsInfoList, err := ts.GetObjectEntityTag(ctx, objectId) @@ -218,7 +203,7 @@ func (ts *TagCommonService) GetObjectEntityTag(ctx context.Context, objectId str for _, tag := range tagList { tagIDList = append(tagIDList, tag.TagID) } - objTags, err = ts.tagRepo.GetTagListByIDs(ctx, tagIDList) + objTags, err = ts.tagCommonRepo.GetTagListByIDs(ctx, tagIDList) if err != nil { return nil, err } @@ -253,7 +238,7 @@ func (ts *TagCommonService) BatchGetObjectTag(ctx context.Context, objectIds []s for _, tag := range tagList { tagIDList = append(tagIDList, tag.TagID) } - tagsInfoList, err := ts.tagRepo.GetTagListByIDs(ctx, tagIDList) + tagsInfoList, err := ts.tagCommonRepo.GetTagListByIDs(ctx, tagIDList) if err != nil { return objectIDTagMap, err } @@ -297,7 +282,7 @@ func (ts *TagCommonService) CheckTag(ctx context.Context, tags []string, userID } // find tags name - tagListInDb, err := ts.tagRepo.GetTagListByNames(ctx, thisTagNameList) + tagListInDb, err := ts.tagCommonRepo.GetTagListByNames(ctx, thisTagNameList) if err != nil { return err } @@ -380,7 +365,7 @@ func (ts *TagCommonService) ObjectChangeTag(ctx context.Context, objectTagData * } // find tags name - tagListInDb, err := ts.tagRepo.GetTagListByNames(ctx, thisObjTagNameList) + tagListInDb, err := ts.tagCommonRepo.GetTagListByNames(ctx, thisObjTagNameList) if err != nil { return err } @@ -407,7 +392,7 @@ func (ts *TagCommonService) ObjectChangeTag(ctx context.Context, objectTagData * } if len(addTagList) > 0 { - err = ts.tagRepo.AddTagList(ctx, addTagList) + err = ts.tagCommonRepo.AddTagList(ctx, addTagList) if err != nil { return err } @@ -441,7 +426,7 @@ func (ts *TagCommonService) RefreshTagQuestionCount(ctx context.Context, tagIDs if err != nil { return err } - err = ts.tagRepo.UpdateTagQuestionCount(ctx, tagID, int(count)) + err = ts.tagCommonRepo.UpdateTagQuestionCount(ctx, tagID, int(count)) if err != nil { return err }