Ejemplo n.º 1
0
        // Usuwanie znajomego
        private void RemoveFriend__Task(string friendId)
        {
            // Sprawdzanie, czy użytkownicy są znajomymi
            UserFriendshipModel friendship;
            try
            {
                friendship = 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)))
                    .Single();
            }
            catch (InvalidOperationException)
            {
                friendship = null;
            }

            if (friendship != null)
            {
                ApplicationDbContext db = new ApplicationDbContext();
                Friend friend = new Friend(friendship, this.Id);
                friendship.IsActive = false;

                db.AspNetUserFriendships.Attach(friendship);
                var entry = db.Entry(friendship);
                entry.Property(m => m.IsActive).IsModified = true;
                db.SaveChanges();

                Friends.Remove(friend);
            }
            return;
        }
Ejemplo n.º 2
0
        // 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;
        }