Skip to content

Commit

Permalink
[fix] scaffold
Browse files Browse the repository at this point in the history
  • Loading branch information
igtm committed Jan 15, 2022
1 parent 516cdc0 commit 280b140
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions example/go-ddd-api/SCAFFOLD.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,26 @@ func New{{$Name}}Repository({{$name}}DB *sqlx.DB) domain{{$Name}}.Repository {
}
}

// WithTransaction
func (repo *{{$name}}Repository) WithDBTransaction(ctx context.Context, txFunc func(*sqlx.Tx) error) (err error) {
tx, err := repo.{{$name}}DB.BeginTxx(ctx, nil)
if err != nil {
return
}
defer func() {
if p := recover(); p != nil {
_ = tx.Rollback()
panic(p) // re-throw panic after Rollback
} else if err != nil {
_ = tx.Rollback()
} else {
err = tx.Commit()
}
}()
err = txFunc(tx)
return err
}

// Get{{$Name}} ...
func (repo *{{$name}}Repository) Get{{$Name}}(ctx context.Context, id uint64) (*{{$name}}.V1{{$Name}}, error) {
query := sq.Select(`*`).
Expand Down

0 comments on commit 280b140

Please sign in to comment.