2024-10-09 18:55:16 +00:00
|
|
|
package repository
|
2024-08-18 13:01:26 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-08-22 22:56:03 +00:00
|
|
|
"errors"
|
2024-10-05 16:05:29 +00:00
|
|
|
"git.sch9.ru/new_gate/models"
|
2024-10-09 18:55:16 +00:00
|
|
|
"git.sch9.ru/new_gate/ms-tester/pkg/utils"
|
|
|
|
"github.com/jackc/pgerrcode"
|
|
|
|
"github.com/jackc/pgx/v5/pgconn"
|
2024-08-18 13:01:26 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2024-10-09 18:55:16 +00:00
|
|
|
type ProblemRepository struct {
|
2024-08-18 13:01:26 +00:00
|
|
|
db *sqlx.DB
|
|
|
|
logger *zap.Logger
|
|
|
|
}
|
|
|
|
|
2024-10-09 18:55:16 +00:00
|
|
|
func NewProblemRepository(db *sqlx.DB, logger *zap.Logger) *ProblemRepository {
|
|
|
|
return &ProblemRepository{
|
2024-08-18 13:01:26 +00:00
|
|
|
db: db,
|
|
|
|
logger: logger,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-09 18:55:16 +00:00
|
|
|
func (storage *ProblemRepository) CreateProblem(ctx context.Context, problem *models.Problem, testGroupData []models.TestGroupData) (int32, error) {
|
2024-08-20 22:54:46 +00:00
|
|
|
tx, err := storage.db.Beginx()
|
2024-08-22 22:56:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, handlePgErr(err)
|
|
|
|
}
|
2024-08-18 13:41:08 +00:00
|
|
|
query := tx.Rebind(`
|
2024-08-18 13:01:26 +00:00
|
|
|
INSERT INTO problems
|
|
|
|
(name,description,time_limit,memory_limit)
|
|
|
|
VALUES (?, ?, ?, ?)
|
|
|
|
RETURNING id
|
|
|
|
`)
|
2024-08-18 13:41:08 +00:00
|
|
|
rows, err := tx.QueryxContext(
|
2024-08-18 13:01:26 +00:00
|
|
|
ctx,
|
|
|
|
query,
|
2024-08-20 22:54:46 +00:00
|
|
|
problem.Name,
|
|
|
|
problem.Description,
|
|
|
|
problem.TimeLimit,
|
|
|
|
problem.MemoryLimit,
|
2024-08-18 13:01:26 +00:00
|
|
|
)
|
2024-08-18 13:51:28 +00:00
|
|
|
if err != nil {
|
2024-08-22 22:56:03 +00:00
|
|
|
return 0, handlePgErr(errors.Join(err, tx.Rollback()))
|
2024-08-18 13:51:28 +00:00
|
|
|
}
|
2024-08-20 22:54:46 +00:00
|
|
|
for _, tgd := range testGroupData {
|
|
|
|
query := tx.Rebind(`
|
2024-08-18 13:41:08 +00:00
|
|
|
INSERT INTO testgroups
|
|
|
|
(problem_id,testing_strategy)
|
|
|
|
VALUES ((select last_value from problems_id_seq),?)
|
|
|
|
RETURNING id
|
|
|
|
`)
|
2024-08-20 22:54:46 +00:00
|
|
|
rows, err = tx.QueryxContext(ctx, query, tgd.Ts)
|
|
|
|
if err != nil {
|
2024-08-22 22:56:03 +00:00
|
|
|
return 0, handlePgErr(errors.Join(err, tx.Rollback()))
|
2024-08-20 22:54:46 +00:00
|
|
|
}
|
|
|
|
var i int32 = 0
|
|
|
|
for ; i < tgd.TestAmount; i++ {
|
|
|
|
query := tx.Rebind(`
|
2024-08-18 13:41:08 +00:00
|
|
|
INSERT INTO tests
|
|
|
|
(testgroup_id)
|
|
|
|
VALUES ((select last_value from testgroups_id_seq))
|
|
|
|
RETURNING id
|
|
|
|
`)
|
2024-08-20 22:54:46 +00:00
|
|
|
rows, err = tx.QueryxContext(ctx, query, tgd.Ts)
|
|
|
|
if err != nil {
|
2024-08-22 22:56:03 +00:00
|
|
|
return 0, handlePgErr(errors.Join(err, tx.Rollback()))
|
2024-08-20 22:54:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = tx.Commit()
|
|
|
|
//add test saving
|
2024-08-18 13:01:26 +00:00
|
|
|
|
|
|
|
defer rows.Close()
|
|
|
|
var id int32
|
|
|
|
err = rows.StructScan(&id)
|
|
|
|
if err != nil {
|
2024-08-20 22:54:46 +00:00
|
|
|
return 0, handlePgErr(err)
|
2024-08-18 13:01:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
2024-10-09 18:55:16 +00:00
|
|
|
func (storage *ProblemRepository) ReadProblemById(ctx context.Context, id int32) (*models.Problem, error) {
|
2024-08-18 13:01:26 +00:00
|
|
|
var problem models.Problem
|
|
|
|
query := storage.db.Rebind("SELECT * from problems WHERE id=? LIMIT 1")
|
|
|
|
err := storage.db.GetContext(ctx, &problem, query, id)
|
|
|
|
if err != nil {
|
2024-08-20 22:54:46 +00:00
|
|
|
return nil, handlePgErr(err)
|
2024-08-18 13:01:26 +00:00
|
|
|
}
|
|
|
|
return &problem, nil
|
|
|
|
}
|
|
|
|
|
2024-10-09 18:55:16 +00:00
|
|
|
func (storage *ProblemRepository) UpdateProblem(ctx context.Context, problem *models.Problem) error {
|
2024-08-20 22:54:46 +00:00
|
|
|
query := storage.db.Rebind("UPDATE problems SET name=?,description=?,time_limit=?,memory_limit=? WHERE id=?")
|
|
|
|
_, err := storage.db.ExecContext(ctx, query, problem.Name, problem.Description, problem.TimeLimit, problem.MemoryLimit, problem.Id)
|
2024-08-18 13:01:26 +00:00
|
|
|
if err != nil {
|
2024-08-20 22:54:46 +00:00
|
|
|
return handlePgErr(err)
|
2024-08-18 13:01:26 +00:00
|
|
|
}
|
2024-08-20 22:54:46 +00:00
|
|
|
return nil
|
2024-08-18 13:01:26 +00:00
|
|
|
}
|
|
|
|
|
2024-10-09 18:55:16 +00:00
|
|
|
func (storage *ProblemRepository) DeleteProblem(ctx context.Context, id int32) error {
|
2024-08-18 13:01:26 +00:00
|
|
|
query := storage.db.Rebind("DELETE FROM problems WHERE id=?")
|
|
|
|
_, err := storage.db.ExecContext(ctx, query, id)
|
|
|
|
if err != nil {
|
2024-08-20 22:54:46 +00:00
|
|
|
return handlePgErr(err)
|
2024-08-18 13:01:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-10-09 18:55:16 +00:00
|
|
|
|
|
|
|
func handlePgErr(err error) error {
|
|
|
|
var pgErr *pgconn.PgError
|
|
|
|
if !errors.As(err, &pgErr) {
|
|
|
|
return utils.StorageError(err, utils.ErrUnknown, "unexpected error from postgres")
|
|
|
|
}
|
|
|
|
if pgerrcode.IsIntegrityConstraintViolation(pgErr.Code) {
|
|
|
|
// TODO: probably should specify which constraint
|
|
|
|
return utils.StorageError(err, utils.ErrConflict, pgErr.Message)
|
|
|
|
}
|
|
|
|
if pgerrcode.IsNoData(pgErr.Code) {
|
|
|
|
return utils.StorageError(err, utils.ErrNotFound, pgErr.Message)
|
|
|
|
}
|
|
|
|
return utils.StorageError(err, utils.ErrUnimplemented, "unimplemented error")
|
|
|
|
}
|