2022-10-22 17:38:39 +08:00
|
|
|
package submission
|
|
|
|
|
|
|
|
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"
|
2024-04-28 23:19:00 +08:00
|
|
|
"git.0x7f.app/WOJ/woj-server/internal/service/problem"
|
2023-07-14 21:47:11 +08:00
|
|
|
"git.0x7f.app/WOJ/woj-server/internal/service/submission"
|
2022-10-22 17:38:39 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
type createRequest struct {
|
2023-12-23 15:35:09 +08:00
|
|
|
Pid uint `form:"pid" json:"pid" binding:"required"`
|
|
|
|
Language string `form:"language" json:"language" binding:"required"`
|
|
|
|
Code string `form:"code" json:"code" binding:"required"`
|
2022-10-22 17:38:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create
|
2023-12-22 15:19:13 +08:00
|
|
|
// @Summary submit for judgement
|
|
|
|
// @Description Submit the code for judgement.
|
2023-12-18 21:21:15 +08:00
|
|
|
// @Tags submission
|
2022-10-22 17:38:39 +08:00
|
|
|
// @Accept application/x-www-form-urlencoded
|
|
|
|
// @Produce json
|
|
|
|
// @Param pid formData int true "problem id"
|
|
|
|
// @Param language formData string true "language"
|
|
|
|
// @Param code formData string true "code"
|
2023-12-22 15:19:13 +08:00
|
|
|
// @Response 200 {object} e.Response[uint] "submission id"
|
2022-10-22 17:38:39 +08:00
|
|
|
// @Security Authentication
|
|
|
|
// @Router /v1/submission/create [post]
|
|
|
|
func (h *handler) Create(c *gin.Context) {
|
|
|
|
claim, exist := c.Get("claim")
|
|
|
|
if !exist {
|
2023-12-22 15:19:13 +08:00
|
|
|
e.Pong[any](c, e.UserUnauthenticated, nil)
|
2022-10-22 17:38:39 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-15 16:19:49 +08:00
|
|
|
uid := claim.(*model.Claim).UID
|
|
|
|
role := claim.(*model.Claim).Role
|
2022-10-22 17:38:39 +08:00
|
|
|
|
2022-10-23 17:29:35 +08:00
|
|
|
// guest can not submit
|
|
|
|
if role < model.RoleGeneral {
|
2023-12-22 15:19:13 +08:00
|
|
|
e.Pong[any](c, e.UserUnauthorized, nil)
|
2022-10-23 17:29:35 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-18 23:43:27 +08:00
|
|
|
req := new(createRequest)
|
|
|
|
if err := c.ShouldBind(req); err != nil {
|
|
|
|
e.Pong(c, e.InvalidParameter, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-28 23:19:00 +08:00
|
|
|
// check problem enabled
|
|
|
|
_, status := h.problemService.Query(&problem.QueryData{ID: req.Pid, Associations: false, ShouldEnable: role < model.RoleAdmin})
|
|
|
|
if status != e.Success {
|
|
|
|
e.Pong[any](c, status, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-27 22:28:16 +08:00
|
|
|
// query latest version
|
|
|
|
pv, status := h.problemService.QueryLatestVersion(req.Pid)
|
|
|
|
if status != e.Success {
|
|
|
|
e.Pong[any](c, status, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-22 15:19:13 +08:00
|
|
|
// create submission
|
2022-10-22 17:38:39 +08:00
|
|
|
createData := &submission.CreateData{
|
|
|
|
ProblemID: req.Pid,
|
|
|
|
UserID: uid,
|
|
|
|
Language: req.Language,
|
|
|
|
Code: req.Code,
|
|
|
|
}
|
2023-12-22 15:19:13 +08:00
|
|
|
res, status := h.submissionService.Create(createData)
|
2022-10-22 17:38:39 +08:00
|
|
|
if status != e.Success {
|
2023-12-22 15:19:13 +08:00
|
|
|
e.Pong[any](c, status, nil)
|
2022-10-22 17:38:39 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-22 15:19:13 +08:00
|
|
|
// submit judge
|
2022-10-23 17:29:35 +08:00
|
|
|
payload := &model.SubmitJudgePayload{
|
|
|
|
ProblemVersionID: pv.ID,
|
|
|
|
StorageKey: pv.StorageKey,
|
2023-12-22 15:19:13 +08:00
|
|
|
Submission: *res,
|
2022-10-23 17:29:35 +08:00
|
|
|
}
|
|
|
|
_, status = h.taskService.SubmitJudge(payload)
|
|
|
|
|
2023-12-22 15:19:13 +08:00
|
|
|
e.Pong[any](c, status, res.ID)
|
2022-10-22 17:38:39 +08:00
|
|
|
}
|