public IActionResult GetFriendSuggestions(string user) { try { FriendSuggestion suggestion = _repo.GetFriendSuggestions(user); if (suggestion.suggestions == null) { return(BadRequest(new AppErrorResponse { status = "failure", reason = "User does not exist" })); } else if (suggestion.suggestions.Count() == 0) { return(NotFound(new AppErrorResponse { status = "failure", reason = "User does not have any friends" })); } else { suggestion.suggestions.Remove(user); suggestion.suggestions = suggestion.suggestions.Distinct().ToList(); return(Ok(suggestion)); } } catch (Exception ex) { _logger.LogError(ex, ex.Message, ex.InnerException); return(BadRequest(new AppErrorResponse())); } }
public FriendSuggestion GetFriendSuggestions(string user) { FriendSuggestion suggestion = new FriendSuggestion(); suggestion.suggestions = new List <string>(); AllFriends all = GetAllFriends(user); if (all.friends == null) { suggestion.suggestions = null; return(suggestion); } foreach (var friend1 in all.friends) { var l1 = GetAllFriends(friend1); l1.friends.Remove(user); if (l1.friends.Count > 0) { suggestion.suggestions.AddRange(l1.friends.Except(all.friends)); foreach (var friend2 in l1.friends) { var l2 = GetAllFriends(friend2); l2.friends.Remove(user); if (l2.friends.Count > 0) { suggestion.suggestions.AddRange(l2.friends.Except(all.friends)); } } } } return(suggestion); }