feat: add solutions service

This commit is contained in:
dragonmuffin 2024-08-17 15:43:15 +05:00
parent 0fda91d8d5
commit d9e2f46de8
6 changed files with 80 additions and 5 deletions

View file

@ -13,3 +13,7 @@ var (
var ( var (
ErrBadRole = errors.New("bad role") ErrBadRole = errors.New("bad role")
) )
var (
ErrBadTestingStrategy = errors.New("bad testing strategy")
)

View file

@ -7,7 +7,6 @@ type Solution struct {
ParticipantId *int32 `db:"participant_id"` ParticipantId *int32 `db:"participant_id"`
ProblemId *int32 `db:"problem_id"` ProblemId *int32 `db:"problem_id"`
LanguageId *int32 `db:"language_id"` LanguageId *int32 `db:"language_id"`
ContestId *int32 `db:"contest_id"`
SolutionHash *string `db:"solution_hash"` SolutionHash *string `db:"solution_hash"`
Result *int32 `db:"result"` Result *int32 `db:"result"`
CreatedAt *time.Time `db:"created_at"` CreatedAt *time.Time `db:"created_at"`

View file

@ -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
}

View file

@ -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)
}

View file

@ -122,10 +122,8 @@ CREATE TABLE IF NOT EXISTS solutions
( (
id serial NOT NULL, id serial NOT NULL,
participant_id INT REFERENCES participants ON DELETE CASCADE, participant_id INT REFERENCES participants ON DELETE CASCADE,
--problem_id INT REFERENCES problems ON DELETE CASCADE,
task_id INT REFERENCES problems ON DELETE CASCADE, task_id INT REFERENCES problems ON DELETE CASCADE,
language_id INT REFERENCES languages ON DELETE CASCADE, language_id INT REFERENCES languages ON DELETE CASCADE,
contest_id INT NOT NULL,
solution_hash CHAR(128) NOT NULL, solution_hash CHAR(128) NOT NULL,
result INT NOT NULL, result INT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), 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);
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);

2
proto

@ -1 +1 @@
Subproject commit f25fe458181ecba892584a800db311db3ad272d9 Subproject commit 71ab507e02cb6b9c0e3186c19848a2ec900d6384