mirror of https://gitee.com/answerdev/answer.git
refactor(tag): refactor search tags interface
This commit is contained in:
parent
f7602ddd5e
commit
c68eff2011
|
@ -45,7 +45,6 @@ func (tc *TagController) SearchTagLike(ctx *gin.Context) {
|
||||||
if handler.BindAndCheck(ctx, req) {
|
if handler.BindAndCheck(ctx, req) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
req.IsAdmin = middleware.GetIsAdminFromContext(ctx)
|
|
||||||
resp, err := tc.tagCommonService.SearchTagLike(ctx, req)
|
resp, err := tc.tagCommonService.SearchTagLike(ctx, req)
|
||||||
handler.HandleResponse(ctx, err, resp)
|
handler.HandleResponse(ctx, err, resp)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package tag_common
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/answerdev/answer/internal/base/data"
|
"github.com/answerdev/answer/internal/base/data"
|
||||||
"github.com/answerdev/answer/internal/base/pager"
|
"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
|
// GetTagListByName get tag list all like name
|
||||||
func (tr *tagCommonRepo) GetTagListByName(ctx context.Context, name string, hasReserved bool) (tagList []*entity.Tag, err error) {
|
func (tr *tagCommonRepo) GetTagListByName(ctx context.Context, name string, recommend, reserved bool) (tagList []*entity.Tag, err error) {
|
||||||
tagList = make([]*entity.Tag, 0)
|
|
||||||
cond := &entity.Tag{}
|
cond := &entity.Tag{}
|
||||||
session := tr.data.DB.Context(ctx).Where("")
|
session := tr.data.DB.Context(ctx)
|
||||||
if name != "" {
|
if len(name) > 0 {
|
||||||
session.Where("slug_name LIKE LOWER(?) or display_name LIKE ?", name+"%", name+"%")
|
session.Where("slug_name LIKE ? OR display_name LIKE ?", strings.ToLower(name)+"%", name+"%")
|
||||||
} else {
|
}
|
||||||
session.UseBool("recommend")
|
var columns []string
|
||||||
|
if recommend {
|
||||||
|
columns = append(columns, "recommend")
|
||||||
cond.Recommend = true
|
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.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 {
|
if err != nil {
|
||||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ type TagCommonRepo interface {
|
||||||
AddTagList(ctx context.Context, tagList []*entity.Tag) (err error)
|
AddTagList(ctx context.Context, tagList []*entity.Tag) (err error)
|
||||||
GetTagListByIDs(ctx context.Context, ids []string) (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)
|
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)
|
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)
|
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)
|
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
|
// SearchTagLike get tag list all
|
||||||
func (ts *TagCommonService) SearchTagLike(ctx context.Context, req *schema.SearchTagLikeReq) (resp []schema.SearchTagLikeResp, err error) {
|
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 {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -97,35 +97,39 @@ func (ts *TagCommonService) SearchTagLike(ctx context.Context, req *schema.Searc
|
||||||
mainTagId = append(mainTagId, converter.IntToString(tag.MainTagID))
|
mainTagId = append(mainTagId, converter.IntToString(tag.MainTagID))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
mainTagMap := make(map[string]*entity.Tag)
|
||||||
|
if len(mainTagId) > 0 {
|
||||||
mainTagList, err := ts.tagCommonRepo.GetTagListByIDs(ctx, mainTagId)
|
mainTagList, err := ts.tagCommonRepo.GetTagListByIDs(ctx, mainTagId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return nil, err
|
||||||
}
|
}
|
||||||
mainTagMap := make(map[string]*entity.Tag)
|
|
||||||
for _, tag := range mainTagList {
|
for _, tag := range mainTagList {
|
||||||
mainTagMap[tag.ID] = tag
|
mainTagMap[tag.ID] = tag
|
||||||
}
|
}
|
||||||
|
}
|
||||||
for _, tag := range tags {
|
for _, tag := range tags {
|
||||||
if tag.MainTagID != 0 {
|
if tag.MainTagID == 0 {
|
||||||
_, ok := mainTagMap[converter.IntToString(tag.MainTagID)]
|
continue
|
||||||
if ok {
|
}
|
||||||
tag.SlugName = mainTagMap[converter.IntToString(tag.MainTagID)].SlugName
|
mainTagID := converter.IntToString(tag.MainTagID)
|
||||||
tag.DisplayName = mainTagMap[converter.IntToString(tag.MainTagID)].DisplayName
|
if _, ok := mainTagMap[mainTagID]; ok {
|
||||||
tag.Reserved = mainTagMap[converter.IntToString(tag.MainTagID)].Reserved
|
tag.SlugName = mainTagMap[mainTagID].SlugName
|
||||||
tag.Recommend = mainTagMap[converter.IntToString(tag.MainTagID)].Recommend
|
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)
|
repetitiveTag := make(map[string]bool)
|
||||||
for _, tag := range tags {
|
for _, tag := range tags {
|
||||||
if _, ok := RepetitiveTag[tag.SlugName]; !ok {
|
if _, ok := repetitiveTag[tag.SlugName]; !ok {
|
||||||
item := schema.SearchTagLikeResp{}
|
item := schema.SearchTagLikeResp{}
|
||||||
item.SlugName = tag.SlugName
|
item.SlugName = tag.SlugName
|
||||||
item.DisplayName = tag.DisplayName
|
item.DisplayName = tag.DisplayName
|
||||||
item.Recommend = tag.Recommend
|
item.Recommend = tag.Recommend
|
||||||
item.Reserved = tag.Reserved
|
item.Reserved = tag.Reserved
|
||||||
resp = append(resp, item)
|
resp = append(resp, item)
|
||||||
RepetitiveTag[tag.SlugName] = true
|
repetitiveTag[tag.SlugName] = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return resp, nil
|
return resp, nil
|
||||||
|
|
Loading…
Reference in New Issue