feat(tester): add solution endpoints
add CreateSolution&GetSolution&ListSolutions endpoints
This commit is contained in:
parent
af6e0b89f6
commit
ef696d2836
9 changed files with 316 additions and 8 deletions
|
@ -6,6 +6,7 @@ import (
|
|||
"git.sch9.ru/new_gate/ms-tester/pkg"
|
||||
testerv1 "git.sch9.ru/new_gate/ms-tester/proto/tester/v1"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"io"
|
||||
)
|
||||
|
||||
type TesterHandlers struct {
|
||||
|
@ -332,3 +333,112 @@ func (h *TesterHandlers) UpdateParticipant(c *fiber.Ctx, params testerv1.UpdateP
|
|||
|
||||
return c.SendStatus(fiber.StatusOK)
|
||||
}
|
||||
|
||||
func (h *TesterHandlers) ListSolutions(c *fiber.Ctx, params testerv1.ListSolutionsParams) error {
|
||||
list, total, err := h.contestsUC.ListSolutions(c.Context(), models.SolutionsFilter{
|
||||
ContestId: params.ContestId,
|
||||
Page: params.Page,
|
||||
PageSize: params.PageSize,
|
||||
ParticipantId: params.ParticipantId,
|
||||
TaskId: params.TaskId,
|
||||
Language: params.Language,
|
||||
Order: params.Order,
|
||||
State: params.State,
|
||||
})
|
||||
if err != nil {
|
||||
return c.SendStatus(pkg.ToREST(err))
|
||||
}
|
||||
|
||||
resp := testerv1.ListSolutionsResponse{
|
||||
Solutions: make([]testerv1.SolutionListItem, len(list)),
|
||||
Page: params.Page,
|
||||
MaxPage: func() int32 {
|
||||
if total%params.PageSize == 0 {
|
||||
return total / params.PageSize
|
||||
}
|
||||
return total/params.PageSize + 1
|
||||
}(),
|
||||
}
|
||||
|
||||
for i, solution := range list {
|
||||
resp.Solutions[i] = testerv1.SolutionListItem{
|
||||
Id: solution.Id,
|
||||
TaskId: solution.TaskId,
|
||||
ContestId: solution.ContestId,
|
||||
ParticipantId: solution.ParticipantId,
|
||||
Language: solution.Language,
|
||||
Penalty: solution.Penalty,
|
||||
Score: solution.Score,
|
||||
State: solution.State,
|
||||
TotalScore: solution.TotalScore,
|
||||
CreatedAt: solution.CreatedAt,
|
||||
UpdatedAt: solution.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
return c.JSON(resp)
|
||||
}
|
||||
|
||||
const (
|
||||
maxSolutionSize int64 = 10 * 1024 * 1024
|
||||
)
|
||||
|
||||
func (h *TesterHandlers) CreateSolution(c *fiber.Ctx, params testerv1.CreateSolutionParams) error {
|
||||
s, err := c.FormFile("solution")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if s.Size == 0 || s.Size > maxSolutionSize {
|
||||
return c.SendStatus(fiber.StatusBadRequest)
|
||||
}
|
||||
|
||||
f, err := s.Open()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
b, err := io.ReadAll(f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
id, err := h.contestsUC.CreateSolution(c.Context(), &models.SolutionCreation{
|
||||
TaskId: params.TaskId,
|
||||
ParticipantId: 1,
|
||||
Language: params.Language,
|
||||
Penalty: 0,
|
||||
Solution: string(b),
|
||||
})
|
||||
if err != nil {
|
||||
return c.SendStatus(pkg.ToREST(err))
|
||||
}
|
||||
|
||||
return c.JSON(testerv1.CreateSolutionResponse{
|
||||
Id: id,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *TesterHandlers) GetSolution(c *fiber.Ctx, id int32) error {
|
||||
solution, err := h.contestsUC.ReadSolution(c.Context(), id)
|
||||
if err != nil {
|
||||
return c.SendStatus(pkg.ToREST(err))
|
||||
}
|
||||
|
||||
return c.JSON(
|
||||
testerv1.GetSolutionResponse{Solution: testerv1.Solution{
|
||||
Id: solution.Id,
|
||||
TaskId: solution.TaskId,
|
||||
ParticipantId: solution.ParticipantId,
|
||||
Solution: solution.Solution,
|
||||
State: solution.State,
|
||||
Score: solution.Score,
|
||||
Penalty: solution.Penalty,
|
||||
TotalScore: solution.TotalScore,
|
||||
Language: solution.Language,
|
||||
CreatedAt: solution.CreatedAt,
|
||||
UpdatedAt: solution.UpdatedAt,
|
||||
}},
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue