Skip to content

Commit c68eff2

Browse files
committed
refactor(tag): refactor search tags interface
1 parent f7602dd commit c68eff2

3 files changed

Lines changed: 42 additions & 29 deletions

File tree

internal/controller/tag_controller.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ func (tc *TagController) SearchTagLike(ctx *gin.Context) {
4545
if handler.BindAndCheck(ctx, req) {
4646
return
4747
}
48-
req.IsAdmin = middleware.GetIsAdminFromContext(ctx)
4948
resp, err := tc.tagCommonService.SearchTagLike(ctx, req)
5049
handler.HandleResponse(ctx, err, resp)
5150
}

internal/repo/tag_common/tag_common_repo.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package tag_common
33
import (
44
"context"
55
"fmt"
6+
"strings"
67

78
"github.com/answerdev/answer/internal/base/data"
89
"github.com/answerdev/answer/internal/base/pager"
@@ -56,19 +57,28 @@ func (tr *tagCommonRepo) GetTagBySlugName(ctx context.Context, slugName string)
5657
}
5758

5859
// GetTagListByName get tag list all like name
59-
func (tr *tagCommonRepo) GetTagListByName(ctx context.Context, name string, hasReserved bool) (tagList []*entity.Tag, err error) {
60-
tagList = make([]*entity.Tag, 0)
60+
func (tr *tagCommonRepo) GetTagListByName(ctx context.Context, name string, recommend, reserved bool) (tagList []*entity.Tag, err error) {
6161
cond := &entity.Tag{}
62-
session := tr.data.DB.Context(ctx).Where("")
63-
if name != "" {
64-
session.Where("slug_name LIKE LOWER(?) or display_name LIKE ?", name+"%", name+"%")
65-
} else {
66-
session.UseBool("recommend")
62+
session := tr.data.DB.Context(ctx)
63+
if len(name) > 0 {
64+
session.Where("slug_name LIKE ? OR display_name LIKE ?", strings.ToLower(name)+"%", name+"%")
65+
}
66+
var columns []string
67+
if recommend {
68+
columns = append(columns, "recommend")
6769
cond.Recommend = true
6870
}
71+
if reserved {
72+
columns = append(columns, "reserved")
73+
cond.Reserved = true
74+
}
75+
if len(columns) > 0 {
76+
session.UseBool(columns...)
77+
}
6978
session.Where(builder.Eq{"status": entity.TagStatusAvailable})
70-
session.Asc("slug_name")
71-
err = session.OrderBy("recommend desc,reserved desc,id desc").Find(&tagList, cond)
79+
80+
tagList = make([]*entity.Tag, 0)
81+
err = session.OrderBy("recommend DESC,reserved DESC,slug_name ASC").Find(&tagList, cond)
7282
if err != nil {
7383
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
7484
}

internal/service/tag_common/tag_common.go

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type TagCommonRepo interface {
2424
AddTagList(ctx context.Context, tagList []*entity.Tag) (err error)
2525
GetTagListByIDs(ctx context.Context, ids []string) (tagList []*entity.Tag, err error)
2626
GetTagBySlugName(ctx context.Context, slugName string) (tagInfo *entity.Tag, exist bool, err error)
27-
GetTagListByName(ctx context.Context, name string, hasReserved bool) (tagList []*entity.Tag, err error)
27+
GetTagListByName(ctx context.Context, name string, recommend, reserved bool) (tagList []*entity.Tag, err error)
2828
GetTagListByNames(ctx context.Context, names []string) (tagList []*entity.Tag, err error)
2929
GetTagByID(ctx context.Context, tagID string, includeDeleted bool) (tag *entity.Tag, exist bool, err error)
3030
GetTagPage(ctx context.Context, page, pageSize int, tag *entity.Tag, queryCond string) (tagList []*entity.Tag, total int64, err error)
@@ -86,7 +86,7 @@ func NewTagCommonService(
8686

8787
// SearchTagLike get tag list all
8888
func (ts *TagCommonService) SearchTagLike(ctx context.Context, req *schema.SearchTagLikeReq) (resp []schema.SearchTagLikeResp, err error) {
89-
tags, err := ts.tagCommonRepo.GetTagListByName(ctx, req.Tag, req.IsAdmin)
89+
tags, err := ts.tagCommonRepo.GetTagListByName(ctx, req.Tag, len(req.Tag) == 0, false)
9090
if err != nil {
9191
return
9292
}
@@ -97,35 +97,39 @@ func (ts *TagCommonService) SearchTagLike(ctx context.Context, req *schema.Searc
9797
mainTagId = append(mainTagId, converter.IntToString(tag.MainTagID))
9898
}
9999
}
100-
mainTagList, err := ts.tagCommonRepo.GetTagListByIDs(ctx, mainTagId)
101-
if err != nil {
102-
return
103-
}
104100
mainTagMap := make(map[string]*entity.Tag)
105-
for _, tag := range mainTagList {
106-
mainTagMap[tag.ID] = tag
101+
if len(mainTagId) > 0 {
102+
mainTagList, err := ts.tagCommonRepo.GetTagListByIDs(ctx, mainTagId)
103+
if err != nil {
104+
return nil, err
105+
}
106+
for _, tag := range mainTagList {
107+
mainTagMap[tag.ID] = tag
108+
}
107109
}
108110
for _, tag := range tags {
109-
if tag.MainTagID != 0 {
110-
_, ok := mainTagMap[converter.IntToString(tag.MainTagID)]
111-
if ok {
112-
tag.SlugName = mainTagMap[converter.IntToString(tag.MainTagID)].SlugName
113-
tag.DisplayName = mainTagMap[converter.IntToString(tag.MainTagID)].DisplayName
114-
tag.Reserved = mainTagMap[converter.IntToString(tag.MainTagID)].Reserved
115-
tag.Recommend = mainTagMap[converter.IntToString(tag.MainTagID)].Recommend
116-
}
111+
if tag.MainTagID == 0 {
112+
continue
113+
}
114+
mainTagID := converter.IntToString(tag.MainTagID)
115+
if _, ok := mainTagMap[mainTagID]; ok {
116+
tag.SlugName = mainTagMap[mainTagID].SlugName
117+
tag.DisplayName = mainTagMap[mainTagID].DisplayName
118+
tag.Reserved = mainTagMap[mainTagID].Reserved
119+
tag.Recommend = mainTagMap[mainTagID].Recommend
117120
}
118121
}
119-
RepetitiveTag := make(map[string]bool)
122+
resp = make([]schema.SearchTagLikeResp, 0)
123+
repetitiveTag := make(map[string]bool)
120124
for _, tag := range tags {
121-
if _, ok := RepetitiveTag[tag.SlugName]; !ok {
125+
if _, ok := repetitiveTag[tag.SlugName]; !ok {
122126
item := schema.SearchTagLikeResp{}
123127
item.SlugName = tag.SlugName
124128
item.DisplayName = tag.DisplayName
125129
item.Recommend = tag.Recommend
126130
item.Reserved = tag.Reserved
127131
resp = append(resp, item)
128-
RepetitiveTag[tag.SlugName] = true
132+
repetitiveTag[tag.SlugName] = true
129133
}
130134
}
131135
return resp, nil

0 commit comments

Comments
 (0)