public async Task <string> UpdateOrAddRosterItem(bool onlyUpdate, RosterItem value) { using (var context = CreateContext()) { var item = DbRosterItem.FromFSharp(value); item.ApplicationUserId = user; if (onlyUpdate) { var contextTrackedItem = await context.RosterItems.FindAsync(item.ApplicationUserId, item.Jid); if (contextTrackedItem == null) { //foreach (var group in item.Groups) { // group.ApplicationUserId = user; // context.RosterItemGroups.Add (group); // //context.Entry (group).State = EntityState.Added; // //context.Entry (group.RosterGroup).State = EntityState.Added; //} context.RosterItems.Add(item); //context.Entry (item).State = EntityState.Modified; } else { System.Diagnostics.Debug.Assert(contextTrackedItem.ApplicationUserId == user); // delete all groups context.RosterItemGroups.RemoveRange(contextTrackedItem.Groups); //from ri in context.RosterItemGroups //where ri.RosterItemId == contextTrackedItem.Jid && ri.ApplicationUserId == contextTrackedItem.ApplicationUserId //select ri); context.Entry(contextTrackedItem).CurrentValues.SetValues(item); contextTrackedItem.Groups.Clear(); foreach (var group in item.Groups) { group.ApplicationUserId = contextTrackedItem.ApplicationUserId; group.RosterItemId = contextTrackedItem.Jid; group.RosterItem = contextTrackedItem; contextTrackedItem.Groups.Add(group); } } } else { context.RosterItems.Add(item); } var change = new DbRosterChange() { Version = await NextVersion(context), ApplicationUserId = user, Jid = value.Jid.BareId, ChangeType = DbChangeType.Set }; context.RosterChanges.Add(change); //context.RosterChanges.Add (change); return(await SaveNextVersion(context)); } }
public async Task <string> DeleteItem(JabberId value) { using (var context = CreateContext()) { var jid = value.BareId; context.RosterItems.RemoveRange(await (from item in context.RosterItems where item.ApplicationUserId == user where item.Jid == jid select item).ToListAsync()); var change = new DbRosterChange() { Version = await NextVersion(context), ApplicationUserId = user, Jid = jid, ChangeType = DbChangeType.Delete }; //user.RosterChanges.Add (change); context.RosterChanges.Add(change); return(await SaveNextVersion(context)); } }