ms-tester/internal/contests/repository/pg_repository.go

85 lines
2.1 KiB
Go
Raw Normal View History

2024-10-09 18:55:16 +00:00
package repository
import (
"context"
"errors"
2024-10-13 14:01:36 +00:00
"git.sch9.ru/new_gate/ms-tester/internal/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"
"github.com/jmoiron/sqlx"
"go.uber.org/zap"
)
type ContestRepository struct {
db *sqlx.DB
logger *zap.Logger
}
func NewContestRepository(db *sqlx.DB, logger *zap.Logger) *ContestRepository {
return &ContestRepository{
db: db,
logger: logger,
}
}
2024-10-13 14:01:36 +00:00
const createContestQuery = "INSERT INTO contest (title) VALUES (?) RETURNING id"
2024-10-09 18:55:16 +00:00
2024-10-13 14:01:36 +00:00
func (r *ContestRepository) CreateContest(ctx context.Context, title string) (int32, error) {
query := r.db.Rebind(createContestQuery)
rows, err := r.db.QueryxContext(ctx, query, title)
2024-10-09 18:55:16 +00:00
if err != nil {
return 0, handlePgErr(err)
}
defer rows.Close()
var id int32
2024-10-13 14:01:36 +00:00
rows.Next()
err = rows.Scan(&id)
2024-10-09 18:55:16 +00:00
if err != nil {
return 0, handlePgErr(err)
}
return id, nil
}
2024-10-13 14:01:36 +00:00
const readContestByIdQuery = "SELECT * from contest WHERE id=? LIMIT 1"
2024-10-09 18:55:16 +00:00
func (r *ContestRepository) ReadContestById(ctx context.Context, id int32) (*models.Contest, error) {
var contest models.Contest
2024-10-13 14:01:36 +00:00
query := r.db.Rebind(readContestByIdQuery)
2024-10-09 18:55:16 +00:00
err := r.db.GetContext(ctx, &contest, query, id)
if err != nil {
return nil, handlePgErr(err)
}
return &contest, nil
}
2024-10-13 14:01:36 +00:00
const deleteContestQuery = "DELETE FROM contest WHERE id=?"
2024-10-09 18:55:16 +00:00
func (r *ContestRepository) DeleteContest(ctx context.Context, id int32) error {
2024-10-13 14:01:36 +00:00
query := r.db.Rebind(deleteContestQuery)
2024-10-09 18:55:16 +00:00
_, err := r.db.ExecContext(ctx, query, id)
if err != nil {
return handlePgErr(err)
}
return nil
}
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")
}