diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-11-21 23:41:00 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-21 23:41:00 +0800 |
commit | d710af6669654f27f02b69d7ef1ba563e7d58a90 (patch) | |
tree | 9727f468a570106293dc90beb70035180bbb7e8e /models/user/follow.go | |
parent | 0add627182388ac63fd04b94cdf912fb87fd0326 (diff) | |
download | gitea-d710af6669654f27f02b69d7ef1ba563e7d58a90.tar.gz gitea-d710af6669654f27f02b69d7ef1ba563e7d58a90.zip |
Remove NewSession method from db.Engine interface (#17577)
* Remove NewSession method from db.Engine interface
* Fix bug
* Some improvements
* Fix bug
* Fix test
* Use XXXBean instead of XXXExample
Diffstat (limited to 'models/user/follow.go')
-rw-r--r-- | models/user/follow.go | 28 |
1 files changed, 14 insertions, 14 deletions
diff --git a/models/user/follow.go b/models/user/follow.go index 89675b5078..6b02486c43 100644 --- a/models/user/follow.go +++ b/models/user/follow.go @@ -33,24 +33,24 @@ func FollowUser(userID, followID int64) (err error) { return nil } - sess := db.NewSession(db.DefaultContext) - defer sess.Close() - if err = sess.Begin(); err != nil { + ctx, committer, err := db.TxContext() + if err != nil { return err } + defer committer.Close() - if _, err = sess.Insert(&Follow{UserID: userID, FollowID: followID}); err != nil { + if err = db.Insert(ctx, &Follow{UserID: userID, FollowID: followID}); err != nil { return err } - if _, err = sess.Exec("UPDATE `user` SET num_followers = num_followers + 1 WHERE id = ?", followID); err != nil { + if _, err = db.Exec(ctx, "UPDATE `user` SET num_followers = num_followers + 1 WHERE id = ?", followID); err != nil { return err } - if _, err = sess.Exec("UPDATE `user` SET num_following = num_following + 1 WHERE id = ?", userID); err != nil { + if _, err = db.Exec(ctx, "UPDATE `user` SET num_following = num_following + 1 WHERE id = ?", userID); err != nil { return err } - return sess.Commit() + return committer.Commit() } // UnfollowUser unmarks someone as another's follower. @@ -59,22 +59,22 @@ func UnfollowUser(userID, followID int64) (err error) { return nil } - sess := db.NewSession(db.DefaultContext) - defer sess.Close() - if err = sess.Begin(); err != nil { + ctx, committer, err := db.TxContext() + if err != nil { return err } + defer committer.Close() - if _, err = sess.Delete(&Follow{UserID: userID, FollowID: followID}); err != nil { + if _, err = db.DeleteByBean(ctx, &Follow{UserID: userID, FollowID: followID}); err != nil { return err } - if _, err = sess.Exec("UPDATE `user` SET num_followers = num_followers - 1 WHERE id = ?", followID); err != nil { + if _, err = db.Exec(ctx, "UPDATE `user` SET num_followers = num_followers - 1 WHERE id = ?", followID); err != nil { return err } - if _, err = sess.Exec("UPDATE `user` SET num_following = num_following - 1 WHERE id = ?", userID); err != nil { + if _, err = db.Exec(ctx, "UPDATE `user` SET num_following = num_following - 1 WHERE id = ?", userID); err != nil { return err } - return sess.Commit() + return committer.Commit() } |