Skip to content

Commit

Permalink
Separate DeleteUser DB operation to own function
Browse files Browse the repository at this point in the history
  • Loading branch information
RichDom2185 committed Aug 22, 2023
1 parent 060a0c9 commit 273b869
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions model/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,19 @@ func CreateUsers(db *gorm.DB, users *[]*User) (int64, error) {
return rowCount, nil
}

func (u *User) delete(tx *gorm.DB, userID uint) *gorm.DB {
return tx.
Model(u).
Where("id = ?", userID).
First(u). // store the value to be returned
Delete(u)
}

func DeleteUser(db *gorm.DB, userID int) (User, error) {
var user User
err := db.
Model(&user).
Where("id = ?", userID).
First(&user). // store the value to be returned
Delete(&user).
Error
err := db.Transaction(func(tx *gorm.DB) error {
return user.delete(tx, uint(userID)).Error
})
if err != nil {
return user, database.HandleDBError(err, "user")
}
Expand Down

0 comments on commit 273b869

Please sign in to comment.