package storage import ( "context" "git.sch9.ru/new_gate/ms-tester/internal/models" "github.com/jmoiron/sqlx" "go.uber.org/zap" ) type ContestStorage struct { db *sqlx.DB logger *zap.Logger } func NewContestStorage(db *sqlx.DB, logger *zap.Logger) *ContestStorage { return &ContestStorage{ db: db, logger: logger, } } func (storage *ContestStorage) CreateContest(ctx context.Context, contest *models.Contest) (int32, error) { query := storage.db.Rebind(` INSERT INTO contests (name) VALUES (?) RETURNING id `) rows, err := storage.db.QueryxContext( ctx, query, contest.Name, ) if err != nil { return 0, handlePgErr(err) } defer rows.Close() var id int32 err = rows.StructScan(&id) if err != nil { return 0, handlePgErr(err) } return id, nil } func (storage *ContestStorage) ReadContestById(ctx context.Context, id int32) (*models.Contest, error) { var contest models.Contest query := storage.db.Rebind("SELECT * from contests WHERE id=? LIMIT 1") err := storage.db.GetContext(ctx, &contest, query, id) if err != nil { return nil, handlePgErr(err) } return &contest, nil } func (storage *ContestStorage) UpdateContest(ctx context.Context, contest *models.Contest) error { query := storage.db.Rebind("UPDATE contests SET name=? WHERE id=?") _, err := storage.db.ExecContext(ctx, query, contest.Name, contest.Id) if err != nil { return handlePgErr(err) } return nil } func (storage *ContestStorage) DeleteContest(ctx context.Context, id int32) error { query := storage.db.Rebind("DELETE FROM contests WHERE id=?") _, err := storage.db.ExecContext(ctx, query, id) if err != nil { return handlePgErr(err) } return nil }