public void UpdateBacklogItemGroup(BacklogItemGroup group) { using (var context = new ScrumFactoryEntities(this.connectionString)) { BacklogItemGroup oldGroup = context.BacklogItemGroups.Where(g => g.GroupUId == group.GroupUId).SingleOrDefault(); if (oldGroup == null) { context.AddObject("BacklogItemGroups", group); } else { context.ApplyCurrentValues <BacklogItemGroup>("BacklogItemGroups", group); } context.SaveChanges(); } }
private void SaveBacklogItem(ScrumFactoryEntities context, BacklogItem item, bool saveHours) { // dont want to add the item group again, so if the group is already at the database // remove it from the item BacklogItemGroup group = context.BacklogItemGroups.SingleOrDefault(g => g.GroupUId == item.GroupUId); if (group != null) { item.Group = null; } BacklogItem oldItem = GetBacklogItem(context, item.BacklogItemUId); // if is a new item insert it if (oldItem == null) { int?lastNumber = context.BacklogItems.Where(b => b.ProjectUId == item.ProjectUId).Max(b => (int?)b.BacklogItemNumber); if (lastNumber == null) { lastNumber = 0; } item.BacklogItemNumber = (int)lastNumber + 1; if (item.BusinessPriority == 0) { int?lastPriority = context.BacklogItems.Where(b => b.ProjectUId == item.ProjectUId).Max(b => (int?)b.BusinessPriority); if (lastPriority == null) { lastPriority = 1; } else { lastPriority = lastPriority + 10; } item.BusinessPriority = lastPriority.Value; } context.BacklogItems.AddObject(item); } else { // updates the item context.AttachTo("BacklogItems", oldItem); context.ApplyCurrentValues <BacklogItem>("BacklogItems", item); if (saveHours) { // detect the changes var insertedHours = item.PlannedHours; if (oldItem.PlannedHours != null) { insertedHours = item.PlannedHours.Where(p => !oldItem.PlannedHours.Any(o => (o.BacklogItemUId == p.BacklogItemUId && o.RoleUId == p.RoleUId && o.PlanningNumber == p.PlanningNumber))).ToList(); } // ATTENTION HERE: DID NOT INCLUDE THE o.PlanningNumber == p.PlanningNumber BECAUSE I DONT WANT TO DELETE OLD PLANNINGS var deletedHours = new List <PlannedHour>(); if (oldItem.PlannedHours != null) { deletedHours = oldItem.PlannedHours.Where(o => !item.PlannedHours.Any(p => o.BacklogItemUId == p.BacklogItemUId && o.RoleUId == p.RoleUId)).ToList(); } var updatedHours = new List <PlannedHour>(); if (oldItem.PlannedHours != null) { updatedHours = item.PlannedHours.Where(p => oldItem.PlannedHours.Any(o => o.BacklogItemUId == p.BacklogItemUId && o.RoleUId == p.RoleUId && o.PlanningNumber == p.PlanningNumber)).ToList(); } // insert, update and delete foreach (PlannedHour p in updatedHours) { context.ApplyCurrentValues <PlannedHour>("PlannedHours", p); } foreach (PlannedHour p in insertedHours) { context.AddObject("PlannedHours", p); } foreach (PlannedHour p in deletedHours) { context.DeleteObject(p); } } } }