public void AddFriend(string user, string newFriend) { using (SqlDBFriendListContext db = new SqlDBFriendListContext()) { db.Friends.Add(new Friend() { User = user, Friend1 = newFriend }); db.SaveChanges(); } }
public void DeleteUser(string userName) { //Remove user from Friend table using(SqlDBFriendListContext db = new SqlDBFriendListContext()) { List<Friend> friends = db.Friends.Where(u => (u.Friend1 == userName) || (u.User == userName)).ToList(); foreach(Friend friend in friends) { db.Friends.Remove(friend); db.Entry(friend).State = EntityState.Deleted; } db.SaveChanges(); } //Remove user from FriendRequest and Message table using(TempDBFriendContext db = new TempDBFriendContext()) { List<FriendRequest> friends = db.FriendRequests.Where(u => (u.Sender == userName) || (u.Receiver == userName)).ToList(); List<Message> messages = db.Messages.Where(u => (u.Sender == userName) || (u.Receiver == userName)).ToList(); foreach (FriendRequest friend in friends) { db.FriendRequests.Remove(friend); db.Entry(friend).State = EntityState.Deleted; } foreach(Message m in messages) { db.Messages.Remove(m); db.Entry(m).State = EntityState.Deleted; } db.SaveChanges(); } //Remove user from User table using(TempDBContext1 db = new TempDBContext1()) { User user = db.Users.Where(u => u.UserName == userName).FirstOrDefault(); if (user != null) { //db.Users.Attach(user); db.Users.Remove(user); db.Entry(user).State = EntityState.Deleted; db.SaveChanges(); } } //Remove user from LogUser table and UserRoles table using(TempDBLogUsers db = new TempDBLogUsers()) { LogUser user = db.LogUsers.Where(u => u.UserName == userName).FirstOrDefault(); if (user != null) { user.Roles.FirstOrDefault().LogUsers.Remove(user); user.Roles.Remove(user.Roles.FirstOrDefault()); //db.LogUsers.Attach(user); db.LogUsers.Remove(user); db.Entry(user).State = EntityState.Deleted; db.SaveChanges(); } } }
public void RemoveFriend(string user, string friend) { using (SqlDBFriendListContext db = new SqlDBFriendListContext()) { Friend toRemove; try { toRemove = db.Friends.Where(x => x.User == user && x.Friend1 == friend).First(); } catch(Exception) { toRemove = db.Friends.Where(x => x.User == friend && x.Friend1 == user).First(); } db.Friends.Remove(toRemove); db.SaveChanges(); } }
public IEnumerable<string> GetFriends(User user) { IEnumerable<string> friends = null; using (SqlDBFriendListContext db = new SqlDBFriendListContext()) { IEnumerable<string> temp = db.Friends.Where(x => x.User.Equals(user.UserName)).Select(x => x.Friend1).ToList(); IEnumerable<string> temp2 = db.Friends.Where(x => x.Friend1.Equals(user.UserName)).Select(x => x.User).ToList(); friends = temp.Union(temp2).ToList(); } return friends; }