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

74 lines
2.1 KiB
Go
Raw Normal View History

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"
"github.com/gin-gonic/gin"
)
type createVersionRequest struct {
ProblemID uint `form:"pid" json:"pid" binding:"required"`
StorageKey string `form:"storage_key" json:"storage_key" binding:"required"`
}
// CreateVersion
// @Summary [admin] create a problem version
// @Description Create a problem version associated with `pid`.
// @Tags problem,admin
// @Accept application/x-www-form-urlencoded
// @Produce json
// @Param pid formData int true "problem id"
// @Param storage_key formData string true "storage key, zip file containing problem data"
// @Response 200 {object} e.Response[any] "nothing"
// @Security Authentication
// @Router /v1/problem/create_version [post]
func (h *handler) CreateVersion(c *gin.Context) {
claim, exist := c.Get("claim")
if !exist {
e.Pong[any](c, e.UserUnauthenticated, nil)
return
}
req := new(createVersionRequest)
if err := c.ShouldBind(req); err != nil {
e.Pong(c, e.InvalidParameter, err.Error())
return
}
// only admin can create problem version
role := claim.(*model.Claim).Role
if role < model.RoleAdmin {
e.Pong[any](c, e.UserUnauthorized, nil)
return
}
// make sure problem exists
_, status := h.problemService.Query(&problem.QueryData{ID: req.ProblemID, Associations: false, ShouldEnable: false})
if status != e.Success {
e.Pong[any](c, status, nil)
return
}
// create problem version
createVersionData := &problem.CreateVersionData{
ProblemID: req.ProblemID,
StorageKey: req.StorageKey,
}
pv, status := h.problemService.CreateVersion(createVersionData)
if status != e.Success {
e.Pong[any](c, status, nil)
return
}
// enqueue task: runner build problem
payload := &model.ProblemBuildPayload{
ProblemVersionID: pv.ID,
StorageKey: pv.StorageKey,
}
_, status = h.taskService.ProblemBuild(payload)
e.Pong[any](c, status, nil)
// TODO: if failed, delete problem version
}