add tags search

This commit is contained in:
aichy126 2023-02-15 12:21:59 +08:00
parent 83763a1095
commit a2b036cc16
7 changed files with 305 additions and 1 deletions

View File

@ -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": {

View File

@ -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": {

View File

@ -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

View File

@ -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)
}

View File

@ -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

View File

@ -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

View File

@ -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) {