Exemplo n.º 1
0
        public IProductMapper CreateMapper(ProductInfoModelConfiguration configuration)
        {
            var builder = ProductMapperBuilder.Default();

            if (configuration.ProductManufacturer.IncludedForMap)
            {
                builder.WithManufacturer();
            }

            if (configuration.ProductCountryOfOrigin.IncludedForMap)
            {
                builder.WithCountryOfOrigin();
            }

            if (configuration.ProductImporter.IncludedForMap)
            {
                builder.WithImporter();
            }

            if (configuration.ProductImporterCountry.IncludedForMap)
            {
                builder.WithImporterCountry();
            }

            if (configuration.ProductName.IncludedForMap)
            {
                builder.WithName();
            }

            if (configuration.ProductQuantityPerPackage.IncludedForMap)
            {
                builder.WithQuantityPerPackage();
            }

            if (configuration.ProductSize.IncludedForMap)
            {
                builder.WithSize();
            }

            if (configuration.ProductImage.IncludedForMap)
            {
                if (configuration.ProductImage.ShouldRotate90Left)
                {
                    builder.WithImageAsRotated90Left();
                }
                else
                {
                    builder.WithImage();
                }
            }

            if (configuration.LogoPath.IncludedForMap)
            {
                builder.WithLogoPath();
            }

            return(builder.Build());
        }
        public ProductInfoModelValidator(
            ProductInfoModelConfiguration configuration, IStringLocalizer <SharedLocalizationResources> localizer)
        {
            RuleFor(e => e.Manufacturer)
            .NotEmpty()
            .When(_ => configuration.ProductManufacturer.IsRequired)

            .MaximumLength(configuration.ProductSize.MaxLength)
            .When(_ => configuration.ProductSize.MaxLength > 0)
            .WithName(localizer[SharedLocalizationResources.Manufacturer]);

            RuleFor(e => e.CountryOfOrigin)
            .NotEmpty()
            .When(_ => configuration.ProductCountryOfOrigin.IsRequired)

            .MaximumLength(configuration.ProductCountryOfOrigin.MaxLength)
            .When(_ => configuration.ProductCountryOfOrigin.MaxLength > 0)
            .WithName(localizer[SharedLocalizationResources.CountryOfOrigin]);

            RuleFor(e => e.Importer)
            .NotEmpty()
            .When(_ => configuration.ProductImporter.IsRequired)

            .MaximumLength(configuration.ProductImporter.MaxLength)
            .When(_ => configuration.ProductImporter.MaxLength > 0)
            .WithName(localizer[SharedLocalizationResources.Importer]);

            RuleFor(e => e.ImporterCountry)
            .NotEmpty()
            .When(_ => configuration.ProductImporterCountry.IsRequired)

            .MaximumLength(configuration.ProductImporterCountry.MaxLength)
            .When(_ => configuration.ProductImporterCountry.MaxLength > 0)
            .WithName(localizer[SharedLocalizationResources.ImporterCountry]);

            RuleFor(e => e.ProductName)
            .NotEmpty()
            .When(_ => configuration.ProductName.IsRequired)

            .MaximumLength(configuration.ProductName.MaxLength)
            .When(_ => configuration.ProductName.MaxLength > 0)
            .WithName(localizer[SharedLocalizationResources.Name]);

            RuleFor(m => m.QuantityPerPackage)
            .NotEmpty()
            .When(_ => configuration.ProductQuantityPerPackage.IsRequired)

            .LessThan(configuration.ProductQuantityPerPackage.MaxValue)
            .When(_ => configuration.ProductQuantityPerPackage.MaxValue > 0)
            .WithName(localizer[SharedLocalizationResources.QuantityPerPackage]);;

            RuleFor(m => m.File)

            .NotNull()
            .When(_ => configuration.ProductImage.IsRequired)

            .Must(m => m.Length < configuration.ProductImage.MaxSize)
            .When(m => configuration.ProductImage.MaxSize > 0L && m != null)
            .WithMessage(localizer[SharedLocalizationResources.MaxSizeExceeded])

            .Must(m => configuration.ProductImage.SupportedFormats.Any(f => m.FileName.EndsWith(f)))
            .When(m => (configuration.ProductImage.SupportedFormats?.Any() ?? false) && m != null)
            .WithName(localizer[SharedLocalizationResources.Image])
            .WithMessage(localizer[SharedLocalizationResources.UnsupportedFormat]);

            RuleFor(e => e.Size)
            .NotEmpty()
            .When(_ => configuration.ProductSize.IsRequired)

            .MaximumLength(configuration.ProductSize.MaxLength)
            .When(_ => configuration.ProductSize.MaxLength > 0)
            .WithName(localizer[SharedLocalizationResources.Size]);
        }