2022-09-27 17:59:05 +08:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
2022-10-24 16:51:05 +08:00
|
|
|
"github.com/answerdev/answer/internal/base/handler"
|
|
|
|
"github.com/answerdev/answer/internal/base/middleware"
|
|
|
|
"github.com/answerdev/answer/internal/schema"
|
|
|
|
"github.com/answerdev/answer/internal/service"
|
2022-09-27 17:59:05 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SearchController tag controller
|
|
|
|
type SearchController struct {
|
|
|
|
searchService *service.SearchService
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSearchController new controller
|
|
|
|
func NewSearchController(searchService *service.SearchService) *SearchController {
|
|
|
|
return &SearchController{searchService: searchService}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search godoc
|
|
|
|
// @Summary search object
|
|
|
|
// @Description search object
|
|
|
|
// @Tags Search
|
|
|
|
// @Produce json
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Param q query string true "query string"
|
2022-10-17 11:11:23 +08:00
|
|
|
// @Param order query string true "order" Enums(newest,active,score,relevance)
|
2022-09-27 17:59:05 +08:00
|
|
|
// @Success 200 {object} handler.RespBody{data=schema.SearchListResp}
|
|
|
|
// @Router /answer/api/v1/search [get]
|
|
|
|
func (sc *SearchController) Search(ctx *gin.Context) {
|
2022-10-25 11:45:45 +08:00
|
|
|
dto := schema.SearchDTO{}
|
2022-09-27 17:59:05 +08:00
|
|
|
|
2022-10-25 11:45:45 +08:00
|
|
|
if handler.BindAndCheck(ctx, &dto) {
|
|
|
|
return
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
2022-10-25 11:45:45 +08:00
|
|
|
dto.UserID = middleware.GetLoginUserIDFromContext(ctx)
|
2022-09-27 17:59:05 +08:00
|
|
|
|
|
|
|
resp, total, extra, err := sc.searchService.Search(ctx, &dto)
|
|
|
|
|
|
|
|
handler.HandleResponse(ctx, err, schema.SearchListResp{
|
|
|
|
Total: total,
|
|
|
|
SearchResp: resp,
|
|
|
|
Extra: extra,
|
|
|
|
})
|
|
|
|
}
|