woj-server/internal/api/problem/search.go

50 lines
1.5 KiB
Go
Raw Normal View History

2022-09-26 16:13:31 +08:00
package problem
import (
2023-07-14 21:47:11 +08:00
"git.0x7f.app/WOJ/woj-server/internal/e"
"git.0x7f.app/WOJ/woj-server/internal/model"
"git.0x7f.app/WOJ/woj-server/internal/service/problem"
2022-09-26 16:13:31 +08:00
"github.com/gin-gonic/gin"
)
type searchRequest struct {
2023-12-27 21:30:52 +08:00
Keyword string `form:"keyword" json:"keyword"`
Tag string `form:"tag" json:"tag"`
Offset int `form:"offset" json:"offset"`
Limit int `form:"limit" json:"limit" binding:"required"`
2022-09-26 16:13:31 +08:00
}
// Search
// @Summary search for problems
// @Description Search for problems based on keywords. If the keyword is empty, return all problems.
2023-12-18 21:21:15 +08:00
// @Tags problem
2022-09-26 16:13:31 +08:00
// @Accept application/x-www-form-urlencoded
// @Produce json
2023-12-27 21:30:52 +08:00
// @Param keyword formData string false "keyword"
// @Param tag formData string false "tag"
// @Param offset formData int false "start position"
// @Param limit formData int true "limit number of records"
// @Response 200 {object} e.Response[e.WithCount[model.Problem]] "problems found"
2022-09-26 16:13:31 +08:00
// @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
}
var count int64
param := problem.QueryData{
2023-12-31 16:09:02 +08:00
Keyword: req.Keyword,
Tag: req.Tag,
Associations: true,
2023-12-31 16:09:02 +08:00
ShouldEnable: true,
Offset: req.Offset,
Limit: req.Limit,
Count: &count,
2022-09-26 16:13:31 +08:00
}
problems, status := h.problemService.QueryFuzz(&param)
e.Pong(c, status, e.WithCount[*model.Problem]{Count: count, Data: problems})
2022-09-26 16:13:31 +08:00
}