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 23, 2024
1 parent 87b00d9 commit 635ddf7
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 30 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...)
20 changes: 7 additions & 13 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 Expand Up @@ -83,21 +83,15 @@ func CountProblemByOptions(tx *gorm.DB, options GetProblemOptions) (int64, error
return count, err
}

func GetProblemInfoListByOptions(tx *gorm.DB, options GetProblemOptions) ([]Problem, int64, error) {
total, err := CountProblemByOptions(tx, options)
if err != nil {
return nil, 0, err
}

func GetProblemListByOptions(tx *gorm.DB, options GetProblemOptions) ([]Problem, error) {
problemList := []Problem{}

tx = buildGetProblemTXByOptions(tx, options, false)
err = tx.Find(&problemList).Error
err := tx.Find(&problemList).Error
if err != nil {
return nil, 0, err
return nil, err
}

return problemList, total, nil
return problemList, nil
}

func GetTagsList(problem Problem) []string {
Expand Down
10 changes: 5 additions & 5 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,16 +36,16 @@ func TestProblemDB(t *testing.T) {

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

problemList, problemCount, err := GetProblemInfoListByOptions(db, problemOption)
problemList, err := GetProblemListByOptions(db, problemOption)
if err != nil {
t.Error(err)
}
fmt.Printf("problemCount: %d\n", problemCount)
if problemCount != 1 {
fmt.Printf("problemList: %v\n", problemList)
if len(problemList) != 1 {
t.Error("problemCount should be 1")
}

Expand Down
6 changes: 5 additions & 1 deletion services/problem/problem_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ func GetProblemInfoList(_ context.Context, limit, offset *int) ([]problem_model.
Offset: offset,
}

problemList, total, err := problem_model.GetProblemInfoListByOptions(db, getOptions)
total, err := problem_model.CountProblemByOptions(db, getOptions)
if err != nil {
return nil, 0, err
}
problemList, err := problem_model.GetProblemListByOptions(db, getOptions)
if err != nil {
return nil, 0, err
}
Expand Down

0 comments on commit 635ddf7

Please sign in to comment.