private static TimeSpan GetDateTimeDelta(TimeSheetItem timeSheetItem, TimeSheetItem original, PropertyInfo property)
        {
            // Get the original value or get the default value
            var defaultDateTime = new TimeSheetItem().Monday;

            var originalValue = original == null
                                    ? defaultDateTime
                                    : Convert.ToDateTime(property.GetValue(original));

            var changedValue = Convert.ToDateTime(property.GetValue(timeSheetItem));

            // Sanity check to make sure we're not adding a massive amount of time
            // And to make sure we can save the datetime into sql Db
            changedValue = new DateTime(defaultDateTime.Year, defaultDateTime.Month, defaultDateTime.Day,
                changedValue.Hour, changedValue.Minute, changedValue.Second);

            // Determine the delta between the to datetimes
            return changedValue.Subtract(originalValue);
        }
        private void UpdateTotalRecord()
        {
            _week.Remove(_week.SingleOrDefault(t => t.IsTotal));
            var total = new TimeSheetItem { IsTotal = true, Name = "TOTAL" };

            foreach (var item in _week)
            {
                total.WorkRemaining = total.WorkRemaining
                                           .AddHours(item.WorkRemaining.Hour)
                                           .AddMinutes(item.WorkRemaining.Minute)
                                           .AddSeconds(item.WorkRemaining.Second);
                total.Monday = total.Monday
                                    .AddHours(item.Monday.Hour)
                                    .AddMinutes(item.Monday.Minute)
                                    .AddSeconds(item.Monday.Second);
                total.Tuesday = total.Tuesday
                                     .AddHours(item.Tuesday.Hour)
                                     .AddMinutes(item.Tuesday.Minute)
                                     .AddSeconds(item.Tuesday.Second);
                total.Wednesday = total.Wednesday
                                       .AddHours(item.Wednesday.Hour)
                                       .AddMinutes(item.Wednesday.Minute)
                                       .AddSeconds(item.Wednesday.Second);
                total.Thursday = total.Thursday
                                      .AddHours(item.Thursday.Hour)
                                      .AddMinutes(item.Thursday.Minute)
                                      .AddSeconds(item.Thursday.Second);
                total.Friday = total.Friday
                                    .AddHours(item.Friday.Hour)
                                    .AddMinutes(item.Friday.Minute)
                                    .AddSeconds(item.Friday.Second);
            }
            _week.Add(total);
        }
        public void SaveItem(TimeSheetItem timeSheetItem, string changedPropertyName)
        {
            if (timeSheetItem == null)
            {
                throw new ArgumentNullException("timeSheetItem");
            }
            if (string.IsNullOrWhiteSpace(changedPropertyName))
            {
                throw new ArgumentNullException("changedPropertyName");
            }

            using (var transactionScope = new TransactionScope())
            {
                using (var appDbContext = new AppDbContext())
                {
                    var original =
                        appDbContext.TimeSheetItems
                                    .SingleOrDefault(t => t.WorkItemId.Equals(timeSheetItem.WorkItemId) &&
                                                          t.UserName.Equals(_tfsDataService.UserUniqueName));

                    // Create a new item when it doesn't exist in the Db
                    if (original == null)
                    {
                        original = new TimeSheetItem
                            {
                                WorkItemId = timeSheetItem.WorkItemId,
                                Project = _project,
                                ServerUrl = _url,
                                Name = timeSheetItem.Name,
                                FirstDayOfWeek = timeSheetItem.FirstDayOfWeek,
                                UserName = _tfsDataService.UserUniqueName
                            };
                        appDbContext.TimeSheetItems.Add(original);
                    }

                    var property = typeof(TimeSheetItem).GetProperty(changedPropertyName);
                    // Check if we're updating the effort
                    if (property.PropertyType == typeof(DateTime))
                    {
                        var delta = GetDateTimeDelta(timeSheetItem, original, property);

                        _tfsDataService.UpdateWorkItemEffort(original.WorkItemId, delta);

                        // Update the object with the delta and save it into the Db
                        property.SetValue(original, Convert.ToDateTime(property.GetValue(original)).Add(delta));
                        appDbContext.SaveChanges();
                    }
                    // Or if we're updating the state
                    else if (property.Name.Equals("IsClosed"))
                    {
                        _tfsDataService.UpdateWorkItemClosedState(original.WorkItemId);

                        appDbContext.SaveChanges();
                    }

                    // Commit the entire transaction
                    transactionScope.Complete();
                }
            }
        }