public bool saveChangesToActionItem(Models.ActionItem toUpdate, Models.Release targetRelease, Models.User changedBy) { var existingActionItem = db.SQLActionItems.FirstOrDefault(x => x.ID == toUpdate.ID); bool savedUpdate = false; if (existingActionItem != null) { var historyToAdd = findDifferences(existingActionItem, toUpdate); var changeDate = DateTime.UtcNow; var uniqueChangeSetID = Guid.NewGuid(); foreach (var history in historyToAdd) { history.ChangedWhen = changeDate; history.ChangedBy = changedBy.ID; history.ID = Guid.NewGuid(); history.ChangeGrouping = uniqueChangeSetID; db.SQLActionItemHistories.Add(history); } existingActionItem.Estimate = null; existingActionItem.ActionItemTypeId = toUpdate.CurrentType.ID; existingActionItem.CurrentPriority = toUpdate.CurrentPriority.Order; existingActionItem.CurrentStatusId = toUpdate.CurrentStatus.ID; existingActionItem.DateCompleted = toUpdate.DateCompleted; existingActionItem.DateCreated = toUpdate.DateCreated; existingActionItem.Description = toUpdate.Description; existingActionItem.IndividualTargetDate = toUpdate.TargetDate; existingActionItem.InReleaseId = targetRelease.ID; existingActionItem.Name = toUpdate.Title; if (toUpdate.Estimate.HasValue) existingActionItem.Estimate = toUpdate.Estimate.Value.Ticks; else existingActionItem.Estimate = null; if (toUpdate.TimeSpent.HasValue) existingActionItem.TimeSpent = toUpdate.TimeSpent.Value.Ticks; else existingActionItem.TimeSpent = null; if (toUpdate.TargetDate.HasValue) existingActionItem.IndividualTargetDate = toUpdate.TargetDate.Value; #warning this should accept multiple users instead of just one foreach (var newAssignment in toUpdate.AssignedTo) { SQLActionItemUser assignmentsToCreate = new SQLActionItemUser() { ActionItemId = toUpdate.ID, AssignedToUserID = newAssignment.ID }; db.SQLActionItemUsers.Add(assignmentsToCreate); } db.Database.ExecuteSqlCommand(string.Format("delete from ProjectArsenal.dbo.ActionItemUsers where ActionItemID = '{0}'", toUpdate.ID)); db.SaveChanges(); } return savedUpdate; }
public Models.ActionItem createNewActionItem(Models.User creator, Models.Release release, Models.ActionItem toCreate, Models.User assignedTo) { toCreate.AssignedTo = new List<Models.User>(){ assignedTo }; SQLActionItem toInsert = new SQLActionItem() { ActionItemTypeId = toCreate.CurrentType.ID, CreatedByUserId = toCreate.CreatedBy.ID, CurrentPriority = toCreate.CurrentPriority.Order, CurrentStatusId = toCreate.CurrentStatus.ID, DateCompleted = toCreate.DateCompleted, DateCreated = toCreate.DateCreated, Description = toCreate.Description, ID = toCreate.ID == Guid.Empty ? Guid.NewGuid() : toCreate.ID, Estimate = null, InReleaseId = release.ID, Name = toCreate.Title, TimeSpent = null }; #warning this should accept multiple users instead of just one SQLActionItemUser assignmentsToCreate = new SQLActionItemUser() { ActionItemId = toInsert.ID, AssignedToUserID = assignedTo.ID }; if (toCreate.Estimate.HasValue) toInsert.Estimate = toCreate.Estimate.Value.Ticks; if (toCreate.TimeSpent.HasValue) toInsert.TimeSpent = toCreate.TimeSpent.Value.Ticks; db.SQLActionItems.Add(toInsert); db.SaveChanges(); db.SQLActionItemUsers.Add(assignmentsToCreate); List<SQLActionItemHistory> historicEvents = new List<SQLActionItemHistory>(); if (toInsert.Estimate.HasValue) historicEvents.Add(new SQLActionItemHistory() { ActionItemID = toInsert.ID, DescriptionOfChange = string.Format("set estimate to: {0}", toInsert.Estimate.Value), ThingChanged = "Estimate" }); historicEvents.Add(new SQLActionItemHistory() { ActionItemID = toInsert.ID, DescriptionOfChange = string.Format("set type to: {0}", toInsert.ActionItemType.Name), ThingChanged = "Type" }); historicEvents.Add(new SQLActionItemHistory() { ActionItemID = toInsert.ID, DescriptionOfChange = string.Format("set priority to: {0}", toInsert.CurrentPriority), ThingChanged = "Priority" }); historicEvents.Add(new SQLActionItemHistory() { ActionItemID = toInsert.ID, DescriptionOfChange = string.Format("set status to: {0}", toInsert.Status.Name), ThingChanged = "Status" }); if (toInsert.DateCompleted.HasValue) historicEvents.Add(new SQLActionItemHistory() { ActionItemID = toInsert.ID, DescriptionOfChange = string.Format("set completion date to: {0}", toInsert.DateCompleted.Value), ThingChanged = "Date Completed" }); historicEvents.Add(new SQLActionItemHistory() { ActionItemID = toInsert.ID, DescriptionOfChange = string.Format("set action-item description: {0}", toInsert.Description), ThingChanged = "Description" }); if (toInsert.IndividualTargetDate.HasValue) historicEvents.Add(new SQLActionItemHistory() { ActionItemID = toInsert.ID, DescriptionOfChange = string.Format("set target date: {0}", toInsert.IndividualTargetDate.Value), ThingChanged = "Target Date" }); var toAdd = new SQLActionItemHistory() { ActionItemID = toInsert.ID, DescriptionOfChange = "", ThingChanged = "Assigned To" }; foreach (var assignedUser in toInsert.ActionItemUsers) { if (assignedUser.User != null) toAdd.DescriptionOfChange += string.Format("{0},", assignedUser.User.Email); } historicEvents.Add(toAdd); historicEvents.Add(new SQLActionItemHistory() { ActionItemID = toInsert.ID, DescriptionOfChange = string.Format("set title to: {0}", toInsert.Name), ThingChanged = "Title" }); if (toInsert.TimeSpent.HasValue) historicEvents.Add(new SQLActionItemHistory() { ActionItemID = toInsert.ID, DescriptionOfChange = string.Format("set time spent to: {0}", toInsert.TimeSpent.Value), ThingChanged = "Time Spent" }); Guid createEventID = Guid.NewGuid(); foreach (var history in historicEvents) { history.ID = Guid.NewGuid(); history.ChangeGrouping = createEventID; history.ChangedWhen = toInsert.DateCreated; history.ChangedBy = creator.ID; db.SQLActionItemHistories.Add(history); } db.SaveChanges(); return toCreate; }