-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add problem mapper * Add problem option search test * Move service code * Problem router & service
- Loading branch information
Showing
8 changed files
with
211 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,103 @@ | ||
package mapper | ||
|
||
import ( | ||
"github.com/OJ-lab/oj-lab-services/packages/application" | ||
"github.com/OJ-lab/oj-lab-services/packages/model" | ||
) | ||
|
||
func CreateProblem(problem model.Problem) error { | ||
db := application.GetDefaultDB() | ||
return db.Create(&problem).Error | ||
} | ||
|
||
func GetProblem(slug string) (model.Problem, error) { | ||
db := application.GetDefaultDB() | ||
db_problem := model.Problem{} | ||
err := db.Model(&model.Problem{}).Preload("Tags").Where("Slug = ?", slug).First(&db_problem).Error | ||
return db_problem, err | ||
} | ||
|
||
func DeleteProblem(problem model.Problem) error { | ||
db := application.GetDefaultDB() | ||
return db.Delete(&model.Problem{Slug: problem.Slug}).Error | ||
} | ||
|
||
func UpdateProblem(problem model.Problem) error { | ||
db := application.GetDefaultDB() | ||
return db.Model(&model.Problem{Slug: problem.Slug}).Updates(problem).Error | ||
} | ||
|
||
type GetProblemOptions struct { | ||
Slug string | ||
Title string | ||
Tags []*model.AlgorithmTag | ||
Offset *int | ||
Limit *int | ||
} | ||
|
||
func CountProblemByOptions(options GetProblemOptions) (int64, error) { | ||
db := application.GetDefaultDB() | ||
var count int64 | ||
|
||
tagsList := []string{} | ||
for _, tag := range options.Tags { | ||
tagsList = append(tagsList, tag.Slug) | ||
} | ||
|
||
tx := db. | ||
Model(&model.Problem{}). | ||
Joins("JOIN problem_algorithm_tags ON problem_algorithm_tags.problem_slug = problems.slug"). | ||
Where("problem_algorithm_tags.algorithm_tag_slug in ?", tagsList). | ||
Or("Slug = ?", options.Slug). | ||
Or("Title = ?", options.Title). | ||
Distinct(). | ||
Preload("Tags") | ||
|
||
err := tx.Count(&count).Error | ||
|
||
return count, err | ||
} | ||
|
||
func GetProblemByOptions(options GetProblemOptions) ([]model.Problem, int64, error) { | ||
total, err := CountProblemByOptions(options) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
|
||
db := application.GetDefaultDB() | ||
db_problems := []model.Problem{} | ||
tagsList := []string{} | ||
for _, tag := range options.Tags { | ||
tagsList = append(tagsList, tag.Slug) | ||
} | ||
tx := db. | ||
Model(&model.Problem{}). | ||
Joins("JOIN problem_algorithm_tags ON problem_algorithm_tags.problem_slug = problems.slug"). | ||
Where("problem_algorithm_tags.algorithm_tag_slug in ?", tagsList). | ||
Or("Slug = ?", options.Slug). | ||
Or("Title = ?", options.Title). | ||
Distinct(). | ||
Preload("Tags") | ||
|
||
if options.Offset != nil { | ||
tx = tx.Offset(*options.Offset) | ||
} | ||
if options.Limit != nil { | ||
tx = tx.Limit(*options.Limit) | ||
} | ||
|
||
err = tx.Find(&db_problems).Error | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
|
||
return db_problems, total, nil | ||
} | ||
|
||
func GetTagsList(problem model.Problem) []string { | ||
tagsList := []string{} | ||
for _, tag := range problem.Tags { | ||
tagsList = append(tagsList, tag.Slug) | ||
} | ||
return tagsList | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package mapper | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/OJ-lab/oj-lab-services/packages/model" | ||
) | ||
|
||
func TestProblemMapper(t *testing.T) { | ||
problem := model.Problem{ | ||
Slug: "a+b-problem", | ||
Title: "A+B Problem", | ||
Description: "Given two integer A and B, please output the answer of A+B.", | ||
Tags: []*model.AlgorithmTag{{Slug: "tag1"}, {Slug: "tag2"}}, | ||
} | ||
err := CreateProblem(problem) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
dbProblem, err := GetProblem(problem.Slug) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
problemJson, err := json.MarshalIndent(dbProblem, "", "\t") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
fmt.Printf("%+v\n", string(problemJson)) | ||
|
||
problemOption := GetProblemOptions{ | ||
Slug: "", | ||
Title: "", | ||
Tags: []*model.AlgorithmTag{{Slug: "tag1"}}, | ||
} | ||
|
||
dbProblems, problemCount, err := GetProblemByOptions(problemOption) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
fmt.Printf("problemCount: %d\n", problemCount) | ||
if problemCount != 1 { | ||
t.Error("problemCount should be 1") | ||
} | ||
|
||
problemsJson, err := json.MarshalIndent(dbProblems, "", "\t") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
fmt.Printf("%+v\n", string(problemsJson)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package problem | ||
|
||
import ( | ||
"github.com/OJ-lab/oj-lab-services/packages/mapper" | ||
"github.com/OJ-lab/oj-lab-services/packages/utils" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func GetProblemInfo(c *gin.Context) { | ||
slug := c.Param("slug") | ||
problem, err := mapper.GetProblem(slug) | ||
if err != nil { | ||
utils.ApplyError(c, err) | ||
return | ||
} | ||
|
||
c.JSON(200, gin.H{ | ||
"slug": problem.Slug, | ||
"title": problem.Title, | ||
"description": problem.Description, | ||
"tags": mapper.GetTagsList(problem), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package router | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/OJ-lab/oj-lab-services/service/problem" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func SetupUserRouter(r *gin.Engine) { | ||
g := r.Group("/api/user") | ||
{ | ||
g.GET("/health", func(c *gin.Context) { | ||
c.String(http.StatusOK, "Hello, this is user service") | ||
}) | ||
} | ||
} | ||
|
||
func SetupProblemRoute(r *gin.Engine) { | ||
g := r.Group("/api/v1/problem") | ||
{ | ||
g.GET("/health", func(c *gin.Context) { | ||
c.String(http.StatusOK, "Hello, this is problem service") | ||
}) | ||
g.GET("/:slug", problem.GetProblemInfo) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.