// Konstruktor pobierający dane o znajomym z bazy, a następnie zapisujący wynik do bieżącego // obiektu. W parametrach należy podać informacje o znajomości - model znajomości zawiera // ID obu użytkowników. Dodatkowo potrzebny jest ID użytkownika, którego ZNAJOMEGO dane chcemy // pobrać. public Friend( UserFriendshipModel friendship, string currentUserId ) { // Ustalenie, czyje dane chcemy pobrać if (friendship.User1_ID == currentUserId) UserID = friendship.User2_ID; if (friendship.User2_ID == currentUserId) UserID = friendship.User1_ID; // Pobranie danych z bazy ApplicationDbContext db = new ApplicationDbContext(); ApplicationUser friendData = db.Users.Find(UserID); // Uciekamy przed NullReferenceException if (friendData != null) { ContextID = friendship.ContextID; UserName = friendData.UserName; UserAvatar = friendData.AvatarSrc; UserSecureID = friendData.UserSecureID; MessageContext = new Messages.MessagesContext(friendship.ContextID); } }
// Dodawanie nowego użytkownika private void AddFriend__Task(string friendId) { // Sprawdzanie, czy użytkownicy nie są już znajomymi UserFriendshipModel test; try { test = ApplicationDbContext.Create().AspNetUserFriendships.Where(c => ( (c.User1_ID == friendId && c.User2_ID == this.Id) || (c.User1_ID == this.Id && c.User2_ID == friendId) && (c.IsActive == true))) .First(); } catch (InvalidOperationException) { test = null; } // Jeżeli nie, dodawanie nowej znajomości do bazy danych i do bieżącego modelu if (test == null) { UserFriendshipModel newFriendship = new UserFriendshipModel(this.Id, friendId); Friend newFriend = new Friend(newFriendship, this.Id); Friends.Add(newFriend); } return; }