Skip to content

Commit

Permalink
Merge pull request #94 from bmf-san/fix/tx-rollback
Browse files Browse the repository at this point in the history
Fix transaction rollback
  • Loading branch information
bmf-san authored Jun 19, 2023
2 parents 2d5ffe3 + c85e0b5 commit 634a379
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 7 deletions.
7 changes: 0 additions & 7 deletions app/interfaces/controller/auth_test.go

This file was deleted.

2 changes: 2 additions & 0 deletions app/interfaces/repository/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ func (cr *Category) Save(req request.StoreCategory) (int, error) {
(?, ?, ?)
`, req.Name, now, now)
if err != nil {
_ = tx.Rollback()
return 0, err
}

Expand Down Expand Up @@ -253,6 +254,7 @@ func (cr *Category) DeleteByID(id int) (int, error) {
var count int
err = row.Scan(&count)
if err != nil {
_ = tx.Rollback()
return 0, nil
}

Expand Down
2 changes: 2 additions & 0 deletions app/interfaces/repository/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ func (cr *Comment) FindByID(id int) (domain.Comment, error) {
func (cr *Comment) Save(req request.StoreComment) (int, error) {
tx, err := cr.ConnMySQL.Begin()
if err != nil {
_ = tx.Rollback()
return 0, err
}

Expand Down Expand Up @@ -178,6 +179,7 @@ func (cr *Comment) Save(req request.StoreComment) (int, error) {
func (cr *Comment) SaveStatusByID(req request.UpdateCommentStatus) error {
tx, err := cr.ConnMySQL.Begin()
if err != nil {
_ = tx.Rollback()
return err
}

Expand Down
6 changes: 6 additions & 0 deletions app/interfaces/repository/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -1706,6 +1706,7 @@ func (pr *Post) FindByID(id int) (domain.Post, error) {
func (pr *Post) Save(req request.StorePost) (int, error) {
tx, err := pr.ConnMySQL.Begin()
if err != nil {
_ = tx.Rollback()
return 0, err
}

Expand Down Expand Up @@ -1750,6 +1751,7 @@ func (pr *Post) Save(req request.StorePost) (int, error) {

_, err = tx.Exec(stmt, vArgs...)
if err != nil {
_ = tx.Rollback()
return 0, err
}

Expand All @@ -1764,6 +1766,7 @@ func (pr *Post) Save(req request.StorePost) (int, error) {
func (pr *Post) SaveByID(req request.UpdatePost) error {
tx, err := pr.ConnMySQL.Begin()
if err != nil {
_ = tx.Rollback()
return err
}

Expand Down Expand Up @@ -1816,6 +1819,7 @@ func (pr *Post) SaveByID(req request.UpdatePost) error {

_, err = tx.Exec(stmt, vArgs...)
if err != nil {
_ = tx.Rollback()
return err
}

Expand All @@ -1840,12 +1844,14 @@ func (pr *Post) DeleteByID(id int) (int, error) {
`, id)

if err != nil {
_ = tx.Rollback()
return 0, err
}

var count int
err = row.Scan(&count)
if err != nil {
_ = tx.Rollback()
return 0, err
}

Expand Down
3 changes: 3 additions & 0 deletions app/interfaces/repository/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ func (tr *Tag) FindByName(name string) (domain.Tag, error) {
func (tr *Tag) Save(req request.StoreTag) (int, error) {
tx, err := tr.ConnMySQL.Begin()
if err != nil {
_ = tx.Rollback()
return 0, err
}

Expand Down Expand Up @@ -211,6 +212,7 @@ func (tr *Tag) Save(req request.StoreTag) (int, error) {
func (tr *Tag) SaveByID(req request.UpdateTag) error {
tx, err := tr.ConnMySQL.Begin()
if err != nil {
_ = tx.Rollback()
return err
}

Expand Down Expand Up @@ -249,6 +251,7 @@ func (tr *Tag) DeleteByID(id int) (int, error) {
`, id)

if err != nil {
_ = tx.Rollback()
return 0, err
}

Expand Down

0 comments on commit 634a379

Please sign in to comment.