Skip to content

Commit

Permalink
Add submission service
Browse files Browse the repository at this point in the history
  • Loading branch information
slhmy committed Oct 22, 2023
1 parent 076952d commit 2cda668
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 20 deletions.
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,12 @@ run-server: build check
run-rpc-server: build check
./bin/rpc_server

.PHONY: run
run: build check
.PHONY: run-backgroud
run-backgroud: build check
make -j run-task-worker run-schedule

.PHONY: run-all
run-all: build check
make -j run-task-worker run-server run-schedule

.PHONY: help
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ Currently the backend server for OJ Lab.
For service development, we don't want to make it too complex.
Using VSCode on either Win/*nix System are avaliabe, try using the Makefile/Dockerfile in the repository.

### Run Processes

It's more recommended to use 2 terminals to run the services and background workers seperately.
In this way you will get a better experience of debugging.
(background workers will produce a lot of logs, since they are running in a loop)

```bash
# Terminal 1
make run-server
# Terminal 2
make run-background
```

Alternatively, you can use `make run-all` to run all the processes in one terminal.

## Serve Frontend

Use `make get-front` to get the frontend dist codes.
Expand Down
18 changes: 0 additions & 18 deletions application/server/handler/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func SetupProblemRoute(baseRoute *gin.RouterGroup) {
g.GET("/:slug", getProblem)
g.PUT("/:slug/package", putProblemPackage)
g.POST("/:slug/submission", postSubmission)
// g.POST("/:slug/judge", judge)
}
}

Expand Down Expand Up @@ -109,20 +108,3 @@ func postSubmission(ginCtx *gin.Context) {

ginCtx.JSON(200, submission)
}

// func judge(ginCtx *gin.Context) {
// slug := ginCtx.Param("slug")
// body := model.JudgeTask{}
// if err := ginCtx.ShouldBindJSON(&body); err != nil {
// ginCtx.Error(err)
// return
// }

// responseBody, err := service.Judge(ginCtx, slug, body.Code, body.Language)
// if err != nil {
// ginCtx.Error(err)
// return
// }

// ginCtx.JSON(200, responseBody)
// }
71 changes: 71 additions & 0 deletions application/server/handler/submission.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package handler

import (
"strconv"

"github.com/OJ-lab/oj-lab-services/core"
"github.com/OJ-lab/oj-lab-services/service"
"github.com/OJ-lab/oj-lab-services/service/mapper"
"github.com/OJ-lab/oj-lab-services/service/model"
"github.com/gin-gonic/gin"
)

func SetupSubmissionRouter(baseRoute *gin.RouterGroup) {
g := baseRoute.Group("/submission")
{
g.GET("", getSubmissionList)
}
}

type getSubmissionResponseBody struct {
Total int64 `json:"total"`
Data []*model.JudgeTaskSubmission `json:"data"`
}

// Get Submission List
//
// @Summary Get submission list
// @Description Get submission list
// @Tags submission
// @Accept json
// @Param limit query int false "limit"
// @Param offset query int false "offset"
// @Router /submission [get]
// @Success 200 {object} getSubmissionResponseBody
func getSubmissionList(ginCtx *gin.Context) {
limitQuery, _ := ginCtx.GetQuery("limit")
offsetQuery, _ := ginCtx.GetQuery("offset")
if limitQuery == "" {
limitQuery = "10"
}
if offsetQuery == "" {
offsetQuery = "0"
}

limit, err := strconv.Atoi(limitQuery)
if err != nil {
core.NewInvalidParamError("limit", "invalid limit").AppendToGin(ginCtx)
return
}
offset, err := strconv.Atoi(offsetQuery)
if err != nil {
core.NewInvalidParamError("offset", "invalid offset").AppendToGin(ginCtx)
return
}

options := mapper.GetSubmissionOptions{
Limit: &limit,
Offset: &offset,
}

submissions, total, svcErr := service.GetJudgeTaskSubmissionList(ginCtx, options)
if svcErr != nil {
svcErr.AppendToGin(ginCtx)
return
}

ginCtx.JSON(200, getSubmissionResponseBody{
Total: total,
Data: submissions,
})
}
1 change: 1 addition & 0 deletions application/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func main() {
handler.SetupUserRouter(apiRouter)
handler.SetupProblemRoute(apiRouter)
handler.SetupEventRouter(apiRouter)
handler.SetupSubmissionRouter(apiRouter)

err := r.Run(servicePort)
if err != nil {
Expand Down
34 changes: 34 additions & 0 deletions service/submission.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package service

import (
"context"

"github.com/OJ-lab/oj-lab-services/core"
gormAgent "github.com/OJ-lab/oj-lab-services/core/agent/gorm"
"github.com/OJ-lab/oj-lab-services/service/mapper"
"github.com/OJ-lab/oj-lab-services/service/model"
)

func GetJudgeTaskSubmissionList(
ctx context.Context, options mapper.GetSubmissionOptions,
) ([]*model.JudgeTaskSubmission, int64, *core.SeviceError) {
db := gormAgent.GetDefaultDB()
submissions, total, err := mapper.GetSubmissionListByOptions(db, options)
if err != nil {
return nil, 0, core.NewInternalError("failed to get submission list")
}

return submissions, total, nil
}

func CreateJudgeTaskSubmission(
ctx context.Context, submission model.JudgeTaskSubmission,
) (*model.JudgeTaskSubmission, *core.SeviceError) {
db := gormAgent.GetDefaultDB()
newSubmission, err := mapper.CreateSubmission(db, submission)
if err != nil {
return nil, core.NewInternalError("failed to create submission")
}

return newSubmission, nil
}

0 comments on commit 2cda668

Please sign in to comment.