public void EntityUpdated(colleaguesList updatedEntities) { using (var db = new ApplicationDbContext()) { using (var userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(db))) { Trace.WriteLine("Updated Colleague"); foreach (var entry in updatedEntities) { var dbColleague = userManager.FindByIdAsync(entry.Item2.ColleagueId); switch (entry.Item1) { case EntityState.Added: { //Add Colleague to list var colleagueItemA = new ColleagueItemViewModel(dbColleague.Result); var indexGroup = Clients.Group(LiveUpdateHelper.GroupName(ViewModelDataType.Colleague, ActionType.Index, sId: entry.Item2.UserId)); indexGroup.AddData(ViewModelDataType.Colleague.ToString(), ActionType.Index.ToString(), colleagueItemA); } break; case EntityState.Deleted: { //Remove Colleague from list (by email) var detailsGroup = Clients.Group(LiveUpdateHelper.GroupName(ViewModelDataType.Colleague, ActionType.Details, sId: dbColleague.Result.Email)); detailsGroup.RemoveData(ViewModelDataType.Colleague.ToString(), ActionType.Index.ToString(), dbColleague.Result.Email); } break; case EntityState.Modified: //Not sure when this will happen, so lets ignore it... Trace.WriteLine("UserColleague has been modified and i don't know why..."); break; } } } } }
public bool StartMonitoring_ColleagueViewModel(ColleaguesViewModel model) { //Find the user's Id string userId = HttpContext.Current.GetOwinContext().Authentication.User.Identity.GetUserId(); //Add this user as watching for Colleague Adds Groups.Add(Context.ConnectionId, LiveUpdateHelper.GroupName(userId)); //Add this user as watching updates on each colleague Groups.Add(Context.ConnectionId, LiveUpdateHelper.GroupName(ViewModelDataType.Colleague, ActionType.Index, sId: userId)); foreach (var colleague in model.Colleagues) { Groups.Add(Context.ConnectionId, LiveUpdateHelper.GroupName(ViewModelDataType.Colleague, ActionType.Details, sId: colleague.Email)); } //Check if the model has changed Task.Factory.StartNew(() => { //Check if the model has changed, call update if it has using (var db = new ApplicationDbContext()) { using (var userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(db))) { var thisUser = userManager.FindById(userId); foreach (var userColleagues in thisUser.Colleagues) { //Find this in model if (model.Colleagues.Any(item => item.Email == userColleagues.Colleague.Email)) { var oldColleague = model.Colleagues.First(item => item.Email == userColleagues.Colleague.Email); var newModel = new ColleagueItemViewModel(userColleagues.Colleague); //In new and old if (!oldColleague.PublicInstancePropertiesEqual(newModel)) { //Data has changed //Push change LiveUpdateSingleton.Instance.EntityUpdated(new colleaguesList() { new Tuple <EntityState, UserColleagues>(EntityState.Modified, userColleagues) }); } //Remove from model list model.Colleagues.Remove(oldColleague); } else { //Data has been added from database //Push add LiveUpdateSingleton.Instance.EntityUpdated(new colleaguesList() { new Tuple <EntityState, UserColleagues>(EntityState.Added, userColleagues) }); } } } if (model.Colleagues.Any()) { //Items in the list have been deleted on the server foreach (var removedColleague in model.Colleagues) { //Push delete var detailsGroup = Clients.Group(LiveUpdateHelper.GroupName(ViewModelDataType.Colleague, ActionType.Details, sId: removedColleague.Email)); detailsGroup.RemoveData(ViewModelDataType.Colleague.ToString(), ActionType.Index.ToString(), removedColleague.Email); } } } }); return(true); }