/// <summary> /// we will now assign all of the values in the 'save' model to the DTO object /// </summary> /// <param name="saveModel"></param> /// <param name="dto"></param> public void MapPropertyValuesFromSaved(IContentProperties <ContentPropertyBasic> saveModel, ContentPropertyCollectionDto dto) { //NOTE: Don't convert this to linq, this is much quicker foreach (var p in saveModel.Properties) { foreach (var propertyDto in dto.Properties) { if (propertyDto.Alias != p.Alias) { continue; } propertyDto.Value = p.Value; break; } } }
public void To_Content_Item_Dto() { Content content = _contentBuilder .AddContentType() .AddPropertyGroup() .AddPropertyType() .Done() .Done() .Done() .WithCreatorId(Constants.Security.SuperUserId) .Build(); ContentPropertyCollectionDto result = _sut.Map <IContent, ContentPropertyCollectionDto>(content); foreach (IProperty p in content.Properties) { AssertProperty(result, p); } }
public void To_Media_Item_Dto() { Media content = _mediaBuilder .AddMediaType() .AddPropertyGroup() .AddPropertyType() .Done() .Done() .Done() .Build(); ContentPropertyCollectionDto result = _sut.Map <IMedia, ContentPropertyCollectionDto>(content); Assert.GreaterOrEqual(result.Properties.Count(), 1); Assert.AreEqual(content.Properties.Count, result.Properties.Count()); foreach (IProperty p in content.Properties) { AssertProperty(result, p); } }
/// <summary> /// We need to manually validate a few things here like email and login to make sure they are valid and aren't duplicates /// </summary> /// <param name="model"></param> /// <param name="dto"></param> /// <param name="modelState"></param> /// <param name="modelWithProperties"></param> /// <returns></returns> public override bool ValidatePropertiesData(MemberSave model, IContentProperties <ContentPropertyBasic> modelWithProperties, ContentPropertyCollectionDto dto, ModelStateDictionary modelState) { if (model.Username.IsNullOrWhiteSpace()) { modelState.AddPropertyError( new ValidationResult("Invalid user name", new[] { "value" }), $"{Constants.PropertyEditors.InternalGenericPropertiesPrefix}login"); } if (model.Email.IsNullOrWhiteSpace() || new EmailAddressAttribute().IsValid(model.Email) == false) { modelState.AddPropertyError( new ValidationResult("Invalid email", new[] { "value" }), $"{Constants.PropertyEditors.InternalGenericPropertiesPrefix}email"); } //default provider! var membershipProvider = Core.Security.MembershipProviderExtensions.GetMembersMembershipProvider(); var validEmail = ValidateUniqueEmail(model, membershipProvider); if (validEmail == false) { modelState.AddPropertyError( new ValidationResult("Email address is already in use", new[] { "value" }), $"{Constants.PropertyEditors.InternalGenericPropertiesPrefix}email"); } var validLogin = ValidateUniqueLogin(model, membershipProvider); if (validLogin == false) { modelState.AddPropertyError( new ValidationResult("Username is already in use", new[] { "value" }), $"{Constants.PropertyEditors.InternalGenericPropertiesPrefix}login"); } return(base.ValidatePropertiesData(model, modelWithProperties, dto, modelState)); }
/// <summary> /// Maps the dto property values to the persisted model /// </summary> internal void MapPropertyValuesForPersistence <TPersisted, TSaved>( TSaved contentItem, ContentPropertyCollectionDto dto, Func <TSaved, Property, object> getPropertyValue, Action <TSaved, Property, object> savePropertyValue, string culture) where TPersisted : IContentBase where TSaved : IContentSave <TPersisted> { // map the property values foreach (var propertyDto in dto.Properties) { // get the property editor if (propertyDto.PropertyEditor == null) { Logger.Warn <ContentController>("No property editor found for property {PropertyAlias}", propertyDto.Alias); continue; } // get the value editor // nothing to save/map if it is readonly var valueEditor = propertyDto.PropertyEditor.GetValueEditor(); if (valueEditor.IsReadOnly) { continue; } // get the property var property = contentItem.PersistedContent.Properties[propertyDto.Alias]; // prepare files, if any matching property and culture var files = contentItem.UploadedFiles .Where(x => x.PropertyAlias == propertyDto.Alias && x.Culture == propertyDto.Culture) .ToArray(); foreach (var file in files) { file.FileName = file.FileName.ToSafeFileName(); } // create the property data for the property editor var data = new ContentPropertyData(propertyDto.Value, propertyDto.DataType.Configuration) { ContentKey = contentItem.PersistedContent.Key, PropertyTypeKey = property.PropertyType.Key, Files = files }; // let the editor convert the value that was received, deal with files, etc var value = valueEditor.FromEditor(data, getPropertyValue(contentItem, property)); // set the value - tags are special var tagAttribute = propertyDto.PropertyEditor.GetTagAttribute(); if (tagAttribute != null) { var tagConfiguration = ConfigurationEditor.ConfigurationAs <TagConfiguration>(propertyDto.DataType.Configuration); if (tagConfiguration.Delimiter == default) { tagConfiguration.Delimiter = tagAttribute.Delimiter; } var tagCulture = property.PropertyType.VariesByCulture() ? culture : null; property.SetTagsValue(value, tagConfiguration, tagCulture); } else { savePropertyValue(contentItem, property, value); } } }
// Umbraco.Code.MapAll private static void Map(IContent source, ContentPropertyCollectionDto target, MapperContext context) { target.Properties = context.MapEnumerable <Property, ContentPropertyDto>(source.Properties); }
// Umbraco.Code.MapAll private static void Map(IMember source, ContentPropertyCollectionDto target, MapperContext context) { target.Properties = context.MapEnumerable <IProperty, ContentPropertyDto>(source.Properties).WhereNotNull(); }