Skip to content

Commit

Permalink
bring back refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
therealpaulgg committed Aug 15, 2024
1 parent a7685cd commit 12afa58
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 56 deletions.
2 changes: 1 addition & 1 deletion pkg/database/query/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func RollbackFunc(txQueryService TransactionService, tx pgx.Tx, w http.ResponseW
log.Err(err).Msg("error rolling back transaction")
}
}
if err != nil {
if *err != nil {
rb(tx)
} else {
internalErr := txQueryService.Commit(tx)
Expand Down
56 changes: 20 additions & 36 deletions pkg/web/router/routes/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,24 +116,7 @@ func addData(i *do.Injector) http.HandlerFunc {
w.WriteHeader(http.StatusInternalServerError)
return
}
defer func() {
rb := func(tx pgx.Tx) {
err := txQueryService.Rollback(tx)
if err != nil {
log.Err(err).Msg("error rolling back transaction")
}
}
if err != nil {
rb(tx)
} else {
internalErr := txQueryService.Commit(tx)
if internalErr != nil {
log.Err(internalErr).Msg("error committing transaction")
rb(tx)
w.WriteHeader(http.StatusInternalServerError)
}
}
}()
defer query.RollbackFunc(txQueryService, tx, w, &err)
if err = userRepo.AddAndUpdateConfigTx(user, tx); err != nil {
log.Err(err).Msg("could not add config")
w.WriteHeader(http.StatusInternalServerError)
Expand Down Expand Up @@ -198,24 +181,25 @@ func deleteData(i *do.Injector) http.HandlerFunc {
w.WriteHeader(http.StatusInternalServerError)
return
}
defer func() {
rb := func(tx pgx.Tx) {
err := txQueryService.Rollback(tx)
if err != nil {
log.Err(err).Msg("error rolling back transaction")
}
}
if err != nil {
rb(tx)
} else {
internalErr := txQueryService.Commit(tx)
if internalErr != nil {
log.Err(internalErr).Msg("error committing transaction")
rb(tx)
w.WriteHeader(http.StatusInternalServerError)
}
}
}()
defer query.RollbackFunc(txQueryService, tx, w, &err)
// defer func() {
// rb := func(tx pgx.Tx) {
// err := txQueryService.Rollback(tx)
// if err != nil {
// log.Err(err).Msg("error rolling back transaction")
// }
// }
// if err != nil {
// rb(tx)
// } else {
// internalErr := txQueryService.Commit(tx)
// if internalErr != nil {
// log.Err(internalErr).Msg("error committing transaction")
// rb(tx)
// w.WriteHeader(http.StatusInternalServerError)
// }
// }
// }()
if err = userRepo.DeleteUserKeyTx(user, key.ID, tx); err != nil {
log.Err(err).Msg("could not delete key")
w.WriteHeader(http.StatusInternalServerError)
Expand Down
1 change: 0 additions & 1 deletion pkg/web/router/routes/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,5 +340,4 @@ func TestDeleteKeyError(t *testing.T) {
t.Errorf("deleteData returned wrong status code: got %v want %v",
status, http.StatusInternalServerError)
}

}
19 changes: 1 addition & 18 deletions pkg/web/router/routes/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,7 @@ func initialSetup(i *do.Injector) http.HandlerFunc {
w.WriteHeader(http.StatusInternalServerError)
return
}
defer func() {
rb := func(tx pgx.Tx) {
err := txQueryService.Rollback(tx)
if err != nil {
log.Err(err).Msg("error rolling back transaction")
}
}
if err != nil {
rb(tx)
} else {
internalErr := txQueryService.Commit(tx)
if internalErr != nil {
log.Err(internalErr).Msg("error committing transaction")
rb(tx)
w.WriteHeader(http.StatusInternalServerError)
}
}
}()
defer query.RollbackFunc(txQueryService, tx, w, &err)
userRepo := do.MustInvoke[repository.UserRepository](i)
user := &models.User{}
user.Username = userDto.Username
Expand Down

0 comments on commit 12afa58

Please sign in to comment.