feat(tester): extend GetContestResponse
This commit is contained in:
parent
e6088953b9
commit
81d7aa2366
17 changed files with 539 additions and 238 deletions
|
@ -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,
|
||||
}},
|
||||
)
|
||||
}
|
||||
|
|
71
internal/tester/delivery/rest/middlewares.go
Normal file
71
internal/tester/delivery/rest/middlewares.go
Normal 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()
|
||||
}
|
||||
}
|
|
@ -19,4 +19,5 @@ type ContestRepository interface {
|
|||
DeleteTask(ctx context.Context, taskId int32) error
|
||||
AddParticipant(ctx context.Context, contestId int32, userId int32) (int32, error)
|
||||
DeleteParticipant(ctx context.Context, participantId int32) error
|
||||
ReadRichTasks(ctx context.Context, contestId int32) ([]*models.RichTask, error)
|
||||
}
|
||||
|
|
|
@ -4,18 +4,15 @@ import (
|
|||
"context"
|
||||
"git.sch9.ru/new_gate/ms-tester/internal/models"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type ContestRepository struct {
|
||||
db *sqlx.DB
|
||||
logger *zap.Logger
|
||||
db *sqlx.DB
|
||||
}
|
||||
|
||||
func NewContestRepository(db *sqlx.DB, logger *zap.Logger) *ContestRepository {
|
||||
func NewContestRepository(db *sqlx.DB) *ContestRepository {
|
||||
return &ContestRepository{
|
||||
db: db,
|
||||
logger: logger,
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -136,3 +133,28 @@ func (r *ContestRepository) DeleteParticipant(ctx context.Context, participantId
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
const readTasksQuery = `SELECT tasks.id,
|
||||
problem_id,
|
||||
contest_id,
|
||||
position,
|
||||
title,
|
||||
memory_limit,
|
||||
time_limit,
|
||||
tasks.created_at,
|
||||
tasks.updated_at
|
||||
FROM tasks
|
||||
INNER JOIN problems ON tasks.problem_id = problems.id
|
||||
WHERE contest_id = ? ORDER BY position`
|
||||
|
||||
func (r *ContestRepository) ReadRichTasks(ctx context.Context, contestId int32) ([]*models.RichTask, error) {
|
||||
const op = "ContestRepository.ReadTasks"
|
||||
|
||||
var tasks []*models.RichTask
|
||||
query := r.db.Rebind(readTasksQuery)
|
||||
err := r.db.SelectContext(ctx, &tasks, query, contestId)
|
||||
if err != nil {
|
||||
return nil, handlePgErr(err, op)
|
||||
}
|
||||
return tasks, nil
|
||||
}
|
||||
|
|
|
@ -4,18 +4,17 @@ import (
|
|||
"context"
|
||||
"git.sch9.ru/new_gate/ms-tester/internal/models"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type ProblemRepository struct {
|
||||
db *sqlx.DB
|
||||
logger *zap.Logger
|
||||
db *sqlx.DB
|
||||
//logger *zap.Logger
|
||||
}
|
||||
|
||||
func NewProblemRepository(db *sqlx.DB, logger *zap.Logger) *ProblemRepository {
|
||||
func NewProblemRepository(db *sqlx.DB) *ProblemRepository {
|
||||
return &ProblemRepository{
|
||||
db: db,
|
||||
logger: logger,
|
||||
db: db,
|
||||
//logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -19,4 +19,5 @@ type ContestUseCase interface {
|
|||
DeleteTask(ctx context.Context, taskId int32) error
|
||||
AddParticipant(ctx context.Context, contestId int32, userId int32) (int32, error)
|
||||
DeleteParticipant(ctx context.Context, participantId int32) error
|
||||
ReadRichTasks(ctx context.Context, contestId int32) ([]*models.RichTask, error)
|
||||
}
|
||||
|
|
|
@ -45,3 +45,7 @@ func (uc *ContestUseCase) AddParticipant(ctx context.Context, contestId int32, u
|
|||
func (uc *ContestUseCase) DeleteParticipant(ctx context.Context, participantId int32) error {
|
||||
return uc.contestRepo.DeleteParticipant(ctx, participantId)
|
||||
}
|
||||
|
||||
func (uc *ContestUseCase) ReadRichTasks(ctx context.Context, contestId int32) ([]*models.RichTask, error) {
|
||||
return uc.contestRepo.ReadRichTasks(ctx, contestId)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue