Skip to content

Commit

Permalink
Perf APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
slhmy committed Sep 21, 2024
1 parent c632393 commit 215fe8e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
38 changes: 22 additions & 16 deletions cmd/web_server/handler/judge.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ package handler

import (
"fmt"
"strconv"

"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/oj-lab/platform/cmd/web_server/middleware"
"github.com/oj-lab/platform/models"
judge_model "github.com/oj-lab/platform/models/judge"
gin_utils "github.com/oj-lab/platform/modules/utils/gin"
judge_service "github.com/oj-lab/platform/services/judge"
user_service "github.com/oj-lab/platform/services/user"
)

func SetupJudgeRouter(baseRoute *gin.RouterGroup) {
g := baseRoute.Group("/judge")
{
g.GET("", getJudgeList)
g.GET("/:uid", getJudge)
g.GET("", middleware.HandleRequireLogin, getJudgeList)
g.GET("/:uid", middleware.HandleRequireLogin, getJudge)
}
}

Expand Down Expand Up @@ -52,31 +53,36 @@ type getJudgeListResponse struct {
// @Param offset query int false "offset"
// @Router /judge [get] getJudgeListResponse
func getJudgeList(ginCtx *gin.Context) {
limitQuery, _ := ginCtx.GetQuery("limit")
offsetQuery, _ := ginCtx.GetQuery("offset")
if limitQuery == "" {
limitQuery = "10"
}
if offsetQuery == "" {
offsetQuery = "0"
}

limit, err := strconv.Atoi(limitQuery)
limit, err := gin_utils.QueryInt(ginCtx, "limit", 10)
if err != nil {
gin_utils.NewInvalidParamError(ginCtx, "limit", "invalid limit")
gin_utils.NewInvalidParamError(ginCtx, "limit", err.Error())
return
}
offset, err := strconv.Atoi(offsetQuery)
offset, err := gin_utils.QueryInt(ginCtx, "offset", 0)
if err != nil {
gin_utils.NewInvalidParamError(ginCtx, "offset", "invalid offset")
gin_utils.NewInvalidParamError(ginCtx, "offset", err.Error())
return
}
selfOnly := gin_utils.QueryBool(ginCtx, "self_only", false)

options := judge_model.GetJudgeOptions{
Limit: &limit,
Offset: &offset,
OrderByColumns: []models.OrderByColumnOption{{Column: "create_at", Desc: true}},
}
if selfOnly {
ls, err := middleware.GetLoginSessionFromGinCtx(ginCtx)
if err != nil {
gin_utils.NewUnauthorizedError(ginCtx, "cannot load login session from cookie")
return
}
user, err := user_service.GetUser(ginCtx, ls.Key.Account)
if err != nil {
gin_utils.NewInternalError(ginCtx, fmt.Sprintf("failed to get user: %v", err))
return
}
options.UserAccount = user.Account
}

judges, total, err := judge_service.GetJudgeList(ginCtx, options)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion cmd/web_server/handler/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handler

import (
"github.com/gin-gonic/gin"
"github.com/oj-lab/platform/cmd/web_server/middleware"
judge_model "github.com/oj-lab/platform/models/judge"
problem_model "github.com/oj-lab/platform/models/problem"
gin_utils "github.com/oj-lab/platform/modules/utils/gin"
Expand All @@ -18,7 +19,7 @@ func SetupProblemRouter(baseRoute *gin.RouterGroup) {
g.DELETE("/:slug", deleteProblem)
g.GET("/:slug/check", checkProblemSlug)
g.PUT("/:slug/package", putProblemPackage)
g.POST("/:slug/judge", postJudge)
g.POST("/:slug/judge", middleware.HandleRequireLogin, postJudge)
}
}

Expand Down
12 changes: 12 additions & 0 deletions modules/utils/gin/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,15 @@ func QueryString(ginCtx *gin.Context, key string, defaultValue string) string {
}
return ValueStr
}

func QueryBool(ginCtx *gin.Context, key string, defaultValue bool) bool {
ValueStr := ginCtx.Query(key)
switch ValueStr {
case "true":
return true
case "false":
return false
default:
return defaultValue
}
}

0 comments on commit 215fe8e

Please sign in to comment.