refactor(tag): refactor search tags interface

This commit is contained in:
LinkinStars 2023-08-25 17:43:08 +08:00
parent f7602ddd5e
commit c68eff2011
3 changed files with 44 additions and 31 deletions

View File

@ -45,7 +45,6 @@ func (tc *TagController) SearchTagLike(ctx *gin.Context) {
if handler.BindAndCheck(ctx, req) {
return
}
req.IsAdmin = middleware.GetIsAdminFromContext(ctx)
resp, err := tc.tagCommonService.SearchTagLike(ctx, req)
handler.HandleResponse(ctx, err, resp)
}

View File

@ -3,6 +3,7 @@ package tag_common
import (
"context"
"fmt"
"strings"
"github.com/answerdev/answer/internal/base/data"
"github.com/answerdev/answer/internal/base/pager"
@ -56,19 +57,28 @@ func (tr *tagCommonRepo) GetTagBySlugName(ctx context.Context, slugName string)
}
// GetTagListByName get tag list all like name
func (tr *tagCommonRepo) GetTagListByName(ctx context.Context, name string, hasReserved bool) (tagList []*entity.Tag, err error) {
tagList = make([]*entity.Tag, 0)
func (tr *tagCommonRepo) GetTagListByName(ctx context.Context, name string, recommend, reserved bool) (tagList []*entity.Tag, err error) {
cond := &entity.Tag{}
session := tr.data.DB.Context(ctx).Where("")
if name != "" {
session.Where("slug_name LIKE LOWER(?) or display_name LIKE ?", name+"%", name+"%")
} else {
session.UseBool("recommend")
session := tr.data.DB.Context(ctx)
if len(name) > 0 {
session.Where("slug_name LIKE ? OR display_name LIKE ?", strings.ToLower(name)+"%", name+"%")
}
var columns []string
if recommend {
columns = append(columns, "recommend")
cond.Recommend = true
}
if reserved {
columns = append(columns, "reserved")
cond.Reserved = true
}
if len(columns) > 0 {
session.UseBool(columns...)
}
session.Where(builder.Eq{"status": entity.TagStatusAvailable})
session.Asc("slug_name")
err = session.OrderBy("recommend desc,reserved desc,id desc").Find(&tagList, cond)
tagList = make([]*entity.Tag, 0)
err = session.OrderBy("recommend DESC,reserved DESC,slug_name ASC").Find(&tagList, cond)
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}

View File

@ -24,7 +24,7 @@ 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, hasReserved bool) (tagList []*entity.Tag, err error)
GetTagListByName(ctx context.Context, name string, recommend, reserved bool) (tagList []*entity.Tag, err error)
GetTagListByNames(ctx context.Context, names []string) (tagList []*entity.Tag, err error)
GetTagByID(ctx context.Context, tagID string, includeDeleted bool) (tag *entity.Tag, exist bool, err error)
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(
// 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, req.IsAdmin)
tags, err := ts.tagCommonRepo.GetTagListByName(ctx, req.Tag, len(req.Tag) == 0, false)
if err != nil {
return
}
@ -97,35 +97,39 @@ func (ts *TagCommonService) SearchTagLike(ctx context.Context, req *schema.Searc
mainTagId = append(mainTagId, converter.IntToString(tag.MainTagID))
}
}
mainTagList, err := ts.tagCommonRepo.GetTagListByIDs(ctx, mainTagId)
if err != nil {
return
}
mainTagMap := make(map[string]*entity.Tag)
for _, tag := range mainTagList {
mainTagMap[tag.ID] = tag
}
for _, tag := range tags {
if tag.MainTagID != 0 {
_, ok := mainTagMap[converter.IntToString(tag.MainTagID)]
if ok {
tag.SlugName = mainTagMap[converter.IntToString(tag.MainTagID)].SlugName
tag.DisplayName = mainTagMap[converter.IntToString(tag.MainTagID)].DisplayName
tag.Reserved = mainTagMap[converter.IntToString(tag.MainTagID)].Reserved
tag.Recommend = mainTagMap[converter.IntToString(tag.MainTagID)].Recommend
}
if len(mainTagId) > 0 {
mainTagList, err := ts.tagCommonRepo.GetTagListByIDs(ctx, mainTagId)
if err != nil {
return nil, err
}
for _, tag := range mainTagList {
mainTagMap[tag.ID] = tag
}
}
RepetitiveTag := make(map[string]bool)
for _, tag := range tags {
if _, ok := RepetitiveTag[tag.SlugName]; !ok {
if tag.MainTagID == 0 {
continue
}
mainTagID := converter.IntToString(tag.MainTagID)
if _, ok := mainTagMap[mainTagID]; ok {
tag.SlugName = mainTagMap[mainTagID].SlugName
tag.DisplayName = mainTagMap[mainTagID].DisplayName
tag.Reserved = mainTagMap[mainTagID].Reserved
tag.Recommend = mainTagMap[mainTagID].Recommend
}
}
resp = make([]schema.SearchTagLikeResp, 0)
repetitiveTag := make(map[string]bool)
for _, tag := range tags {
if _, ok := repetitiveTag[tag.SlugName]; !ok {
item := schema.SearchTagLikeResp{}
item.SlugName = tag.SlugName
item.DisplayName = tag.DisplayName
item.Recommend = tag.Recommend
item.Reserved = tag.Reserved
resp = append(resp, item)
RepetitiveTag[tag.SlugName] = true
repetitiveTag[tag.SlugName] = true
}
}
return resp, nil