@@ -60,13 +60,48 @@ public async Task<IList<Guid>> GetAllMutualFriendIdsAsync(Guid currentUserId, IL
60
60
. Distinct ( )
61
61
. ToListAsync ( ) ;
62
62
}
63
-
64
- public async Task < IList < Guid > > GetAllFriendIdsOfCurrentUserAsync ( Guid currentUserId )
63
+
64
+ public async Task < IList < Guid > > GetFriendsOfMutualFriend ( Guid currentUserId )
65
65
{
66
- return await context . Friendships
67
- . Where ( f => ( f . SenderId == currentUserId || f . ReceiverId == currentUserId ) && f . Status == FriendshipStatus . Connected )
68
- . Select ( f => f . SenderId == currentUserId ? f . ReceiverId : f . SenderId )
66
+ var friendsOfUser = await DbSet
67
+ . Where ( f1 => ( f1 . SenderId == currentUserId || f1 . ReceiverId == currentUserId ) && f1 . Status == FriendshipStatus . Connected )
68
+ . Select ( f1 => f1 . SenderId == currentUserId ? f1 . ReceiverId : f1 . SenderId )
69
+ . ToListAsync ( ) ;
70
+
71
+ var mutualFriends = await DbSet
72
+ . Where ( f2 => friendsOfUser . Contains ( f2 . SenderId ) || friendsOfUser . Contains ( f2 . ReceiverId ) )
73
+ . Where ( f2 => f2 . Status == FriendshipStatus . Connected && f2 . SenderId != currentUserId && f2 . ReceiverId != currentUserId )
74
+ . Select ( f2 => friendsOfUser . Contains ( f2 . SenderId ) ? f2 . ReceiverId : f2 . SenderId )
75
+ . Distinct ( )
69
76
. ToListAsync ( ) ;
77
+
78
+ return mutualFriends ;
79
+ }
80
+
81
+ public int GetMutualFriendCount ( IList < Guid > currentUserFiends , IList < Guid > friendOfFriends )
82
+ {
83
+ var mutualFriends = currentUserFiends . Intersect ( friendOfFriends ) . ToList ( ) ;
84
+
85
+ int mutualFriendCount = mutualFriends . Count ;
86
+
87
+ return mutualFriendCount ;
88
+ }
89
+
90
+ public async Task < IList < ( Guid MutualFriendId , int MutualFriendCount ) > > GetMutualFriendsAndCount ( Guid currentUserId , IList < Guid > friendsOfMutualFriend )
91
+ {
92
+ var friendsOfUser = await GetAllFriendIdsAsync ( currentUserId ) ;
93
+
94
+ var friendsOfMutualFriendAndCount = new List < ( Guid MutualFriendId , int MutualFriendCount ) > ( ) ;
95
+
96
+ foreach ( var friend in friendsOfMutualFriend )
97
+ {
98
+ var mutualFriendFriends = await GetAllFriendIdsAsync ( friend ) ;
99
+
100
+ var mutualFriendCount = GetMutualFriendCount ( friendsOfUser , mutualFriendFriends ) ;
101
+
102
+ friendsOfMutualFriendAndCount . Add ( ( MutualFriendId : friend , MutualFriendCount : mutualFriendCount ) ) ;
103
+ }
104
+ return friendsOfMutualFriendAndCount ;
70
105
}
71
106
72
107
public async Task < IList < Guid > > GetAllPendingRequestIdsAsync ( Guid currentUserId )
@@ -93,22 +128,26 @@ public async Task<IList<Guid>> GetAllSentRequestIdsAsync(Guid currentUserId)
93
128
. ToListAsync ( ) ;
94
129
}
95
130
96
- public async Task < IList < ( Guid FriendId , int MutualFriendCount ) > > CountMutualFriends ( IList < string > results , Guid currentUserId )
131
+ public async Task < IList < ( Guid FriendId , int MutualFriendCount ) > > CountMutualFriends ( IList < string > friendIds , Guid currentUserId )
97
132
{
98
- var resultGuids = results . Select ( Guid . Parse ) . ToList ( ) ;
99
- var queryResult = await context . Friendships
100
- . Where ( f => resultGuids . Contains ( f . SenderId ) || resultGuids . Contains ( f . ReceiverId ) )
101
- . Where ( f => f . SenderId != currentUserId && f . ReceiverId != currentUserId )
102
- . Select ( f => f . SenderId == currentUserId ? f . ReceiverId : f . SenderId )
103
- . GroupBy ( friendId => friendId )
104
- . Select ( group => new
105
- {
106
- FriendId = group . Key ,
107
- Count = group . Count ( )
108
- } )
109
- . ToListAsync ( ) ;
133
+ var mutualFriendCounts = await context . Friendships
134
+ . Where ( f1 => ( f1 . SenderId == currentUserId || f1 . ReceiverId == currentUserId ) && f1 . Status == FriendshipStatus . Connected )
135
+ . Join (
136
+ context . Friendships ,
137
+ f1 => f1 . SenderId == currentUserId ? f1 . ReceiverId : f1 . SenderId ,
138
+ f2 => f2 . SenderId == currentUserId ? f2 . ReceiverId : f2 . SenderId ,
139
+ ( f1 , f2 ) => new { FriendA = f1 . SenderId == currentUserId ? f1 . ReceiverId : f1 . SenderId , FriendB = f2 . SenderId == currentUserId ? f2 . ReceiverId : f2 . SenderId }
140
+ )
141
+ . Where ( match => friendIds . Contains ( match . FriendB . ToString ( ) ) )
142
+ . GroupBy ( match => match . FriendB )
143
+ . Select ( group => new
144
+ {
145
+ FriendId = group . Key ,
146
+ MutualFriendCount = group . Count ( )
147
+ } )
148
+ . ToListAsync ( ) ;
110
149
111
- return queryResult . Select ( x => ( x . FriendId , x . Count ) ) . ToList ( ) ;
150
+ return mutualFriendCounts . Select ( x => ( x . FriendId , x . MutualFriendCount ) ) . ToList ( ) ;
112
151
}
113
152
114
153
}
0 commit comments