public IEnumerable<DynamicFieldDto> SaveFields(IEnumerable<DynamicFieldDto> fieldDtos, TaskDto taskDto) { var document = _repository.Query<Document>().SingleOrDefault(x => x.Id == taskDto.CurrentDocument.Id); if (document == null) { throw new ArgumentException("Task should have this document"); } var fieldToReturn = new List<DynamicField>(); foreach (var dynamicFieldDto in fieldDtos) { if (dynamicFieldDto.Id == Guid.Empty) { var newField = new DynamicField { Configuration = _repository.Query<DynamicFieldTemplate>().FirstOrDefault(x => x.Id == dynamicFieldDto.Configuration.Id) }; var newFieldValue = dynamicFieldDto.GetValue(); if (newFieldValue != null) { newField.SetValue(newFieldValue); document.Fields.Add(newField); } else { newField.Id = Guid.Empty; } fieldToReturn.Add(newField); } else { var dynamicFieldToUpdate = _repository.Query<DynamicField>().FirstOrDefault(x => x.Id == dynamicFieldDto.Id); if (dynamicFieldToUpdate == null) throw new ArgumentException("Field to update is not found"); var newFieldValue = dynamicFieldDto.GetValue(); dynamicFieldToUpdate.SetValue(newFieldValue); fieldToReturn.Add(dynamicFieldToUpdate); } } _repository.Commit(); var result = new List<DynamicFieldDto>(); foreach (var dynamicField in fieldToReturn) { var dynamicFieldDto = new DynamicFieldDto { Id = dynamicField.Id, Configuration = new DynamicFieldTemplateDto { Id = dynamicField.Configuration.Id, Code = dynamicField.Configuration.Code, IsEnabled = dynamicField.Configuration.IsEnabled, Label = dynamicField.Configuration.Label, Length = dynamicField.Configuration.Length, Type = dynamicField.Configuration.Type } }; dynamicFieldDto.CreateValue(); dynamicFieldDto.Value.Id = dynamicField.Value.Id; var newValue = dynamicField.GetValue(); if (newValue != null) dynamicFieldDto.SetValue(newValue); result.Add(dynamicFieldDto); } return result; }
private static DocumentDto DocumentToDto(Document document) { if (document == null) return null; var documentDto = new DocumentDto { Name = document.Name, Id = document.Id }; var fieldsCollection = new Collection<DynamicFieldDto>(); foreach (var field in document.Fields) { var dFieldDto = new DynamicFieldDto { Configuration = new DynamicFieldTemplateDto() { Code = field.Configuration.Code, Id = field.Configuration.Id, IsEnabled = field.Configuration.IsEnabled, Label = field.Configuration.Label, Length = field.Configuration.Length, Type = field.Configuration.Type, Weight = field.Configuration.Weight, }, Id = field.Id }; dFieldDto.SetValue(field.GetValue()); fieldsCollection.Add(dFieldDto); } documentDto.Fields = fieldsCollection; return documentDto; }