diff --git a/go.mod b/go.mod index 8db94ce..ce789c6 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/gin-gonic/gin v1.8.1 github.com/go-redis/redis/v8 v8.11.5 github.com/golang-jwt/jwt/v4 v4.4.2 + github.com/lib/pq v1.10.2 github.com/prometheus/client_golang v1.13.0 github.com/swaggo/files v0.0.0-20220728132757-551d4a08d97a github.com/swaggo/gin-swagger v1.5.3 diff --git a/go.sum b/go.sum index d2106f7..fd55f08 100644 --- a/go.sum +++ b/go.sum @@ -415,8 +415,9 @@ go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/goleak v1.1.11-0.20210813005559-691160354723/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= -go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI= go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= +go.uber.org/goleak v1.1.12 h1:gZAh5/EyT/HQwlpkCy6wTpqfH9H8Lz8zbm3dZh+OyzA= +go.uber.org/goleak v1.1.12/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4= go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU= diff --git a/internal/api/user/login.go b/internal/api/user/login.go index 78c6767..a5812a3 100644 --- a/internal/api/user/login.go +++ b/internal/api/user/login.go @@ -19,13 +19,13 @@ type loginRequest struct { // @Produce json // @Param username formData string true "username" // @Param password formData string true "password" -// @Response 200 {object} e.Response "jwt token" +// @Response 200 {object} e.Response "jwt token and user nickname" // @Router /v1/user/login [post] func (h *handler) Login(c *gin.Context) { req := new(loginRequest) if err := c.ShouldBind(req); err != nil { - e.Pong(c, e.InvalidParameter, err.Error()) + e.Pong(c, e.InvalidParameter, nil) return } @@ -52,5 +52,8 @@ func (h *handler) Login(c *gin.Context) { Version: version, } token, status := h.jwtService.SignClaim(claim) - e.Pong(c, status, token) + e.Pong(c, status, gin.H{ + "token": token, + "nickname": user.NickName, + }) } diff --git a/internal/api/user/profile.go b/internal/api/user/profile.go index b8b6117..a7bd388 100644 --- a/internal/api/user/profile.go +++ b/internal/api/user/profile.go @@ -34,7 +34,7 @@ func (h *handler) Profile(c *gin.Context) { req := new(profileRequest) if err := c.ShouldBind(req); err == nil { if req.UID != 0 && req.UID != uid { - if role >= model.RoleAdmin { + if role >= model.RoleGeneral { uid = req.UID } else { e.Pong(c, e.UserUnauthorized, nil) diff --git a/internal/repo/model/Problem.go b/internal/repo/model/Problem.go index 075ce26..2e2de22 100644 --- a/internal/repo/model/Problem.go +++ b/internal/repo/model/Problem.go @@ -1,14 +1,19 @@ package model -import "gorm.io/gorm" +import ( + "github.com/lib/pq" + "gorm.io/gorm" +) type Problem struct { gorm.Model `json:"meta"` - Title string `json:"title" gorm:"not null"` - Content string `json:"content" gorm:"not null"` - TimeLimit uint `json:"time_limit" gorm:"not null"` - MemoryLimit uint `json:"memory_limit" gorm:"not null"` - ProviderID uint `json:"provider_id" gorm:"not null;index"` - Provider User `json:"-" gorm:"foreignKey:ProviderID"` - IsEnabled bool `json:"is_enabled" gorm:"not null;index"` + Title string `json:"title" gorm:"not null"` + Content string `json:"content" gorm:"not null"` + TimeLimit uint `json:"time_limit" gorm:"not null"` + MemoryLimit uint `json:"memory_limit" gorm:"not null"` + ProviderID uint `json:"provider_id" gorm:"not null;index"` + Provider User `json:"-" gorm:"foreignKey:ProviderID"` + Languages pq.Int32Array `json:"languages" gorm:"type:int[]"` + Points pq.Int32Array `json:"points" gorm:"type:int[]"` + IsEnabled bool `json:"is_enabled" gorm:"not null;index"` } diff --git a/internal/repo/model/Status.go b/internal/repo/model/Status.go new file mode 100644 index 0000000..76fab37 --- /dev/null +++ b/internal/repo/model/Status.go @@ -0,0 +1,11 @@ +package model + +import "gorm.io/gorm" + +type Status struct { + gorm.Model `json:"-"` + SubmissionID uint `json:"submission_id" gorm:"not null;index"` + Submission Submission `json:"-" gorm:"foreignKey:SubmissionID"` + Verdict Verdict `json:"verdict" gorm:"not null"` + Point int32 `json:"point" gorm:"not null"` +} diff --git a/internal/repo/model/Submission.go b/internal/repo/model/Submission.go new file mode 100644 index 0000000..26407a1 --- /dev/null +++ b/internal/repo/model/Submission.go @@ -0,0 +1,13 @@ +package model + +import "gorm.io/gorm" + +type Submission struct { + gorm.Model `json:"-"` + ProblemID uint `json:"problem_id" gorm:"not null;index"` + Problem Problem `json:"-" gorm:"foreignKey:ProblemID"` + UserID uint `json:"user_id" gorm:"not null;index"` + User User `json:"-" gorm:"foreignKey:UserID"` + Language Lang `json:"language" gorm:"not null"` + Code string `json:"code" gorm:"not null"` +} diff --git a/internal/repo/model/Verdict.go b/internal/repo/model/Verdict.go new file mode 100644 index 0000000..6fafc69 --- /dev/null +++ b/internal/repo/model/Verdict.go @@ -0,0 +1,17 @@ +package model + +type Verdict int + +const ( + VerdictJudging Verdict = 1 + VerdictAccepted Verdict = 2 + VerdictWrongAnswer Verdict = 3 + VerdictTimeLimitExceeded Verdict = 4 + VerdictMemoryLimitExceeded Verdict = 5 + VerdictRuntimeError Verdict = 6 + VerdictCompileError Verdict = 7 + VerdictSystemError Verdict = 8 + VerdictJuryFailed Verdict = 9 + VerdictSkipped Verdict = 10 + VerdictPartiallyCorrect Verdict = 11 +) diff --git a/internal/repo/postgresql/postgresql.go b/internal/repo/postgresql/postgresql.go index 54470fb..40fe689 100644 --- a/internal/repo/postgresql/postgresql.go +++ b/internal/repo/postgresql/postgresql.go @@ -83,6 +83,7 @@ func (r *Repo) migrateDatabase() { _ = r.db.AutoMigrate(&model.User{}) _ = r.db.AutoMigrate(&model.Problem{}) + _ = r.db.AutoMigrate(&model.Submission{}) } // checkAlive deprecated diff --git a/internal/service/submission/service.go b/internal/service/submission/service.go new file mode 100644 index 0000000..96b2f2a --- /dev/null +++ b/internal/service/submission/service.go @@ -0,0 +1,24 @@ +package submission + +import ( + "github.com/WHUPRJ/woj-server/internal/global" + "github.com/WHUPRJ/woj-server/internal/repo/amqp" + "go.uber.org/zap" +) + +var _ Service = (*service)(nil) + +type Service interface { +} + +type service struct { + log *zap.Logger + mq *amqp.Repo +} + +func NewService(g *global.Global) Service { + return &service{ + log: g.Log, + mq: g.Mq.Get().(*amqp.Repo), + } +} diff --git a/internal/service/submission/submit.go b/internal/service/submission/submit.go new file mode 100644 index 0000000..cb33958 --- /dev/null +++ b/internal/service/submission/submit.go @@ -0,0 +1 @@ +package submission