feat(tester): extend GetContestResponse

This commit is contained in:
Vyacheslav1557 2025-03-02 00:29:31 +05:00
parent e6088953b9
commit 81d7aa2366
17 changed files with 539 additions and 238 deletions

View file

@ -53,14 +53,49 @@ func (h *TesterHandlers) GetContest(c *fiber.Ctx, id int32) error {
return c.SendStatus(pkg.ToREST(err))
}
return c.JSON(testerv1.GetContestResponse{
//token, ok := c.Locals(TokenKey).(*models.JWT)
//if !ok {
// return c.SendStatus(fiber.StatusUnauthorized)
//}
tasks, err := h.contestsUC.ReadRichTasks(c.Context(), id)
if err != nil {
return c.SendStatus(pkg.ToREST(err))
}
resp := testerv1.GetContestResponse{
Contest: testerv1.Contest{
Id: *contest.Id,
Id: id,
Title: *contest.Title,
CreatedAt: *contest.CreatedAt,
UpdatedAt: *contest.UpdatedAt,
},
})
Tasks: make([]struct {
BestSolution testerv1.BestSolution `json:"best_solution"`
Task testerv1.RichTask `json:"task"`
}, len(tasks)),
}
for i, task := range tasks {
resp.Tasks[i] = struct {
BestSolution testerv1.BestSolution `json:"best_solution"`
Task testerv1.RichTask `json:"task"`
}{
BestSolution: testerv1.BestSolution{},
Task: testerv1.RichTask{
Id: task.Id,
ProblemId: task.ProblemId,
Position: task.Position,
Title: task.Title,
MemoryLimit: task.MemoryLimit,
TimeLimit: task.TimeLimit,
CreatedAt: task.CreatedAt,
UpdatedAt: task.UpdatedAt,
},
}
}
return c.JSON(resp)
}
func (h *TesterHandlers) DeleteParticipant(c *fiber.Ctx, id int32, params testerv1.DeleteParticipantParams) error {
@ -130,17 +165,17 @@ func (h *TesterHandlers) GetProblem(c *fiber.Ctx, id int32) error {
return c.JSON(
testerv1.GetProblemResponse{Problem: testerv1.Problem{
Id: *problem.Id,
Legend: *problem.Legend,
InputFormat: *problem.InputFormat,
OutputFormat: *problem.OutputFormat,
Notes: *problem.Notes,
Tutorial: *problem.Tutorial,
LatexSummary: *problem.LatexSummary,
TimeLimit: *problem.TimeLimit,
MemoryLimit: *problem.MemoryLimit,
CreatedAt: *problem.CreatedAt,
UpdatedAt: *problem.UpdatedAt,
Id: problem.Id,
Legend: problem.Legend,
InputFormat: problem.InputFormat,
OutputFormat: problem.OutputFormat,
Notes: problem.Notes,
Tutorial: problem.Tutorial,
LatexSummary: problem.LatexSummary,
TimeLimit: problem.TimeLimit,
MemoryLimit: problem.MemoryLimit,
CreatedAt: problem.CreatedAt,
UpdatedAt: problem.UpdatedAt,
}},
)
}

View file

@ -0,0 +1,71 @@
package rest
import (
"fmt"
"git.sch9.ru/new_gate/ms-tester/internal/models"
"github.com/gofiber/fiber/v2"
"github.com/golang-jwt/jwt/v4"
"strings"
)
const (
TokenKey = "token"
)
func AuthMiddleware(jwtSecret string) fiber.Handler {
return func(c *fiber.Ctx) error {
const op = "AuthMiddleware"
authHeader := c.Get("Authorization", "")
if authHeader == "" {
c.Locals(TokenKey, nil)
return c.Next()
}
authParts := strings.Split(authHeader, " ")
if len(authParts) != 2 || strings.ToLower(authParts[0]) != "bearer" {
c.Locals(TokenKey, nil)
return c.Next()
}
parsedToken, err := jwt.ParseWithClaims(authParts[1], &models.JWT{}, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
return []byte(jwtSecret), nil
})
if err != nil {
c.Locals(TokenKey, nil)
return c.Next()
}
token, ok := parsedToken.Claims.(*models.JWT)
if !ok {
c.Locals(TokenKey, nil)
return c.Next()
}
err = token.Valid()
if err != nil {
c.Locals(TokenKey, nil)
return c.Next()
}
//ctx := c.Context()
// check if session exists
//_, err = userUC.ReadSession(ctx, token.SessionId)
//if err != nil {
// if errors.Is(err, pkg.ErrNotFound) {
// c.Locals(TokenKey, nil)
// return c.Next()
// }
//
// return c.SendStatus(pkg.ToREST(err))
//}
c.Locals(TokenKey, token)
return c.Next()
}
}