Skip to content

Commit 88abc0c

Browse files
committedFeb 13, 2024
feat(pkg): add db transaction
1 parent 9fbe61a commit 88abc0c

File tree

7 files changed

+36
-7
lines changed

7 files changed

+36
-7
lines changed
 

‎app/users/repository/get_users.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func (repo *repository) GetUsers(ctx context.Context) (users []domain.User, err error) {
11-
err = repo.db.Find(&users).Error
11+
err = repo.db.DB(ctx).Find(&users).Error
1212
if err != nil {
1313
logrus.Error("repo.GetUsers: failed to get users")
1414
return

‎app/users/repository/repository.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ package repository
22

33
import (
44
"github.com/hammer-code/lms-be/app/users"
5-
"gorm.io/gorm"
5+
pkgDB "github.com/hammer-code/lms-be/pkg/db"
66
)
77

88
type (
99
repository struct {
10-
db *gorm.DB
10+
db pkgDB.DatabaseTransaction
1111
}
1212
)
1313

14-
func NewRepository(db *gorm.DB) users.UserRepository {
14+
func NewRepository(db pkgDB.DatabaseTransaction) users.UserRepository {
1515
return &repository{
1616
db,
1717
}

‎cmd/serve_http.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
users_usecase "github.com/hammer-code/lms-be/app/users/usecase"
1515
"github.com/hammer-code/lms-be/config"
1616
"github.com/hammer-code/lms-be/domain"
17+
pkgDB "github.com/hammer-code/lms-be/pkg/db"
1718
"github.com/hammer-code/lms-be/utils"
1819
"github.com/sirupsen/logrus"
1920
"github.com/spf13/cobra"
@@ -39,11 +40,13 @@ var serveHttpCmd = &cobra.Command{
3940
DSN: cfg.DB_POSTGRES_DSN,
4041
}})
4142

43+
dbTx := pkgDB.NewDBTransaction(db)
44+
4245
// repository
43-
userRepo := users_repo.NewRepository(db)
46+
userRepo := users_repo.NewRepository(dbTx)
4447

4548
// usecase
46-
userUsecase := users_usecase.NewUsecase(userRepo)
49+
userUsecase := users_usecase.NewUsecase(userRepo, dbTx)
4750

4851
// handler
4952
userHandler := users_handler.NewHandler(userUsecase)

‎domain/context.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package domain
2+
3+
const (
4+
ContextDatabaseTransaction = "database-transaction"
5+
)

‎pkg/db/db.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type (
1313

1414
DatabaseTransaction interface {
1515
StartTransaction(ctx context.Context, fn func(txCtx context.Context) error) error
16+
DB(ctx context.Context) *gorm.DB
1617
}
1718
)
1819

‎pkg/db/get_database_transaction.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package db
2+
3+
import (
4+
"context"
5+
6+
"github.com/hammer-code/lms-be/domain"
7+
"gorm.io/gorm"
8+
)
9+
10+
// get db transaction or repo db(without transaction)
11+
func (d *dbTX) DB(ctx context.Context) *gorm.DB {
12+
tx, ok := ctx.Value(domain.ContextDatabaseTransaction).(*gorm.DB)
13+
if !ok {
14+
return d.db
15+
}
16+
return tx
17+
}

‎pkg/db/start_transaction.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package db
33
import (
44
"context"
55

6+
"github.com/hammer-code/lms-be/domain"
67
"github.com/sirupsen/logrus"
78
)
89

@@ -11,7 +12,9 @@ func (d *dbTX) StartTransaction(ctx context.Context, fn func(txCtx context.Conte
1112

1213
defer tx.Rollback()
1314

14-
if err := fn(ctx); err != nil {
15+
txCtx := context.WithValue(ctx, domain.ContextDatabaseTransaction, tx)
16+
17+
if err := fn(txCtx); err != nil {
1518
logrus.Error("transaction database return err: ", err)
1619
return err
1720
}

0 commit comments

Comments
 (0)