From d9e2f46de8518bc45056330895e88d29bdc670fa Mon Sep 17 00:00:00 2001 From: dragonmuffin Date: Sat, 17 Aug 2024 15:43:15 +0500 Subject: [PATCH] feat: add solutions service --- internal/lib/errors.go | 4 +++ internal/models/solution.go | 1 - internal/models/testgroup.go | 24 +++++++++++++ internal/services/solution.go | 50 +++++++++++++++++++++++++++ migrations/20240727123308_initial.sql | 4 +-- proto | 2 +- 6 files changed, 80 insertions(+), 5 deletions(-) create mode 100644 internal/models/testgroup.go create mode 100644 internal/services/solution.go diff --git a/internal/lib/errors.go b/internal/lib/errors.go index fd5c2a9..86e6f79 100644 --- a/internal/lib/errors.go +++ b/internal/lib/errors.go @@ -13,3 +13,7 @@ var ( var ( ErrBadRole = errors.New("bad role") ) + +var ( + ErrBadTestingStrategy = errors.New("bad testing strategy") +) diff --git a/internal/models/solution.go b/internal/models/solution.go index 8fc4cdd..aceae6c 100644 --- a/internal/models/solution.go +++ b/internal/models/solution.go @@ -7,7 +7,6 @@ type Solution struct { ParticipantId *int32 `db:"participant_id"` ProblemId *int32 `db:"problem_id"` LanguageId *int32 `db:"language_id"` - ContestId *int32 `db:"contest_id"` SolutionHash *string `db:"solution_hash"` Result *int32 `db:"result"` CreatedAt *time.Time `db:"created_at"` diff --git a/internal/models/testgroup.go b/internal/models/testgroup.go new file mode 100644 index 0000000..81aa98e --- /dev/null +++ b/internal/models/testgroup.go @@ -0,0 +1,24 @@ +package models + +import "time" + +type TestingStrategy int32 + +const ( + EachTestTS TestingStrategy = 1; + CompleteGroupTS TestingStrategy = 2; +) + +type Testgroup struct { + Id *int32 `db:"id"` + ProblemId *int32 `db:"problem_id"` + TestingStrategy *int32 `db:"testing_strategy"` +} + +func (TestingStrategy ts) Valid() error { + switch ts { + case EachTestTS, CompleteGroupTS: + return nil + } + return lib.ErrBadTestingStrategy +} diff --git a/internal/services/solution.go b/internal/services/solution.go new file mode 100644 index 0000000..a95e3df --- /dev/null +++ b/internal/services/solution.go @@ -0,0 +1,50 @@ +package services + +import ( + "context" + "git.sch9.ru/new_gate/ms-tester/internal/models" +) + +type SolutionStorage interface { + CreateSolution(ctx context.Context, models.Solution) (int32, error) + ReadSolutionById(ctx context.Context, id int32) (models.Solution, error) + RejudgeSolution(ctx context.Context, id int32) error + DeleteSolution(ctx context.Context, id int32) error +} + +type SolutionService struct { + solutionStorage SolutionStorage +} + +func NewSolutionService( + solutionStorage SolutionStorage, +) *SolutionService { + return &SolutionService{ + solutionStorage: solutionStorage, + } +} + +func (service *SolutionService) CreateSolution(ctx context.Context, solution models.Solution) (int32, error) { + userId := ctx.Value("user_id").(int32) + panic("access control is not implemented yet") + return service.solutionStorage.CreateSolution(ctx, solution) +} + +func (service *SolutionService) ReadSolutionById(ctx context.Context, id int32) (models.Solution, error) { + userId := ctx.Value("user_id").(int32) + panic("access control is not implemented yet") + return service.solutionStorage.ReadSolutionById(ctx, id) +} + +func (service *SolutionService) RejudgeSolution(ctx context.Context, id int32) error { + userId := ctx.Value("user_id").(int32) + panic("access control is not implemented yet") + return service.solutionStorage.RejudgeSolution(ctx, id) +} + +func (service *SolutionService) DeleteSolution(ctx context.Context, id int32) error { + userId := ctx.Value("user_id").(int32) + panic("access control is not implemented yet") + return service.solutionStorage.DeleteSolution(ctx, id) +} + diff --git a/migrations/20240727123308_initial.sql b/migrations/20240727123308_initial.sql index 13a3246..64139e8 100644 --- a/migrations/20240727123308_initial.sql +++ b/migrations/20240727123308_initial.sql @@ -122,10 +122,8 @@ CREATE TABLE IF NOT EXISTS solutions ( id serial NOT NULL, participant_id INT REFERENCES participants ON DELETE CASCADE, - --problem_id INT REFERENCES problems ON DELETE CASCADE, task_id INT REFERENCES problems ON DELETE CASCADE, language_id INT REFERENCES languages ON DELETE CASCADE, - contest_id INT NOT NULL, solution_hash CHAR(128) NOT NULL, result INT NOT NULL, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), @@ -134,7 +132,7 @@ CREATE TABLE IF NOT EXISTS solutions ); CREATE INDEX ON solutions USING BTREE (id); -CREATE INDEX ON solutions USING BTREE (id,participant_id,task_id,language_id,contest_id); +CREATE INDEX ON solutions USING BTREE (id,participant_id,task_id,language_id); diff --git a/proto b/proto index f25fe45..71ab507 160000 --- a/proto +++ b/proto @@ -1 +1 @@ -Subproject commit f25fe458181ecba892584a800db311db3ad272d9 +Subproject commit 71ab507e02cb6b9c0e3186c19848a2ec900d6384