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"
|
2023-12-27 22:17:08 +08:00
|
|
|
"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"`
|
2023-12-27 22:17:08 +08:00
|
|
|
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
|
2023-12-22 15:19:13 +08:00
|
|
|
// @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"
|
2023-12-27 22:17:08 +08:00
|
|
|
// @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
|
|
|
|
}
|
|
|
|
|
2023-12-27 22:17:08 +08:00
|
|
|
var count int64
|
|
|
|
param := problem.QueryData{
|
2023-12-31 16:09:02 +08:00
|
|
|
Keyword: req.Keyword,
|
|
|
|
Tag: req.Tag,
|
2023-12-31 17:01:20 +08:00
|
|
|
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
|
|
|
}
|
2023-12-27 22:17:08 +08:00
|
|
|
|
|
|
|
problems, status := h.problemService.QueryFuzz(¶m)
|
|
|
|
e.Pong(c, status, e.WithCount[*model.Problem]{Count: count, Data: problems})
|
2022-09-26 16:13:31 +08:00
|
|
|
}
|