woj-server/internal/api/problem/search.go
2023-12-27 21:49:10 +08:00

43 lines
1.2 KiB
Go

package problem
import (
"git.0x7f.app/WOJ/woj-server/internal/e"
_ "git.0x7f.app/WOJ/woj-server/internal/model" // swag requires this
"github.com/gin-gonic/gin"
)
type searchRequest struct {
Keyword string `form:"keyword" json:"keyword"`
Tag string `form:"tag" json:"tag"`
}
// Search
// @Summary search for problems
// @Description Search for problems based on keywords. If the keyword is empty, return all problems.
// @Tags problem
// @Accept application/x-www-form-urlencoded
// @Produce json
// @Param keyword formData string false "keyword"
// @Param tag formData string false "tag"
// @Response 200 {object} e.Response[[]model.Problem] "problems found"
// @Router /v1/problem/search [post]
func (h *handler) Search(c *gin.Context) {
req := new(searchRequest)
if err := c.ShouldBind(req); err != nil {
e.Pong(c, e.InvalidParameter, err.Error())
return
}
// TODO: pagination
if req.Keyword == "" {
// TODO: query without LIKE
problems, status := h.problemService.QueryFuzz(req.Keyword, req.Tag, true, true)
e.Pong(c, status, problems)
return
} else {
problems, status := h.problemService.QueryFuzz(req.Keyword, req.Tag, true, true)
e.Pong(c, status, problems)
return
}
}