From a2b036cc16494323fe5d23f3729a0fedd767cccb Mon Sep 17 00:00:00 2001 From: aichy126 <16996097+aichy126@users.noreply.github.com> Date: Wed, 15 Feb 2023 12:21:59 +0800 Subject: [PATCH] add tags search --- docs/docs.go | 100 ++++++++++++++++++++++++++ docs/swagger.json | 100 ++++++++++++++++++++++++++ docs/swagger.yaml | 63 ++++++++++++++++ internal/controller/tag_controller.go | 22 +++++- internal/router/answer_api_router.go | 1 + internal/schema/tag_schema.go | 6 ++ internal/service/tag/tag_service.go | 14 ++++ 7 files changed, 305 insertions(+), 1 deletion(-) diff --git a/docs/docs.go b/docs/docs.go index 3adfd474..41460363 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -4040,6 +4040,53 @@ const docTemplate = `{ } } }, + "/answer/api/v1/tags": { + "get": { + "description": "get tags list", + "produces": [ + "application/json" + ], + "tags": [ + "Tag" + ], + "summary": "get tags list", + "parameters": [ + { + "type": "array", + "items": { + "type": "string" + }, + "collectionFormat": "csv", + "description": "string collection", + "name": "tags", + "in": "query" + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/handler.RespBody" + }, + { + "type": "object", + "properties": { + "data": { + "type": "array", + "items": { + "$ref": "#/definitions/entity.Tag" + } + } + } + } + ] + } + } + } + } + }, "/answer/api/v1/tags/following": { "get": { "security": [ @@ -5262,6 +5309,59 @@ const docTemplate = `{ } }, "definitions": { + "entity.Tag": { + "type": "object", + "properties": { + "createdAt": { + "type": "string" + }, + "displayName": { + "type": "string" + }, + "followCount": { + "type": "integer" + }, + "id": { + "type": "string" + }, + "mainTagID": { + "type": "integer" + }, + "mainTagSlugName": { + "type": "string" + }, + "originalText": { + "type": "string" + }, + "parsedText": { + "type": "string" + }, + "questionCount": { + "type": "integer" + }, + "recommend": { + "type": "boolean" + }, + "reserved": { + "type": "boolean" + }, + "revisionID": { + "type": "string" + }, + "slugName": { + "type": "string" + }, + "status": { + "type": "integer" + }, + "updatedAt": { + "type": "string" + }, + "userID": { + "type": "string" + } + } + }, "handler.RespBody": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index 0282b612..abcceeae 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -4028,6 +4028,53 @@ } } }, + "/answer/api/v1/tags": { + "get": { + "description": "get tags list", + "produces": [ + "application/json" + ], + "tags": [ + "Tag" + ], + "summary": "get tags list", + "parameters": [ + { + "type": "array", + "items": { + "type": "string" + }, + "collectionFormat": "csv", + "description": "string collection", + "name": "tags", + "in": "query" + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/handler.RespBody" + }, + { + "type": "object", + "properties": { + "data": { + "type": "array", + "items": { + "$ref": "#/definitions/entity.Tag" + } + } + } + } + ] + } + } + } + } + }, "/answer/api/v1/tags/following": { "get": { "security": [ @@ -5250,6 +5297,59 @@ } }, "definitions": { + "entity.Tag": { + "type": "object", + "properties": { + "createdAt": { + "type": "string" + }, + "displayName": { + "type": "string" + }, + "followCount": { + "type": "integer" + }, + "id": { + "type": "string" + }, + "mainTagID": { + "type": "integer" + }, + "mainTagSlugName": { + "type": "string" + }, + "originalText": { + "type": "string" + }, + "parsedText": { + "type": "string" + }, + "questionCount": { + "type": "integer" + }, + "recommend": { + "type": "boolean" + }, + "reserved": { + "type": "boolean" + }, + "revisionID": { + "type": "string" + }, + "slugName": { + "type": "string" + }, + "status": { + "type": "integer" + }, + "updatedAt": { + "type": "string" + }, + "userID": { + "type": "string" + } + } + }, "handler.RespBody": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 3fac2ae0..cc8260ed 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -1,4 +1,39 @@ definitions: + entity.Tag: + properties: + createdAt: + type: string + displayName: + type: string + followCount: + type: integer + id: + type: string + mainTagID: + type: integer + mainTagSlugName: + type: string + originalText: + type: string + parsedText: + type: string + questionCount: + type: integer + recommend: + type: boolean + reserved: + type: boolean + revisionID: + type: string + slugName: + type: string + status: + type: integer + updatedAt: + type: string + userID: + type: string + type: object handler.RespBody: properties: code: @@ -4351,6 +4386,34 @@ paths: summary: get tag synonyms tags: - Tag + /answer/api/v1/tags: + get: + description: get tags list + parameters: + - collectionFormat: csv + description: string collection + in: query + items: + type: string + name: tags + type: array + produces: + - application/json + responses: + "200": + description: OK + schema: + allOf: + - $ref: '#/definitions/handler.RespBody' + - properties: + data: + items: + $ref: '#/definitions/entity.Tag' + type: array + type: object + summary: get tags list + tags: + - Tag /answer/api/v1/tags/following: get: description: get following tag list diff --git a/internal/controller/tag_controller.go b/internal/controller/tag_controller.go index 35b3f69f..03a3669b 100644 --- a/internal/controller/tag_controller.go +++ b/internal/controller/tag_controller.go @@ -1,6 +1,8 @@ package controller import ( + "strings" + "github.com/answerdev/answer/internal/base/handler" "github.com/answerdev/answer/internal/base/middleware" "github.com/answerdev/answer/internal/base/reason" @@ -48,6 +50,25 @@ func (tc *TagController) SearchTagLike(ctx *gin.Context) { handler.HandleResponse(ctx, err, resp) } +// GetTagsBySlugName +// @Summary get tags list +// @Description get tags list +// @Tags Tag +// @Produce json +// @Param tags query []string false "string collection" collectionFormat(csv) +// @Success 200 {object} handler.RespBody{data=[]*schema.TagItem} +// @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) + handler.HandleResponse(ctx, err, resp) +} + // RemoveTag delete tag // @Summary delete tag // @Description delete tag @@ -73,7 +94,6 @@ func (tc *TagController) RemoveTag(ctx *gin.Context) { handler.HandleResponse(ctx, errors.Forbidden(reason.RankFailToMeetTheCondition), nil) return } - err = tc.tagService.RemoveTag(ctx, req) handler.HandleResponse(ctx, err, nil) } diff --git a/internal/router/answer_api_router.go b/internal/router/answer_api_router.go index a8933446..b2cb22ce 100644 --- a/internal/router/answer_api_router.go +++ b/internal/router/answer_api_router.go @@ -141,6 +141,7 @@ func (a *AnswerAPIRouter) RegisterUnAuthAnswerAPIRouter(r *gin.RouterGroup) { r.GET("/tags/page", a.tagController.GetTagWithPage) r.GET("/tags/following", a.tagController.GetFollowingTags) r.GET("/tag", a.tagController.GetTagInfo) + r.GET("/tags", a.tagController.GetTagsBySlugName) r.GET("/tag/synonyms", a.tagController.GetTagSynonyms) //search diff --git a/internal/schema/tag_schema.go b/internal/schema/tag_schema.go index 654e9178..cc046b37 100644 --- a/internal/schema/tag_schema.go +++ b/internal/schema/tag_schema.go @@ -16,6 +16,12 @@ type SearchTagLikeReq struct { IsAdmin bool `json:"-"` } +type SearchTagsBySlugName struct { + Tags string `json:"tags" form:"tags"` + TagList []string `json:"-"` + IsAdmin bool `json:"-"` +} + // GetTagInfoReq get tag info request type GetTagInfoReq struct { // tag id diff --git a/internal/service/tag/tag_service.go b/internal/service/tag/tag_service.go index 2497b2c7..1fb118ec 100644 --- a/internal/service/tag/tag_service.go +++ b/internal/service/tag/tag_service.go @@ -135,6 +135,20 @@ 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) + if err != nil { + return tagList, err + } + for _, tag := range tagListInDB { + tagItem := &schema.TagItem{} + copier.Copy(tagItem, tag) + tagList = append(tagList, tagItem) + } + return tagList, nil +} + // GetFollowingTags get following tags func (ts *TagService) GetFollowingTags(ctx context.Context, userID string) ( resp []*schema.GetFollowingTagsResp, err error) {