@@ -921,48 +921,41 @@ func deleteUser(e *xorm.Session, u *User) error {
921
921
}
922
922
923
923
// ***** START: Watch *****
924
- watches := make ([]* Watch , 0 , 10 )
925
- if err = e .Find (& watches , & Watch {UserID : u .ID }); err != nil {
924
+ watchedRepoIDs := make ([]int64 , 0 , 10 )
925
+ if err = e .Table ("watch" ).Cols ("watch.repo_id" ).
926
+ Where ("watch.user_id = ?" , u .ID ).Find (& watchedRepoIDs ); err != nil {
926
927
return fmt .Errorf ("get all watches: %v" , err )
927
928
}
928
- for i := range watches {
929
- if _ , err = e .Exec ("UPDATE `repository` SET num_watches=num_watches-1 WHERE id=?" , watches [i ].RepoID ); err != nil {
930
- return fmt .Errorf ("decrease repository watch number[%d]: %v" , watches [i ].RepoID , err )
931
- }
929
+ if _ , err = e .Decr ("num_watches" ).In ("id" , watchedRepoIDs ).Update (new (Repository )); err != nil {
930
+ return fmt .Errorf ("decrease repository num_watches: %v" , err )
932
931
}
933
932
// ***** END: Watch *****
934
933
935
934
// ***** START: Star *****
936
- stars := make ([]* Star , 0 , 10 )
937
- if err = e .Find (& stars , & Star {UID : u .ID }); err != nil {
935
+ starredRepoIDs := make ([]int64 , 0 , 10 )
936
+ if err = e .Table ("star" ).Cols ("star.repo_id" ).
937
+ Where ("star.uid = ?" , u .ID ).Find (& starredRepoIDs ); err != nil {
938
938
return fmt .Errorf ("get all stars: %v" , err )
939
- }
940
- for i := range stars {
941
- if _ , err = e .Exec ("UPDATE `repository` SET num_stars=num_stars-1 WHERE id=?" , stars [i ].RepoID ); err != nil {
942
- return fmt .Errorf ("decrease repository star number[%d]: %v" , stars [i ].RepoID , err )
943
- }
939
+ } else if _ , err = e .Decr ("num_watches" ).In ("id" , starredRepoIDs ).Update (new (Repository )); err != nil {
940
+ return fmt .Errorf ("decrease repository num_stars: %v" , err )
944
941
}
945
942
// ***** END: Star *****
946
943
947
944
// ***** START: Follow *****
948
- followees := make ([]* Follow , 0 , 10 )
949
- if err = e .Find (& followees , & Follow {UserID : u .ID }); err != nil {
945
+ followeeIDs := make ([]int64 , 0 , 10 )
946
+ if err = e .Table ("follow" ).Cols ("follow.follow_id" ).
947
+ Where ("follow.user_id = ?" , u .ID ).Find (& followeeIDs ); err != nil {
950
948
return fmt .Errorf ("get all followees: %v" , err )
951
- }
952
- for i := range followees {
953
- if _ , err = e .Exec ("UPDATE `user` SET num_followers=num_followers-1 WHERE id=?" , followees [i ].FollowID ); err != nil {
954
- return fmt .Errorf ("decrease user follower number[%d]: %v" , followees [i ].FollowID , err )
955
- }
949
+ } else if _ , err = e .Decr ("num_followers" ).In ("id" , followeeIDs ).Update (new (User )); err != nil {
950
+ return fmt .Errorf ("decrease user num_followers: %v" , err )
956
951
}
957
952
958
- followers := make ([]* Follow , 0 , 10 )
959
- if err = e .Find (& followers , & Follow {FollowID : u .ID }); err != nil {
953
+ followerIDs := make ([]int64 , 0 , 10 )
954
+ if err = e .Table ("follow" ).Cols ("follow.user_id" ).
955
+ Where ("follow.follow_id = ?" , u .ID ).Find (& followerIDs ); err != nil {
960
956
return fmt .Errorf ("get all followers: %v" , err )
961
- }
962
- for i := range followers {
963
- if _ , err = e .Exec ("UPDATE `user` SET num_following=num_following-1 WHERE id=?" , followers [i ].UserID ); err != nil {
964
- return fmt .Errorf ("decrease user following number[%d]: %v" , followers [i ].UserID , err )
965
- }
957
+ } else if _ , err = e .Decr ("num_following" ).In ("id" , followerIDs ).Update (new (User )); err != nil {
958
+ return fmt .Errorf ("decrease user num_following: %v" , err )
966
959
}
967
960
// ***** END: Follow *****
968
961
0 commit comments