This is an automated email from the ASF dual-hosted git repository.

linkinstar pushed a commit to branch feat/1.3.6/template
in repository https://gitbox.apache.org/repos/asf/incubator-answer.git

commit b7ebe3083b717f0556ab5f27c79502b5380be1c3
Author: LinkinStars <[email protected]>
AuthorDate: Fri Jul 12 10:59:28 2024 +0800

    feat(tag): update tags search API
---
 internal/controller/tag_controller.go     | 15 ++++++---------
 internal/schema/tag_schema.go             |  9 +++++----
 internal/service/tag/tag_service.go       | 25 ++++++++++++++++---------
 internal/service/tag_common/tag_common.go |  6 +++---
 4 files changed, 30 insertions(+), 25 deletions(-)

diff --git a/internal/controller/tag_controller.go 
b/internal/controller/tag_controller.go
index 11b1fcde..5f5cf5fb 100644
--- a/internal/controller/tag_controller.go
+++ b/internal/controller/tag_controller.go
@@ -20,8 +20,6 @@
 package controller
 
 import (
-       "strings"
-
        "github.com/apache/incubator-answer/internal/base/handler"
        "github.com/apache/incubator-answer/internal/base/middleware"
        "github.com/apache/incubator-answer/internal/base/reason"
@@ -57,7 +55,7 @@ func NewTagController(
 // @Produce json
 // @Security ApiKeyAuth
 // @Param tag query string false "tag"
-// @Success 200 {object} handler.RespBody{data=[]schema.GetTagResp}
+// @Success 200 {object} handler.RespBody{data=[]schema.GetTagBasicResp}
 // @Router /answer/api/v1/question/tags [get]
 func (tc *TagController) SearchTagLike(ctx *gin.Context) {
        req := &schema.SearchTagLikeReq{}
@@ -68,22 +66,21 @@ func (tc *TagController) SearchTagLike(ctx *gin.Context) {
        handler.HandleResponse(ctx, err, resp)
 }
 
-// GetTagsBySlugName
+// GetTagsBySlugName get tags list
 // @Summary get tags list
-// @Description get tags list
+// @Description get tags list by slug name
 // @Tags Tag
 // @Produce json
 // @Param tags query []string false "string collection" collectionFormat(csv)
-// @Success 200 {object} handler.RespBody{}
+// @Success 200 {object} handler.RespBody{data=[]schema.GetTagBasicResp}
 // @Router /answer/api/v1/tags [get]
 func (tc *TagController) GetTagsBySlugName(ctx *gin.Context) {
        req := &schema.SearchTagsBySlugName{}
        if handler.BindAndCheck(ctx, req) {
                return
        }
-       req.TagList = strings.Split(req.Tags, ",")
-       // req.IsAdmin = middleware.GetIsAdminFromContext(ctx)
-       resp, err := tc.tagService.GetTagsBySlugName(ctx, req.TagList)
+
+       resp, err := tc.tagService.GetTagsBySlugName(ctx, req)
        handler.HandleResponse(ctx, err, resp)
 }
 
diff --git a/internal/schema/tag_schema.go b/internal/schema/tag_schema.go
index d283ba73..888c224b 100644
--- a/internal/schema/tag_schema.go
+++ b/internal/schema/tag_schema.go
@@ -33,10 +33,10 @@ type SearchTagLikeReq struct {
        IsAdmin bool   `json:"-"`
 }
 
+// SearchTagsBySlugName search tags by slug name
 type SearchTagsBySlugName struct {
-       Tags    string   `json:"tags" form:"tags"`
-       TagList []string `json:"-"`
-       IsAdmin bool     `json:"-"`
+       // slug name list split by ','
+       Tags string `form:"tags"`
 }
 
 // GetTagInfoReq get tag info request
@@ -298,7 +298,8 @@ type GetFollowingTagsResp struct {
        Reserved        bool   `json:"reserved"`
 }
 
-type SearchTagLikeResp struct {
+// GetTagBasicResp get tag basic response
+type GetTagBasicResp struct {
        SlugName    string `json:"slug_name"`
        DisplayName string `json:"display_name"`
        Recommend   bool   `json:"recommend"`
diff --git a/internal/service/tag/tag_service.go 
b/internal/service/tag/tag_service.go
index 64d17183..01864163 100644
--- a/internal/service/tag/tag_service.go
+++ b/internal/service/tag/tag_service.go
@@ -22,6 +22,7 @@ package tag
 import (
        "context"
        "encoding/json"
+       "strings"
 
        "github.com/apache/incubator-answer/internal/base/constant"
        "github.com/apache/incubator-answer/internal/service/activity_queue"
@@ -190,18 +191,24 @@ func (ts *TagService) GetTagInfo(ctx context.Context, req 
*schema.GetTagInfoReq)
        return resp, nil
 }
 
-func (ts *TagService) GetTagsBySlugName(ctx context.Context, tagNames 
[]string) ([]*schema.TagItem, error) {
-       tagList := make([]*schema.TagItem, 0)
-       tagListInDB, err := ts.tagCommonService.GetTagListByNames(ctx, tagNames)
+// GetTagsBySlugName get tags by slug name
+func (ts *TagService) GetTagsBySlugName(ctx context.Context, req 
*schema.SearchTagsBySlugName) (
+       resp []*schema.GetTagBasicResp, err error) {
+       resp = make([]*schema.GetTagBasicResp, 0)
+       tagSlugNames := strings.Split(req.Tags, ",")
+       if len(tagSlugNames) == 0 {
+               return resp, nil
+       }
+       tagList, err := ts.tagCommonService.GetTagListByNames(ctx, tagSlugNames)
        if err != nil {
-               return tagList, err
+               return resp, err
        }
-       for _, tag := range tagListInDB {
-               tagItem := &schema.TagItem{}
-               copier.Copy(tagItem, tag)
-               tagList = append(tagList, tagItem)
+       for _, tag := range tagList {
+               tagItem := &schema.GetTagBasicResp{}
+               _ = copier.Copy(tagItem, tag)
+               resp = append(resp, tagItem)
        }
-       return tagList, nil
+       return resp, nil
 }
 
 // GetFollowingTags get following tags
diff --git a/internal/service/tag_common/tag_common.go 
b/internal/service/tag_common/tag_common.go
index e6e7a076..239b3c3f 100644
--- a/internal/service/tag_common/tag_common.go
+++ b/internal/service/tag_common/tag_common.go
@@ -108,7 +108,7 @@ func NewTagCommonService(
 }
 
 // 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.GetTagBasicResp, err error) {
        tags, err := ts.tagCommonRepo.GetTagListByName(ctx, req.Tag, 
len(req.Tag) == 0, false)
        if err != nil {
                return
@@ -142,11 +142,11 @@ func (ts *TagCommonService) SearchTagLike(ctx 
context.Context, req *schema.Searc
                        tag.Recommend = mainTagMap[mainTagID].Recommend
                }
        }
-       resp = make([]schema.SearchTagLikeResp, 0)
+       resp = make([]schema.GetTagBasicResp, 0)
        repetitiveTag := make(map[string]bool)
        for _, tag := range tags {
                if _, ok := repetitiveTag[tag.SlugName]; !ok {
-                       item := schema.SearchTagLikeResp{}
+                       item := schema.GetTagBasicResp{}
                        item.SlugName = tag.SlugName
                        item.DisplayName = tag.DisplayName
                        item.Recommend = tag.Recommend

Reply via email to