Skip to content

Commit

Permalink
Merge pull request #3 from Chystik/new_trs_mgr
Browse files Browse the repository at this point in the history
New trx mgr
  • Loading branch information
Chystik authored Nov 15, 2023
2 parents 6a32a60 + c66e95d commit ba5ebf1
Show file tree
Hide file tree
Showing 21 changed files with 1,284 additions and 376 deletions.
9 changes: 7 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@ require (
)

require (
github.com/cockroachdb/apd v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
github.com/avito-tech/go-transaction-manager v1.4.0
github.com/distribution/reference v0.5.0 // indirect
github.com/docker/distribution v2.8.3+incompatible // indirect
github.com/docker/docker v24.0.6+incompatible // indirect
Expand All @@ -32,7 +37,7 @@ require (
github.com/jackc/pgx/v5 v5.4.3
github.com/lib/pq v1.10.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/stretchr/testify v1.8.4
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/crypto v0.9.0
golang.org/x/text v0.9.0 // indirect
Expand Down
334 changes: 7 additions & 327 deletions go.sum

Large diffs are not rendered by default.

30 changes: 22 additions & 8 deletions internal/infrastructure/repository/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,25 @@ import (
"errors"

"github.com/Chystik/gophermart/internal/models"
"github.com/Chystik/gophermart/pkg/logger"
"github.com/Chystik/gophermart/pkg/transaction"

trmsqlx "github.com/avito-tech/go-transaction-manager/sqlx"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jmoiron/sqlx"
"go.uber.org/zap"
)

type orderRepository struct {
*sqlx.DB
getter *trmsqlx.CtxGetter
getter transaction.CtxGetter
logger logger.AppLogger
}

func NewOrderRepository(db *sqlx.DB, getter *trmsqlx.CtxGetter) *orderRepository {
func NewOrderRepository(db *sqlx.DB, getter transaction.CtxGetter, logger logger.AppLogger) *orderRepository {
return &orderRepository{
DB: db,
getter: getter,
logger: logger,
}
}

Expand All @@ -30,7 +34,9 @@ func (or *orderRepository) Create(ctx context.Context, order models.Order) error
VALUES (:number, :user_id, :status, :accrual, :uploaded_at)
ON CONFLICT (number) DO NOTHING`

_, err := sqlx.NamedExecContext(ctx, or.getter.DefaultTrOrDB(ctx, or.DB), query, order)
or.logger.Debug("OrderRepository.Create", zap.String("query", query))

_, err := sqlx.NamedExecContext(ctx, or.getter.GetTrxOrDB(ctx, or.DB), query, order)
if err != nil {
pgErr, ok := err.(*pgconn.PgError)
if !ok {
Expand All @@ -49,7 +55,9 @@ func (or *orderRepository) Get(ctx context.Context, order models.Order) (models.
FROM praktikum.order
WHERE number = $1`

err := sqlx.GetContext(ctx, or.getter.DefaultTrOrDB(ctx, or.DB), &order, query, order.Number)
or.logger.Debug("OrderRepository.Get", zap.String("query", query))

err := sqlx.GetContext(ctx, or.getter.GetTrxOrDB(ctx, or.DB), &order, query, order.Number)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return order, &models.AppError{Op: "orderRepository.Get", Code: models.ErrNotFound, Message: "order not found"}
Expand All @@ -70,7 +78,9 @@ func (or *orderRepository) GetList(ctx context.Context, login models.User) ([]mo
WHERE user_id = :login
ORDER BY uploaded_at ASC`

rows, err := sqlx.NamedQueryContext(ctx, or.getter.DefaultTrOrDB(ctx, or.DB), query, login)
or.logger.Debug("OrderRepository.GetList", zap.String("query", query))

rows, err := sqlx.NamedQueryContext(ctx, or.getter.GetTrxOrDB(ctx, or.DB), query, login)
if err != nil {
return nil, err
}
Expand All @@ -94,7 +104,9 @@ func (or *orderRepository) Update(ctx context.Context, order models.Order) error
SET status = :status, accrual = :accrual
WHERE number = :number`

_, err := sqlx.NamedExecContext(ctx, or.getter.DefaultTrOrDB(ctx, or.DB), query, order)
or.logger.Debug("OrderRepository.Update", zap.String("query", query))

_, err := sqlx.NamedExecContext(ctx, or.getter.GetTrxOrDB(ctx, or.DB), query, order)

return err
}
Expand All @@ -108,7 +120,9 @@ func (or *orderRepository) GetUnprocessed(ctx context.Context) ([]models.Order,
FROM praktikum.order
WHERE status = ANY ($1)`

err := sqlx.SelectContext(ctx, or.getter.DefaultTrOrDB(ctx, or.DB), &orders, query, stat)
or.logger.Debug("OrderRepository.GetUnprocessed", zap.String("query", query))

err := sqlx.SelectContext(ctx, or.getter.GetTrxOrDB(ctx, or.DB), &orders, query, stat)
if err != nil {
return nil, err
}
Expand Down
22 changes: 16 additions & 6 deletions internal/infrastructure/repository/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,25 @@ import (
"errors"

"github.com/Chystik/gophermart/internal/models"
"github.com/Chystik/gophermart/pkg/logger"
"github.com/Chystik/gophermart/pkg/transaction"

trmsqlx "github.com/avito-tech/go-transaction-manager/sqlx"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jmoiron/sqlx"
"go.uber.org/zap"
)

type userRepository struct {
*sqlx.DB
getter *trmsqlx.CtxGetter
getter transaction.CtxGetter
logger logger.AppLogger
}

func NewUserRepository(db *sqlx.DB, getter *trmsqlx.CtxGetter) *userRepository {
func NewUserRepository(db *sqlx.DB, getter transaction.CtxGetter, logger logger.AppLogger) *userRepository {
return &userRepository{
DB: db,
getter: getter,
logger: logger,
}
}

Expand All @@ -29,7 +33,9 @@ func (ur *userRepository) Create(ctx context.Context, user models.User) error {
INSERT INTO praktikum.user (login, password, balance, withdrawn)
VALUES (:login, :password, :balance, :withdrawn)`

_, err := sqlx.NamedExecContext(ctx, ur.getter.DefaultTrOrDB(ctx, ur.DB), query, user)
ur.logger.Debug("UserRepository.Create", zap.String("query", query))

_, err := sqlx.NamedExecContext(ctx, ur.getter.GetTrxOrDB(ctx, ur.DB), query, user)
if err != nil {
pgErr, ok := err.(*pgconn.PgError)
if !ok {
Expand All @@ -49,7 +55,9 @@ func (ur *userRepository) Get(ctx context.Context, user models.User) (models.Use
FROM praktikum.user
WHERE login = :login`

rows, err := sqlx.NamedQueryContext(ctx, ur.getter.DefaultTrOrDB(ctx, ur.DB), query, user)
ur.logger.Debug("UserRepository.Get", zap.String("query", query))

rows, err := sqlx.NamedQueryContext(ctx, ur.getter.GetTrxOrDB(ctx, ur.DB), query, user)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return user, &models.AppError{Op: "userRepository.Get", Code: models.ErrNotFound, Message: "user not found"}
Expand Down Expand Up @@ -78,7 +86,9 @@ func (ur *userRepository) Update(ctx context.Context, user models.User) error {
SET balance = :balance, withdrawn = :withdrawn
WHERE login = :login`

_, err := sqlx.NamedExecContext(ctx, ur.getter.DefaultTrOrDB(ctx, ur.DB), query, user)
ur.logger.Debug("UserRepository.Update", zap.String("query", query))

_, err := sqlx.NamedExecContext(ctx, ur.getter.GetTrxOrDB(ctx, ur.DB), query, user)

return err
}
18 changes: 13 additions & 5 deletions internal/infrastructure/repository/withdrawal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@ import (
"context"

"github.com/Chystik/gophermart/internal/models"
"github.com/Chystik/gophermart/pkg/logger"
"github.com/Chystik/gophermart/pkg/transaction"

trmsqlx "github.com/avito-tech/go-transaction-manager/sqlx"
"github.com/jmoiron/sqlx"
"go.uber.org/zap"
)

type withdrawalRepository struct {
*sqlx.DB
getter *trmsqlx.CtxGetter
getter transaction.CtxGetter
logger logger.AppLogger
}

func NewWithdrawalRepository(db *sqlx.DB, getter *trmsqlx.CtxGetter) *withdrawalRepository {
func NewWithdrawalRepository(db *sqlx.DB, getter transaction.CtxGetter, logger logger.AppLogger) *withdrawalRepository {
return &withdrawalRepository{
DB: db,
getter: getter,
logger: logger,
}
}

Expand All @@ -26,7 +30,9 @@ func (wr *withdrawalRepository) Create(ctx context.Context, w models.Withdrawal)
INSERT INTO praktikum.withdrawal (order_id, sum, processed_at)
VALUES (:order_id, :sum, :processed_at)`

_, err := sqlx.NamedExecContext(ctx, wr.getter.DefaultTrOrDB(ctx, wr.DB), query, w)
wr.logger.Debug("WithdrawalRepository.Create", zap.String("query", query))

_, err := sqlx.NamedExecContext(ctx, wr.getter.GetTrxOrDB(ctx, wr.DB), query, w)
if err != nil {
return err
}
Expand All @@ -42,7 +48,9 @@ func (wr *withdrawalRepository) GetAll(ctx context.Context) ([]models.Withdrawal
FROM praktikum.withdrawal
ORDER BY processed_at ASC`

err := sqlx.SelectContext(ctx, wr.getter.DefaultTrOrDB(ctx, wr.DB), &w, query)
wr.logger.Debug("WithdrawalRepository.GetAll", zap.String("query", query))

err := sqlx.SelectContext(ctx, wr.getter.GetTrxOrDB(ctx, wr.DB), &w, query)
if err != nil {
return nil, err
}
Expand Down
10 changes: 7 additions & 3 deletions internal/middleware/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,18 @@ func (l *midLogger) WithLogging(next http.Handler) http.Handler {
ResponseWriter: w,
responseData: responseData,
}
l.Info(
"request started",
zap.String("uri", r.RequestURI),
zap.String("method", r.Method),
)

next.ServeHTTP(&lw, r)

duration := time.Since(start)

l.Info(
"request started",
zap.String("uri", r.RequestURI),
zap.String("method", r.Method),
"response completed",
zap.Int("status", responseData.status),
zap.Duration("duration", duration),
zap.Int("size", responseData.size),
Expand Down
90 changes: 90 additions & 0 deletions internal/usecase/mocks/accrual_web_api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions internal/usecase/mocks/mocks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package mocks

//go:generate mockery --name=.* --recursive=false --case=underscore --dir ./.. --output . --with-expecter
Loading

0 comments on commit ba5ebf1

Please sign in to comment.