public bool AddContactToUser(string targetUserName) { if (!String.IsNullOrWhiteSpace(targetUserName)) { var searchResult = _dbManager.UserCollection.Find(u => u.UserName == targetUserName); var foundUser = searchResult?.SingleOrDefault(); if (foundUser != null) { Contact newContact = new Contact(foundUser.Name); newContact.ConnectedUserID = foundUser.UserID; var currentUserUpdateResult = _dbManager.UserCollection.UpdateOne <User>(c => c.UserID == CurrentUser.UserID, Builders <User> .Update.Push <Contact>(u => u.Contacts, newContact)); var foundUserUpdateResult = _dbManager.UserCollection.UpdateOne <User>(c => c.UserID == foundUser.UserID, Builders <User> .Update.Push <Contact>(u => u.Contacts, newContact)); if (currentUserUpdateResult.IsModifiedCountAvailable && foundUserUpdateResult.IsModifiedCountAvailable) { _dbManager.ChatCollection.InsertOne(new Chat(newContact.ChatID, new List <ObjectId>() { CurrentUser.UserID, foundUser.UserID })); ContactListChanged?.Invoke(); return(true); } else { return(false); //! error couldnt update user contacts } } else { return(false); //! error couldnt find the user } } else { return(false); //! error param is } }
public void OnNewContactAdded() { UpdateCurrentUser(); ContactListChanged?.Invoke(); }