Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cause - 错因系统 #99

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions app/request/cause.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package request

type ReadCauseRequest struct {
}

type ReadCausesRequest struct {
}

type UpdateCauseRequest struct {
Description string `json:"description" form:"description" query:"description"`
Point string `json:"point" form:"point" query:"point" validate:"max=100,min=0"`
}

type DeleteCauseRequest struct {
}
35 changes: 35 additions & 0 deletions app/response/cause.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package response

import "github.com/EduOJ/backend/app/response/resource"

type ReadCauseResponseForAdmin struct {
Message string `json:"message"`
Error interface{} `json:"error"`
Data struct {
*resource.CauseForAdmin `json:"cause"`
} `json:"data"`
}

type ReadCauseResponse struct {
Message string `json:"message"`
Error interface{} `json:"error"`
Data struct {
*resource.Cause `json:"cause"`
} `json:"data"`
}

type ReadCausesResponse struct {
Message string `json:"message"`
Error interface{} `json:"error"`
Data struct {
Causes []resource.CauseForAdmin `json:"causes"`
} `json:"data"`
}

type UpdateCauseResponse struct {
Message string `json:"message"`
Error interface{} `json:"error"`
Data struct {
*resource.CauseForAdmin `json:"cause"`
} `json:"data"`
}
69 changes: 69 additions & 0 deletions app/response/resource/cause.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package resource

import "github.com/EduOJ/backend/database/models"

type CauseForAdmin struct {
ID uint `son:"id"`
ProblemID uint `json:"problem_id"`
TestCaseID uint `json:"test_case_id"`

Hash string `json:"output_stripped_hash"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

json就叫hash就行?

Description string `json:"description"`

// Point: Points to be subtracted for this cause
Point uint `json:"point"`
Marked bool `json:"marked"`
Count uint `json:"count"`
}

type Cause struct {
ID uint `gorm:"primaryKey" json:"id"`
ProblemID uint `sql:"index" json:"problem_id"`
Problem *Problem `json:"problem"`
TestCaseID uint `sql:"index" json:"test_case_id"`
TestCase *TestCase `json:"test_case"`

Hash string `json:"output_stripped_hash" gorm:"index;not null;size:255;default:''"`
Description string `json:"description"`
Marked bool `json:"marked" gorm:"default:false;not null"`
}

func (c *CauseForAdmin) convert(cause *models.Cause) {
c.ID = cause.ID
c.ProblemID = cause.ProblemID
c.TestCaseID = cause.TestCaseID
c.Hash = cause.Hash
c.Description = cause.Description
c.Point = cause.Point
c.Marked = cause.Marked
c.Count = cause.Count
}

func (c *Cause) convert(cause *models.Cause) {
c.ID = cause.ID
c.ProblemID = cause.ProblemID
c.TestCaseID = cause.TestCaseID
c.Hash = cause.Hash
c.Description = cause.Description
c.Marked = cause.Marked
}

func GetCauseForAdmin(cause *models.Cause) *CauseForAdmin {
c := CauseForAdmin{}
c.convert(cause)
return &c
}

func GetCause(cause *models.Cause) *Cause {
c := Cause{}
c.convert(cause)
return &c
}

func GetCauseForAdminSlice(causes []*models.Cause) []CauseForAdmin {
causeSlice := make([]CauseForAdmin, len(causes))
for i, cause := range causes {
causeSlice[i].convert(cause)
}
return causeSlice
}
13 changes: 13 additions & 0 deletions app/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func Register(e *echo.Echo) {
middleware.ValidateParams(map[string]string{
"id": "NOT_FOUND",
"test_case_id": "TEST_CASE_NOT_FOUND",
"cause_id": "CAUSE_NOT_FOUND",
}),
middleware.Logged,
middleware.HasPermission(middleware.OrPermission{
Expand Down Expand Up @@ -134,6 +135,18 @@ func Register(e *echo.Echo) {
updateProblem.PUT("/admin/problem/:id/test_case/:test_case_id", controller.UpdateTestCase).Name = "problem.updateTestCase"
updateProblem.DELETE("/admin/problem/:id/test_case/all", controller.DeleteTestCases).Name = "problem.deleteTestCases"
updateProblem.DELETE("/admin/problem/:id/test_case/:test_case_id", controller.DeleteTestCase).Name = "problem.deleteTestCase"
// cause APIs
api.GET("/problem/:id/test_case/:test_case_id/cause/:cause_id", controller.Todo,
middleware.ValidateParams(map[string]string{
"id": "NOT_FOUND",
"test_case_id": "TEST_CASE_NOT_FOUND",
"cause_id": "CAUSE_NOT_FOUND",
}),
middleware.Logged,
).Name = "problem.getCause"
readProblemSecret.GET("/problem/:id/test_case/:test_case_id/causes", controller.Todo).Name = "problem.getCauses"
updateProblem.PUT("/problem/:id/test_case/:test_case_id/cause/:cause_id", controller.Todo).Name = "problem.updateCause"
updateProblem.DELETE("/problem/:id/test_case/:test_case_id/cause/:cause_id", controller.Todo).Name = "problem.deleteCause"

// submission APIs
submission := api.Group("",
Expand Down
52 changes: 52 additions & 0 deletions database/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,58 @@ func GetMigration() *gormigrate.Gormigrate {
return tx.Migrator().DropColumn(&Grade{}, "ClassID")
},
},
{
// add table cause and add index for field output_stripped_hash in table run
ID: "add_cause",
Migrate: func(tx *gorm.DB) error {
type Problem struct {
ID uint `gorm:"primaryKey" json:"id"`
}
type TestCase struct {
ID uint `gorm:"primaryKey" json:"id"`
}
type Cause struct {
ID uint `gorm:"primaryKey" json:"id"`
ProblemID uint `sql:"index" json:"problem_id"`
Problem *Problem `json:"problem"`
TestCaseID uint `sql:"index" json:"test_case_id"`
TestCase *TestCase `json:"test_case"`

Hash string `json:"hash" gorm:"index;not null;size:255;default:''"`
Description string `json:"description"`

// Point: Points to be subtracted for this cause
Point uint `json:"point" gorm:"default:0;not null"`
Marked bool `json:"marked" gorm:"default:false;not null"`
Count uint `json:"count" gorm:"default:0;not null"`

CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"-"`
DeletedAt gorm.DeletedAt `sql:"index" json:"deleted_at"`
}
type Run struct {
ID uint `gorm:"primaryKey" json:"id"`
OutputStrippedHash string `json:"output_stripped_hash" gorm:"index;not null;size:255;default:''"`
}
return tx.AutoMigrate(&Cause{}, &Run{})
},
Rollback: func(tx *gorm.DB) error {
var err error
type Run struct {
ID uint `gorm:"primaryKey" json:"id"`
OutputStrippedHash string `json:"output_stripped_hash" gorm:""`
}
err = tx.Migrator().AlterColumn(&Run{}, "OutputStrippedHash")
if err != nil {
return err
}
err = tx.Migrator().DropIndex(&Run{}, "idx_runs_output_stripped_hash")
if err != nil {
return err
}
return tx.Migrator().DropTable("causes")
},
},
})
}

Expand Down
26 changes: 26 additions & 0 deletions database/models/cause.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package models

import (
"gorm.io/gorm"
"time"
)

type Cause struct {
ID uint `gorm:"primaryKey" json:"id"`
ProblemID uint `sql:"index" json:"problem_id"`
Problem *Problem `json:"problem"`
TestCaseID uint `sql:"index" json:"test_case_id"`
TestCase *TestCase `json:"test_case"`

Hash string `json:"output_stripped_hash" gorm:"index;not null;size:255;default:''"`
Description string `json:"description"`

// Point: Points to be subtracted for this cause
Point uint `json:"point" gorm:"default:0;not null"`
Marked bool `json:"marked" gorm:"default:false;not null"`
Count uint `json:"count" gorm:"default:0;not null"`

CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"-"`
DeletedAt gorm.DeletedAt `sql:"index" json:"deleted_at"`
}
2 changes: 1 addition & 1 deletion database/models/submission.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type Run struct {
Status string `json:"status"`
MemoryUsed uint `json:"memory_used"` // Byte
TimeUsed uint `json:"time_used"` // ms
OutputStrippedHash string `json:"output_stripped_hash"`
OutputStrippedHash string `json:"output_stripped_hash" gorm:"index;not null;size:255;default:''"`

CreatedAt time.Time `sql:"index" json:"created_at"`
UpdatedAt time.Time `json:"-"`
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/EduOJ/backend
go 1.14

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/duo-labs/webauthn v0.0.0-20200714211715-1daaee874e43
github.com/fatih/color v1.10.0
github.com/gabriel-vasile/mimetype v1.1.2
Expand Down