From 3413b136b16e74e352b330705477efe20e59a24d Mon Sep 17 00:00:00 2001 From: Paul Pan Date: Sun, 28 Apr 2024 23:19:00 +0800 Subject: [PATCH] feat: update api --- internal/api/problem/create_version.go | 2 +- internal/api/problem/details.go | 1 + internal/api/problem/search.go | 8 ++++++-- internal/api/submission/create.go | 8 ++++++++ internal/model/Problem.go | 1 + 5 files changed, 17 insertions(+), 3 deletions(-) diff --git a/internal/api/problem/create_version.go b/internal/api/problem/create_version.go index 457d6aa..7978ddf 100644 --- a/internal/api/problem/create_version.go +++ b/internal/api/problem/create_version.go @@ -69,5 +69,5 @@ func (h *handler) CreateVersion(c *gin.Context) { _, status = h.taskService.ProblemBuild(payload) e.Pong[any](c, status, nil) - // TODO: if failed, delete problem version + // consumer/problemUpdate.go will update problem version status } diff --git a/internal/api/problem/details.go b/internal/api/problem/details.go index 3e659f2..07c19cb 100644 --- a/internal/api/problem/details.go +++ b/internal/api/problem/details.go @@ -32,6 +32,7 @@ func (h *handler) Details(c *gin.Context) { return } + // Only Admin is able to view disabled problems claim, exist := c.Get("claim") shouldEnable := !exist || claim.(*model.Claim).Role < model.RoleAdmin diff --git a/internal/api/problem/search.go b/internal/api/problem/search.go index b72c73a..6d74cf6 100644 --- a/internal/api/problem/search.go +++ b/internal/api/problem/search.go @@ -16,7 +16,7 @@ type searchRequest struct { // Search // @Summary search for problems -// @Description Search for problems based on keywords. If the keyword is empty, return all problems. +// @Description Search for problems based on keywords. If the keyword is empty, return all problems. Admin could view all problems. // @Tags problem // @Accept application/x-www-form-urlencoded // @Produce json @@ -33,12 +33,16 @@ func (h *handler) Search(c *gin.Context) { return } + // Only Admin is able to view disabled problems + claim, exist := c.Get("claim") + shouldEnable := !exist || claim.(*model.Claim).Role < model.RoleAdmin + var count int64 param := problem.QueryData{ Keyword: req.Keyword, Tag: req.Tag, Associations: true, - ShouldEnable: true, + ShouldEnable: shouldEnable, Offset: req.Offset, Limit: req.Limit, Count: &count, diff --git a/internal/api/submission/create.go b/internal/api/submission/create.go index 063deea..b9d63af 100644 --- a/internal/api/submission/create.go +++ b/internal/api/submission/create.go @@ -3,6 +3,7 @@ package submission import ( "git.0x7f.app/WOJ/woj-server/internal/e" "git.0x7f.app/WOJ/woj-server/internal/model" + "git.0x7f.app/WOJ/woj-server/internal/service/problem" "git.0x7f.app/WOJ/woj-server/internal/service/submission" "github.com/gin-gonic/gin" ) @@ -47,6 +48,13 @@ func (h *handler) Create(c *gin.Context) { return } + // check problem enabled + _, status := h.problemService.Query(&problem.QueryData{ID: req.Pid, Associations: false, ShouldEnable: role < model.RoleAdmin}) + if status != e.Success { + e.Pong[any](c, status, nil) + return + } + // query latest version pv, status := h.problemService.QueryLatestVersion(req.Pid) if status != e.Success { diff --git a/internal/model/Problem.go b/internal/model/Problem.go index 6f3d0e1..03edccf 100644 --- a/internal/model/Problem.go +++ b/internal/model/Problem.go @@ -18,6 +18,7 @@ type Problem struct { type ProblemVersion struct { gorm.Model `json:"meta"` ProblemID uint `json:"-" gorm:"not null;index"` + Problem Problem `json:"problem" gorm:"foreignKey:ProblemID"` Context pgtype.JSON `json:"context" gorm:"type:json"` StorageKey string `json:"-" gorm:"not null"` IsEnabled bool `json:"is_enabled" gorm:"not null;index"`