From a5e464d4a797f1d975194a9bde4e532acad9ee8c Mon Sep 17 00:00:00 2001 From: dragonmuffin Date: Sun, 18 Aug 2024 18:41:08 +0500 Subject: [PATCH] feat: test add --- internal/models/testgroup.go | 11 ++++++++--- internal/storage/problem.go | 37 +++++++++++++++++++++++++++++------- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/internal/models/testgroup.go b/internal/models/testgroup.go index 81aa98e..ae85ec3 100644 --- a/internal/models/testgroup.go +++ b/internal/models/testgroup.go @@ -10,9 +10,14 @@ const ( ) type Testgroup struct { - Id *int32 `db:"id"` - ProblemId *int32 `db:"problem_id"` - TestingStrategy *int32 `db:"testing_strategy"` + Id *int32 `db:"id"` + ProblemId *int32 `db:"problem_id"` + TestingStrategy *TestingStrategy `db:"testing_strategy"` +} + +type TestgroupData struct { + Ts TestingStrategy + TestAmount int32 } func (TestingStrategy ts) Valid() error { diff --git a/internal/storage/problem.go b/internal/storage/problem.go index ba32b61..3249ea5 100644 --- a/internal/storage/problem.go +++ b/internal/storage/problem.go @@ -24,15 +24,15 @@ func NewProblemStorage(db *sqlx.DB, logger *zap.Logger) *ProblemStorage { } } -func (storage *ProblemStorage) CreateProblem(ctx context.Context, problem *models.Problem) (int32, error) { - query := storage.db.Rebind(` +func (storage *ProblemStorage) CreateProblem(ctx context.Context, problem *models.Problem, testgroupData []models.TestGroupData, testchan <-chan []byte) (int32, error) { + tx,err := storage.db.Begin() + query := tx.Rebind(` INSERT INTO problems (name,description,time_limit,memory_limit) VALUES (?, ?, ?, ?) RETURNING id `) - - rows, err := storage.db.QueryxContext( + rows, err := tx.QueryxContext( ctx, query, problem.Name, @@ -40,9 +40,32 @@ RETURNING id problem.TimeLimit, problem.MemoryLimit ) - if err != nil { - return 0, storage.HandlePgErr(err) - } + for _,tgd := testGroupData { + query := tx.Rebind(` + INSERT INTO testgroups + (problem_id,testing_strategy) + VALUES ((select last_value from problems_id_seq),?) + RETURNING id + `) + rows, err := tx.QueryxContext(ctx, query, tgd.Ts) + if err != nil { + return 0, storage.HandlePgErr(err) + } + for i:=0;i