package storage import ( "context" "errors" "git.sch9.ru/new_gate/ms-auth/internal/lib" "git.sch9.ru/new_gate/ms-auth/internal/models" "github.com/jackc/pgerrcode" "github.com/jackc/pgx/v5/pgconn" "github.com/jmoiron/sqlx" "go.uber.org/zap" "time" ) type SolutionStorage struct { db *sqlx.DB logger *zap.Logger } func NewSolutionStorage(db *sqlx.DB, logger *zap.Logger) *SolutionStorage { return &SolutionStorage{ db: db, logger: logger, } } //func updateResult(ctx context.Context, participant_id int32, task_id int32) error { // //} func (storage *SolutionStorage) CreateSolution(ctx context.Context, solution *models.Solution) (int32, error) { query := storage.db.Rebind(` INSERT INTO solutions (participant_id,task_id,language_id,solution_hash,result) VALUES (?, ?, ?, ?, ?) RETURNING id `) rows, err := storage.db.QueryxContext( ctx, query, solution.ParticipantId, solution.TaskId, solution.LanguageId, "",//FIXME models.NotTested, ) if err != nil { return 0, storage.HandlePgErr(err) } defer rows.Close() var id int32 err = rows.StructScan(&id) if err != nil { return 0, storage.HandlePgErr(err) } return id, nil } func (storage *SolutionStorage) ReadSolutionById(ctx context.Context, id int32) (*models.Solution, error) { var solution models.Solution query := storage.db.Rebind("SELECT * from solutions WHERE id=? LIMIT 1") err := storage.db.GetContext(ctx, &solution, query, id) if err != nil { return nil, storage.HandlePgErr(err) } return &solution, nil } func (storage *SolutionStorage) RejudgeSolution(ctx context.Context, id int32) error { tx,err := storage.db.Begin() if err != nil { return storage.HandlePgErr(err) } tx.Exec("UPDATE solutions SET result = ? WHERE id = ?",models.NotTested, id) tx.Exec("UPDATE testgroupruns SET result = ? WHERE solution_id = ?",models.NotTested, id) tx.Exec("UPDATE testruns SET result = ? WHERE testgrouprun_id IN (SELECT id FROM tesgrouprun WHERE solution_id = ?)", models.NotTested, id) err = tx.Commit() if err != nil { return storage.HandlePgErr(err) } //FIXME: update result return nil } func (storage *SolutionStorage) DeleteSolution(ctx context.Context, id int32) error { query := storage.db.Rebind("DELETE FROM solutions WHERE id=?") _, err := storage.db.ExecContext(ctx, query, id) if err != nil { return storage.HandlePgErr(err) } return nil }