refactor: tag repo remove site info service

This commit is contained in:
LinkinStar 2022-11-18 19:56:10 +08:00
parent 0467165264
commit dc1f83f819
6 changed files with 130 additions and 122 deletions

View File

@ -6,7 +6,6 @@ import (
"github.com/answerdev/answer/internal/base/data"
"github.com/answerdev/answer/internal/base/reason"
"github.com/answerdev/answer/internal/entity"
"github.com/answerdev/answer/internal/service/siteinfo_common"
"github.com/answerdev/answer/internal/service/tag"
"github.com/answerdev/answer/internal/service/unique"
"github.com/segmentfault/pacman/errors"
@ -15,21 +14,18 @@ import (
// tagRepo tag repository
type tagRepo struct {
data *data.Data
uniqueIDRepo unique.UniqueIDRepo
siteInfoService *siteinfo_common.SiteInfoCommonService
data *data.Data
uniqueIDRepo unique.UniqueIDRepo
}
// NewTagRepo new repository
func NewTagRepo(
data *data.Data,
uniqueIDRepo unique.UniqueIDRepo,
siteInfoService *siteinfo_common.SiteInfoCommonService,
) tag.TagRepo {
return &tagRepo{
data: data,
uniqueIDRepo: uniqueIDRepo,
siteInfoService: siteInfoService,
data: data,
uniqueIDRepo: uniqueIDRepo,
}
}

View File

@ -7,43 +7,29 @@ import (
"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
data *data.Data
uniqueIDRepo unique.UniqueIDRepo
}
// 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,
data: data,
uniqueIDRepo: uniqueIDRepo,
}
}
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)
@ -53,11 +39,6 @@ func (tr *tagCommonRepo) GetTagListByIDs(ctx context.Context, ids []string) (tag
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
if !tr.tagRecommendStatus(ctx) {
for _, tag := range tagList {
tag.Recommend = false
}
}
return
}
@ -70,10 +51,6 @@ func (tr *tagCommonRepo) GetTagBySlugName(ctx context.Context, slugName string)
if err != nil {
return nil, false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
if !tr.tagRecommendStatus(ctx) {
tagInfo.Recommend = false
}
return
}
@ -99,11 +76,6 @@ func (tr *tagCommonRepo) GetTagListByName(ctx context.Context, name string, limi
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
if !tr.tagRecommendStatus(ctx) {
for _, tag := range tagList {
tag.Recommend = false
}
}
return
}
@ -119,11 +91,6 @@ func (tr *tagCommonRepo) GetRecommendTagList(ctx context.Context) (tagList []*en
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
if !tr.tagRecommendStatus(ctx) {
for _, tag := range tagList {
tag.Recommend = false
}
}
return
}
@ -139,11 +106,6 @@ func (tr *tagCommonRepo) GetReservedTagList(ctx context.Context) (tagList []*ent
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
if !tr.tagRecommendStatus(ctx) {
for _, tag := range tagList {
tag.Recommend = false
}
}
return
}
@ -156,11 +118,6 @@ func (tr *tagCommonRepo) GetTagListByNames(ctx context.Context, names []string)
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
if !tr.tagRecommendStatus(ctx) {
for _, tag := range tagList {
tag.Recommend = false
}
}
return
}
@ -175,9 +132,6 @@ func (tr *tagCommonRepo) GetTagByID(ctx context.Context, tagID string) (
if err != nil {
return nil, false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
if !tr.tagRecommendStatus(ctx) {
tag.Recommend = false
}
return
}
@ -208,11 +162,6 @@ func (tr *tagCommonRepo) GetTagPage(ctx context.Context, page, pageSize int, tag
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
if !tr.tagRecommendStatus(ctx) {
for _, tag := range tagList {
tag.Recommend = false
}
}
return
}

View File

@ -539,14 +539,13 @@ func (qs *QuestionService) SimilarQuestion(ctx context.Context, questionID strin
// SearchList
func (qs *QuestionService) SearchList(ctx context.Context, req *schema.QuestionSearch, loginUserID string) ([]*schema.QuestionInfo, int64, error) {
if len(req.Tag) > 0 {
taginfo, has, err := qs.tagCommon.GetTagListByName(ctx, req.Tag)
tagInfo, has, err := qs.tagCommon.GetTagBySlugName(ctx, strings.ToLower(req.Tag))
if err != nil {
log.Error("tagCommon.GetTagListByNames error", err)
}
if has {
req.TagIDs = append(req.TagIDs, taginfo.ID)
req.TagIDs = append(req.TagIDs, tagInfo.ID)
}
}
list := make([]*schema.QuestionInfo, 0)
if req.UserName != "" {

View File

@ -9,27 +9,28 @@ import (
"github.com/answerdev/answer/internal/schema"
"github.com/answerdev/answer/internal/service/activity_common"
"github.com/answerdev/answer/internal/service/search_common"
tagcommon "github.com/answerdev/answer/internal/service/tag_common"
"github.com/answerdev/answer/internal/service/tag_common"
)
type TagSearch struct {
repo search_common.SearchRepo
tagRepo tagcommon.TagCommonRepo
followCommon activity_common.FollowRepo
page int
size int
exp string
w string
userID string
Extra schema.GetTagPageResp
order string
repo search_common.SearchRepo
tagCommonService *tag_common.TagCommonService
followCommon activity_common.FollowRepo
page int
size int
exp string
w string
userID string
Extra schema.GetTagPageResp
order string
}
func NewTagSearch(repo search_common.SearchRepo, tagRepo tagcommon.TagCommonRepo, followCommon activity_common.FollowRepo) *TagSearch {
func NewTagSearch(repo search_common.SearchRepo,
tagCommonService *tag_common.TagCommonService, followCommon activity_common.FollowRepo) *TagSearch {
return &TagSearch{
repo: repo,
tagRepo: tagRepo,
followCommon: followCommon,
repo: repo,
tagCommonService: tagCommonService,
followCommon: followCommon,
}
}
@ -65,7 +66,7 @@ func (ts *TagSearch) Search(ctx context.Context) (resp []schema.SearchResp, tota
tag *entity.Tag
exists, followed bool
)
tag, exists, err = ts.tagRepo.GetTagBySlugName(ctx, ts.exp)
tag, exists, err = ts.tagCommonService.GetTagBySlugName(ctx, ts.exp)
if err != nil {
return
}

View File

@ -6,6 +6,7 @@ import (
"github.com/answerdev/answer/internal/service/revision_common"
"github.com/answerdev/answer/internal/service/siteinfo_common"
"github.com/answerdev/answer/internal/service/tag_common"
"github.com/answerdev/answer/pkg/htmltext"
"github.com/answerdev/answer/internal/base/pager"
@ -14,7 +15,6 @@ import (
"github.com/answerdev/answer/internal/schema"
"github.com/answerdev/answer/internal/service/activity_common"
"github.com/answerdev/answer/internal/service/permission"
tagcommon "github.com/answerdev/answer/internal/service/tag_common"
"github.com/answerdev/answer/pkg/converter"
"github.com/jinzhu/copier"
"github.com/segmentfault/pacman/errors"
@ -30,26 +30,26 @@ type TagRepo interface {
// TagService user service
type TagService struct {
tagRepo TagRepo
tagCommonRepo tagcommon.TagCommonRepo
revisionService *revision_common.RevisionService
followCommon activity_common.FollowRepo
siteInfoService *siteinfo_common.SiteInfoCommonService
tagRepo TagRepo
tagCommonService *tag_common.TagCommonService
revisionService *revision_common.RevisionService
followCommon activity_common.FollowRepo
siteInfoService *siteinfo_common.SiteInfoCommonService
}
// NewTagService new tag service
func NewTagService(
tagRepo TagRepo,
tagCommonRepo tagcommon.TagCommonRepo,
tagCommonService *tag_common.TagCommonService,
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,
tagRepo: tagRepo,
tagCommonService: tagCommonService,
revisionService: revisionService,
followCommon: followCommon,
siteInfoService: siteInfoService,
}
}
@ -74,7 +74,7 @@ func (ts *TagService) UpdateTag(ctx context.Context, req *schema.UpdateTagReq) (
return err
}
tagInfo, exist, err := ts.tagCommonRepo.GetTagByID(ctx, req.TagID)
tagInfo, exist, err := ts.tagCommonService.GetTagByID(ctx, req.TagID)
if err != nil {
return err
}
@ -119,9 +119,9 @@ func (ts *TagService) GetTagInfo(ctx context.Context, req *schema.GetTagInfoReq)
exist bool
)
if len(req.ID) > 0 {
tagInfo, exist, err = ts.tagCommonRepo.GetTagByID(ctx, req.ID)
tagInfo, exist, err = ts.tagCommonService.GetTagByID(ctx, req.ID)
} else {
tagInfo, exist, err = ts.tagCommonRepo.GetTagBySlugName(ctx, req.Name)
tagInfo, exist, err = ts.tagCommonService.GetTagBySlugName(ctx, req.Name)
}
if err != nil {
return nil, err
@ -133,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.tagCommonRepo.GetTagByID(ctx, converter.IntToString(tagInfo.MainTagID))
tagInfo, exist, err = ts.tagCommonService.GetTagByID(ctx, converter.IntToString(tagInfo.MainTagID))
if err != nil {
return nil, err
}
@ -170,7 +170,7 @@ func (ts *TagService) GetFollowingTags(ctx context.Context, userID string) (
if err != nil {
return nil, err
}
tagList, err := ts.tagCommonRepo.GetTagListByIDs(ctx, objIDs)
tagList, err := ts.tagCommonService.GetTagListByIDs(ctx, objIDs)
if err != nil {
return nil, err
}
@ -183,7 +183,7 @@ func (ts *TagService) GetFollowingTags(ctx context.Context, userID string) (
Reserved: t.Reserved,
}
if t.MainTagID > 0 {
mainTag, exist, err := ts.tagCommonRepo.GetTagByID(ctx, converter.IntToString(t.MainTagID))
mainTag, exist, err := ts.tagCommonService.GetTagByID(ctx, converter.IntToString(t.MainTagID))
if err != nil {
return nil, err
}
@ -199,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.tagCommonRepo.GetTagByID(ctx, req.TagID)
tag, exist, err := ts.tagCommonService.GetTagByID(ctx, req.TagID)
if err != nil {
return
}
@ -248,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.tagCommonRepo.GetTagByID(ctx, req.TagID)
mainTagInfo, exist, err := ts.tagCommonService.GetTagByID(ctx, req.TagID)
if err != nil {
return err
}
@ -260,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.tagCommonRepo.GetTagListByNames(ctx, addSynonymTagList)
tagListInDB, err := ts.tagCommonService.GetTagListByNames(ctx, addSynonymTagList)
if err != nil {
return err
}
@ -285,7 +285,7 @@ func (ts *TagService) UpdateTagSynonym(ctx context.Context, req *schema.UpdateTa
}
if len(needAddTagList) > 0 {
err = ts.tagCommonRepo.AddTagList(ctx, needAddTagList)
err = ts.tagCommonService.AddTagList(ctx, needAddTagList)
if err != nil {
return err
}
@ -343,7 +343,7 @@ func (ts *TagService) GetTagWithPage(ctx context.Context, req *schema.GetTagWith
page := req.Page
pageSize := req.PageSize
tags, total, err := ts.tagCommonRepo.GetTagPage(ctx, page, pageSize, tag, req.QueryCond)
tags, total, err := ts.tagCommonService.GetTagPage(ctx, page, pageSize, tag, req.QueryCond)
if err != nil {
return
}

View File

@ -68,6 +68,7 @@ func (ts *TagCommonService) SearchTagLike(ctx context.Context, req *schema.Searc
if err != nil {
return
}
ts.tagsFormatRecommendAndReserved(ctx, tags)
for _, tag := range tags {
item := schema.SearchTagLikeResp{}
item.SlugName = tag.SlugName
@ -151,17 +152,16 @@ func (ts *TagCommonService) SetTagsAttribute(ctx context.Context, tags []string,
return nil
}
// GetTagListByName
func (ts *TagCommonService) GetTagListByName(ctx context.Context, tagName string) (tagInfo *entity.Tag, exist bool, err error) {
tagName = strings.ToLower(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.tagCommonRepo.GetTagListByNames(ctx, tagNames)
tagList, err := ts.tagCommonRepo.GetTagListByNames(ctx, tagNames)
if err != nil {
return nil, err
}
ts.tagsFormatRecommendAndReserved(ctx, tagList)
return tagList, nil
}
func (ts *TagCommonService) ExistRecommend(ctx context.Context, tags []*schema.TagItem) (bool, error) {
@ -194,6 +194,52 @@ func (ts *TagCommonService) GetObjectTag(ctx context.Context, objectId string) (
return ts.TagFormat(ctx, tagsInfoList)
}
// AddTagList get object tag
func (ts *TagCommonService) AddTagList(ctx context.Context, tagList []*entity.Tag) (err error) {
return ts.tagCommonRepo.AddTagList(ctx, tagList)
}
// GetTagByID get object tag
func (ts *TagCommonService) GetTagByID(ctx context.Context, tagID string) (tag *entity.Tag, exist bool, err error) {
tag, exist, err = ts.tagCommonRepo.GetTagByID(ctx, tagID)
if !exist {
return
}
ts.tagFormatRecommendAndReserved(ctx, tag)
return
}
// GetTagBySlugName get object tag
func (ts *TagCommonService) GetTagBySlugName(ctx context.Context, slugName string) (tag *entity.Tag, exist bool, err error) {
tag, exist, err = ts.tagCommonRepo.GetTagBySlugName(ctx, slugName)
if !exist {
return
}
ts.tagFormatRecommendAndReserved(ctx, tag)
return
}
// GetTagListByIDs get object tag
func (ts *TagCommonService) GetTagListByIDs(ctx context.Context, ids []string) (tagList []*entity.Tag, err error) {
tagList, err = ts.tagCommonRepo.GetTagListByIDs(ctx, ids)
if err != nil {
return nil, err
}
ts.tagsFormatRecommendAndReserved(ctx, tagList)
return
}
// GetTagPage get object tag
func (ts *TagCommonService) GetTagPage(ctx context.Context, page, pageSize int, tag *entity.Tag, queryCond string) (
tagList []*entity.Tag, total int64, err error) {
tagList, total, err = ts.tagCommonRepo.GetTagPage(ctx, page, pageSize, tag, queryCond)
if err != nil {
return nil, 0, err
}
ts.tagsFormatRecommendAndReserved(ctx, tagList)
return
}
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)
@ -203,11 +249,10 @@ func (ts *TagCommonService) GetObjectEntityTag(ctx context.Context, objectId str
for _, tag := range tagList {
tagIDList = append(tagIDList, tag.TagID)
}
objTags, err = ts.tagCommonRepo.GetTagListByIDs(ctx, tagIDList)
objTags, err = ts.GetTagListByIDs(ctx, tagIDList)
if err != nil {
return nil, err
}
return objTags, nil
}
@ -225,6 +270,30 @@ func (ts *TagCommonService) TagFormat(ctx context.Context, tags []*entity.Tag) (
return objTags, nil
}
func (ts *TagCommonService) tagsFormatRecommendAndReserved(ctx context.Context, tagList []*entity.Tag) {
tagConfig, err := ts.siteInfoService.GetSiteWrite(ctx)
if err != nil {
log.Error(err)
return
}
if !tagConfig.RequiredTag {
for _, tag := range tagList {
tag.Recommend = false
}
}
}
func (ts *TagCommonService) tagFormatRecommendAndReserved(ctx context.Context, tag *entity.Tag) {
tagConfig, err := ts.siteInfoService.GetSiteWrite(ctx)
if err != nil {
log.Error(err)
return
}
if !tagConfig.RequiredTag {
tag.Recommend = false
}
}
// BatchGetObjectTag batch get object tag
func (ts *TagCommonService) BatchGetObjectTag(ctx context.Context, objectIds []string) (map[string][]*schema.TagResp, error) {
objectIDTagMap := make(map[string][]*schema.TagResp)
@ -238,7 +307,7 @@ func (ts *TagCommonService) BatchGetObjectTag(ctx context.Context, objectIds []s
for _, tag := range tagList {
tagIDList = append(tagIDList, tag.TagID)
}
tagsInfoList, err := ts.tagCommonRepo.GetTagListByIDs(ctx, tagIDList)
tagsInfoList, err := ts.GetTagListByIDs(ctx, tagIDList)
if err != nil {
return objectIDTagMap, err
}
@ -275,14 +344,8 @@ func (ts *TagCommonService) CheckTag(ctx context.Context, tags []string, userID
return nil
}
thisTagNameList := make([]string, 0)
for _, t := range tags {
t = strings.ToLower(t)
thisTagNameList = append(thisTagNameList, t)
}
// find tags name
tagListInDb, err := ts.tagCommonRepo.GetTagListByNames(ctx, thisTagNameList)
tagListInDb, err := ts.GetTagListByNames(ctx, tags)
if err != nil {
return err
}