コード例 #1
0
        public IEnumerable<TimeSheetItem> GetWorkItems(DateTime firstDayOfWeek, string userName)
        {
            List<TimeSheetItem> dbEntries;
            using (var appDbContext = new AppDbContext())
            {
                dbEntries =
                    appDbContext.TimeSheetItems
                                .Where(t => t.FirstDayOfWeek.Equals(firstDayOfWeek) &&
                                            t.UserName.Equals(_tfsDataService.UserUniqueName) &&
                                            t.ServerUrl.Equals(_url) &&
                                            t.Project.Equals(_project))
                                .OrderBy(t => t.Name)
                                .ToList();
            }

            var tfsEntries = _tfsDataService.GetWorkItems(firstDayOfWeek, userName);

            // All dbEntries that aren't available in tfs anymore are removed or closed
            foreach (var entry in dbEntries.Where(entry => !tfsEntries.Any(t => t.WorkItemId.Equals(entry.WorkItemId))))
            {
                entry.IsClosed = true;
            }

            // Add all new tfsEntries
            foreach (var timeSheetItem in tfsEntries)
            {
                var dbEntry = dbEntries.SingleOrDefault(t => t.WorkItemId.Equals(timeSheetItem.WorkItemId));
                if (dbEntry == null)
                {
                    dbEntries.Add(new TimeSheetItem
                        {
                            WorkItemId = timeSheetItem.WorkItemId,
                            Name = timeSheetItem.Name,
                            WorkRemaining = timeSheetItem.WorkRemaining,
                            FirstDayOfWeek = firstDayOfWeek
                        });
                }
                else
                {
                    dbEntry.WorkRemaining = timeSheetItem.WorkRemaining;
                }
            }
            return dbEntries;
        }
コード例 #2
0
        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();
                }
            }
        }