Merge branch 'fix/search' into 'main'

Fix/search

See merge request opensource/answer!108
This commit is contained in:
杨光富 2022-10-25 03:54:01 +00:00
commit 8bd2e38d9a
2 changed files with 9 additions and 41 deletions

View File

@ -3,12 +3,9 @@ package controller
import (
"github.com/answerdev/answer/internal/base/handler"
"github.com/answerdev/answer/internal/base/middleware"
"github.com/answerdev/answer/internal/base/reason"
"github.com/answerdev/answer/internal/schema"
"github.com/answerdev/answer/internal/service"
"github.com/answerdev/answer/pkg/converter"
"github.com/gin-gonic/gin"
"github.com/segmentfault/pacman/errors"
)
// SearchController tag controller
@ -32,39 +29,12 @@ func NewSearchController(searchService *service.SearchService) *SearchController
// @Success 200 {object} handler.RespBody{data=schema.SearchListResp}
// @Router /answer/api/v1/search [get]
func (sc *SearchController) Search(ctx *gin.Context) {
var (
q,
order,
page,
size string
ok bool
dto schema.SearchDTO
)
q, ok = ctx.GetQuery("q")
if len(q) == 0 || !ok {
handler.HandleResponse(ctx, errors.BadRequest(reason.RequestFormatError), q)
dto := schema.SearchDTO{}
if handler.BindAndCheck(ctx, &dto) {
return
}
page, ok = ctx.GetQuery("page")
if !ok {
page = "1"
}
size, ok = ctx.GetQuery("size")
if !ok {
size = "30"
}
order, ok = ctx.GetQuery("order")
if !ok || (order != "newest" && order != "active" && order != "score" && order != "relevance") {
order = "newest"
}
dto = schema.SearchDTO{
Query: q,
Page: converter.StringToInt(page),
Size: converter.StringToInt(size),
UserID: middleware.GetLoginUserIDFromContext(ctx),
Order: order,
}
dto.UserID = middleware.GetLoginUserIDFromContext(ctx)
resp, total, extra, err := sc.searchService.Search(ctx, &dto)

View File

@ -1,13 +1,11 @@
package schema
type SearchDTO struct {
// Query the query string
Query string
// UserID current login user ID
UserID string
Page int
Size int
Order string
UserID string // UserID current login user ID
Query string `validate:"required,gte=1,lte=60" json:"q" form:"q"` // Query the query string
Page int `validate:"omitempty,min=1" form:"page,default=1" json:"page"` //Query number of pages
Size int `validate:"omitempty,min=1,max=50" form:"size,default=30" json:"size"` //Search page size
Order string `validate:"required,oneof=newest active score relevance" form:"order,default=relevance" json:"order" enums:"newest,active,score,relevance"`
}
type SearchObject struct {