Example #1
0
 private void UpdateSession(SerializerSession session)
 {
     this.Session = session;
     HistoryTypes.Clear();
     foreach (var item in session.PumpDataHistory.MultiPacketHandlers)
     {
         HistoryTypes.Add(item.ReadInfoResponse.HistoryDataType);
     }
     if (this.HistoryTypes.Count > 0)
     {
         this.SelectedHistoryType = HistoryTypes.FirstOrDefault();
     }
 }
        public History(Guid id, string issueId, Guid userId, HistoryTypes action, DateTime createdAt, Field[] changedFields)
        {
            if (id == Guid.Empty)
            {
                throw new InvalidIdException(issueId);
            }

            if (string.IsNullOrEmpty(issueId))
            {
                throw new InvalidIssueIdException(issueId);
            }

            if (userId == Guid.Empty)
            {
                throw new InvalidUserIdException(userId);
            }

            Id            = id;
            IssueId       = issueId;
            UserId        = userId;
            Action        = action;
            CreatedAt     = createdAt == DateTime.MinValue ? DateTime.Now : createdAt;
            ChangedFields = changedFields;
        }
Example #3
0
        public async Task SaveHistory(Issue oldIssue, Issue newIssue, HistoryTypes action)
        {
            if (oldIssue == null)
            {
                oldIssue = Issue.Empty;
            }

            if (newIssue == null)
            {
                throw new ArgumentNullException(nameof(newIssue));
            }

            if (oldIssue != Issue.Empty && oldIssue.Id != newIssue.Id)
            {
                throw new InvalidIssueIdException(newIssue.Id);
            }

            var oldType = oldIssue.GetType();
            var newType = newIssue.GetType();

            var oldProperties = oldType.GetProperties();
            var newProperties = newType.GetProperties();

            List <Field> changedFields = new List <Field>();

            foreach (var oldProperty in oldProperties)
            {
                var matchingProperty = newProperties.FirstOrDefault(x =>
                                                                    !Attribute.IsDefined(x, typeof(IgnoreHistoryAttribute)) &&
                                                                    x.Name == oldProperty.Name && x.PropertyType == oldProperty.PropertyType);

                if (matchingProperty == null)
                {
                    continue;
                }

                var fieldTypeAttribute = oldProperty.GetCustomAttribute(typeof(HistoryFieldTypeAttribute)) as HistoryFieldTypeAttribute;
                var fieldType          = fieldTypeAttribute?.FieldType ?? FieldTypes.String;

                string oldValue;
                string newValue;

                switch (fieldType)
                {
                case FieldTypes.Assignees:
                case FieldTypes.LinkedIssues:
                    var oldPropertyArray = (((IEnumerable <Guid>)oldProperty.GetValue(oldIssue)) ?? Enumerable.Empty <Guid>()).ToArray();
                    var newPropertyArray = (((IEnumerable <Guid>)matchingProperty.GetValue(newIssue)) ?? Enumerable.Empty <Guid>()).ToArray();
                    oldValue = string.Join(',', oldPropertyArray);
                    newValue = string.Join(',', newPropertyArray);
                    break;

                default:
                    oldValue = oldProperty.GetValue(oldIssue)?.ToString();
                    newValue = matchingProperty.GetValue(newIssue)?.ToString();
                    break;
                }

                if (oldValue == newValue)
                {
                    continue;
                }

                var field = new Field(matchingProperty.Name, oldValue, newValue, fieldType);
                changedFields.Add(field);
            }

            var dateChanged = DateTime.Now;
            var userId      = _appContext.Identity.Id;

            if (changedFields.Count == 0)
            {
                return;
            }

            var history = new History(Guid.NewGuid(), newIssue.Id, userId, action, dateChanged, changedFields.ToArray());
            await _historyRepository.AddAsync(history);
        }