From 635ddf7d4d3f9dc6f5b76a3babbc424cd3c8ed6a Mon Sep 17 00:00:00 2001 From: slhmy Date: Thu, 22 Aug 2024 09:23:29 +0800 Subject: [PATCH] Adjust problem model --- cmd/clean/main.go | 4 ++-- cmd/init/problem_package.go | 2 +- go.mod | 6 ++++-- models/problem/problem.go | 13 +++++++------ models/problem/problem_db.go | 20 +++++++------------- models/problem/problem_test.go | 10 +++++----- services/problem/problem_info.go | 6 +++++- 7 files changed, 31 insertions(+), 30 deletions(-) diff --git a/cmd/clean/main.go b/cmd/clean/main.go index ef362ad..49a906c 100644 --- a/cmd/clean/main.go +++ b/cmd/clean/main.go @@ -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", ) diff --git a/cmd/init/problem_package.go b/cmd/init/problem_package.go index 8db48ba..f59a1c1 100644 --- a/cmd/init/problem_package.go +++ b/cmd/init/problem_package.go @@ -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"}, }, }) diff --git a/go.mod b/go.mod index 45c9b00..383e202 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/models/problem/problem.go b/models/problem/problem.go index 6b67dea..42564dc 100644 --- a/models/problem/problem.go +++ b/models/problem/problem.go @@ -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...) diff --git a/models/problem/problem_db.go b/models/problem/problem_db.go index 1161557..0098af9 100644 --- a/models/problem/problem_db.go +++ b/models/problem/problem_db.go @@ -35,7 +35,7 @@ type GetProblemOptions struct { Selection []string Slug *string Title *string - Tags []*AlgorithmTag + Tags []*ProblemTag Offset *int Limit *int } @@ -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) @@ -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 { diff --git a/models/problem/problem_test.go b/models/problem/problem_test.go index c2ee8e7..5e10106 100644 --- a/models/problem/problem_test.go +++ b/models/problem/problem_test.go @@ -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) @@ -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") } diff --git a/services/problem/problem_info.go b/services/problem/problem_info.go index 7876763..2dda459 100644 --- a/services/problem/problem_info.go +++ b/services/problem/problem_info.go @@ -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 }