mirror of https://gitee.com/answerdev/answer.git
style: search form validate
This commit is contained in:
parent
1739424417
commit
866b8a062e
|
@ -3,12 +3,10 @@ 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"
|
||||
"github.com/segmentfault/pacman/log"
|
||||
)
|
||||
|
||||
// SearchController tag controller
|
||||
|
@ -32,39 +30,13 @@ 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)
|
||||
log.Error(dto)
|
||||
|
||||
resp, total, extra, err := sc.searchService.Search(ctx, &dto)
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue