Skip to content

Commit

Permalink
Adjust problem model
Browse files Browse the repository at this point in the history
  • Loading branch information
slhmy committed Aug 22, 2024
1 parent 87b00d9 commit 57590b9
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 16 deletions.
4 changes: 2 additions & 2 deletions cmd/clean/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ func clearDB() {
err := db.Migrator().DropTable(
&user_model.User{},
&problem_model.Problem{},
&problem_model.AlgorithmTag{},
&problem_model.ProblemTag{},
&judge_model.Judge{},
&judge_model.JudgeResult{},
"problem_algorithm_tags",
"problem_problem_tags",
"casbin_rule",
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/init/problem_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func loadProblemPackages(ctx context.Context) {
Slug: slug,
Title: title,
Description: &description,
Tags: []*problem_model.AlgorithmTag{
Tags: []*problem_model.ProblemTag{
{Name: "to-be-add"},
},
})
Expand Down
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ require (
gorm.io/gorm v1.25.10
)

require github.com/stretchr/testify v1.9.0
require (
github.com/stretchr/testify v1.9.0
github.com/swaggo/swag v1.16.3
)

require (
github.com/ClickHouse/ch-go v0.61.5 // indirect
Expand Down Expand Up @@ -41,7 +44,6 @@ require (
github.com/segmentio/asm v1.2.0 // indirect
github.com/shopspring/decimal v1.4.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/swaggo/swag v1.16.3 // indirect
go.opentelemetry.io/otel v1.26.0 // indirect
go.opentelemetry.io/otel/trace v1.26.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
Expand Down
13 changes: 7 additions & 6 deletions models/problem/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import "github.com/oj-lab/oj-lab-platform/models"

type Problem struct {
models.MetaFields
Slug string `json:"slug" gorm:"primaryKey"`
Title string `json:"title" gorm:"not null"`
Description *string `json:"description,omitempty"`
Tags []*AlgorithmTag `json:"tags" gorm:"many2many:problem_algorithm_tags;"`
Slug string `json:"slug" gorm:"primaryKey"`
Title string `json:"title" gorm:"not null"`
Description *string `json:"description,omitempty"`
Tags []*ProblemTag `json:"tags" gorm:"many2many:problem_problem_tags;"`
Solved bool `json:"solved,omitempty" gorm:"-"`
}

type AlgorithmTag struct {
type ProblemTag struct {
models.MetaFields
Name string `json:"name" gorm:"primaryKey"`
Problems []*Problem `json:"problems,omitempty" gorm:"many2many:problem_algorithm_tags;"`
Problems []*Problem `json:"problems,omitempty" gorm:"many2many:problem_problem_tags;"`
}

var ProblemInfoSelection = append([]string{"slug", "title"}, models.MetaFieldsSelection...)
6 changes: 3 additions & 3 deletions models/problem/problem_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type GetProblemOptions struct {
Selection []string
Slug *string
Title *string
Tags []*AlgorithmTag
Tags []*ProblemTag
Offset *int
Limit *int
}
Expand All @@ -51,8 +51,8 @@ func buildGetProblemTXByOptions(tx *gorm.DB, options GetProblemOptions, isCount
}
if len(tagsList) > 0 {
tx = tx.
Joins("JOIN problem_algorithm_tags ON problem_algorithm_tags.problem_slug = problems.slug").
Where("problem_algorithm_tags.algorithm_tag_name in ?", tagsList)
Joins("JOIN problem_problem_tags ON problem_problem_tags.problem_slug = problems.slug").
Where("problem_problem_tags.problem_tag_name in ?", tagsList)
}
if options.Slug != nil {
tx = tx.Where("slug = ?", *options.Slug)
Expand Down
4 changes: 2 additions & 2 deletions models/problem/problem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestProblemDB(t *testing.T) {
Slug: "a-plus-b-problem",
Title: "A+B Problem",
Description: &description,
Tags: []*AlgorithmTag{{Name: "tag1"}, {Name: "tag2"}},
Tags: []*ProblemTag{{Name: "tag1"}, {Name: "tag2"}},
}

err := CreateProblem(db, problem)
Expand All @@ -36,7 +36,7 @@ func TestProblemDB(t *testing.T) {

problemOption := GetProblemOptions{
Selection: ProblemInfoSelection,
Tags: []*AlgorithmTag{{Name: "tag1"}},
Tags: []*ProblemTag{{Name: "tag1"}},
Slug: &problem.Slug,
}

Expand Down

0 comments on commit 57590b9

Please sign in to comment.