예제 #1
0
        public virtual AssociationEntity FromModel(ProductAssociation association)
        {
            if (association == null)
            {
                throw new ArgumentNullException(nameof(association));
            }

            Id              = association.Id;
            Priority        = association.Priority;
            AssociationType = association.Type;
            Quantity        = association.Quantity;
            ItemId          = association.ItemId;

            if (association.AssociatedObjectType.EqualsInvariant("product"))
            {
                AssociatedItemId = association.AssociatedObjectId;
            }
            else if (association.AssociatedObjectType.EqualsInvariant("category"))
            {
                AssociatedCategoryId = association.AssociatedObjectId;
            }

            if (!association.Tags.IsNullOrEmpty())
            {
                Tags = string.Join(";", association.Tags);
            }

            return(this);
        }
예제 #2
0
        public virtual Association ToAssociation(catalogDto.ProductAssociation associationDto)
        {
            Association result = null;

            if (associationDto.AssociatedObjectType.EqualsInvariant("product"))
            {
                result = new ProductAssociation
                {
                    ProductId = associationDto.AssociatedObjectId
                };
            }
            else if (associationDto.AssociatedObjectType.EqualsInvariant("category"))
            {
                result = new CategoryAssociation
                {
                    CategoryId = associationDto.AssociatedObjectId
                };
            }

            if (result != null)
            {
                result.Type     = associationDto.Type;
                result.Priority = associationDto.Priority ?? 0;
                result.Image    = new Image {
                    Url = associationDto.AssociatedObjectImg
                };
                result.Quantity = associationDto.Quantity;
            }

            return(result);
        }
예제 #3
0
        public virtual ProductAssociation ToModel(ProductAssociation association)
        {
            if (association == null)
            {
                throw new ArgumentNullException(nameof(association));
            }

            association.Id                 = Id;
            association.OuterId            = OuterId;
            association.Type               = AssociationType;
            association.Priority           = Priority;
            association.AssociatedObjectId = AssociatedItemId ?? AssociatedCategoryId;
            association.Quantity           = Quantity;
            association.ItemId             = ItemId;
            if (AssociatedCategory != null)
            {
                association.AssociatedObject     = AssociatedCategory.ToModel(AbstractTypeFactory <Category> .TryCreateInstance());
                association.AssociatedObjectType = "category";
            }

            if (AssociatedItem != null)
            {
                association.AssociatedObject     = AssociatedItem.ToModel(AbstractTypeFactory <CatalogProduct> .TryCreateInstance(), false, false);
                association.AssociatedObjectType = "product";
            }

            if (!Tags.IsNullOrEmpty())
            {
                association.Tags = Tags.Split(';');
            }

            return(association);
        }
예제 #4
0
        public virtual ProductAssociation ToReferencedAssociationModel(ProductAssociation association)
        {
            if (association == null)
            {
                throw new ArgumentNullException(nameof(association));
            }

            association.Type               = this.AssociationType;
            association.Priority           = this.Priority;
            association.AssociatedObjectId = this.ItemId;
            association.Quantity           = this.Quantity;

            if (this.Item != null)
            {
                association.AssociatedObject     = this.Item.ToModel(AbstractTypeFactory <CatalogProduct> .TryCreateInstance(), false, false);
                association.AssociatedObjectType = "product";
            }

            if (!this.Tags.IsNullOrEmpty())
            {
                association.Tags = this.Tags.Split(';');
            }

            return(association);
        }
예제 #5
0
        public static Association ToWebModel(this catalogModel.ProductAssociation association)
        {
            Association retVal = null;

            if (association.AssociatedObjectType.EqualsInvariant("product"))
            {
                retVal = new ProductAssociation
                {
                    ProductId = association.AssociatedObjectId
                };
            }
            else if (association.AssociatedObjectType.EqualsInvariant("category"))
            {
                retVal = new CategoryAssociation
                {
                    CategoryId = association.AssociatedObjectId
                };
            }

            if (retVal != null)
            {
                retVal.InjectFrom <NullableAndEnumValueInjecter>(association);
                retVal.Image = new Image {
                    Url = association.AssociatedObjectImg
                };
            }

            return(retVal);
        }
예제 #6
0
        public static Association ToWebModel(this ProductAssociation source)
        {
            var retVal = new Association
            {
                ItemId = source.AssociatedProductId
            };

            retVal.InjectFrom(source);

            return(retVal);
        }
예제 #7
0
        /// <summary>
        /// Create and configure the organization service proxy.
        /// Initiate creating all entity records that this sample requires.
        /// Create a bundle record.
        /// Add products to a bundle.
        /// Optionally delete any entity records that were created for this sample.
        /// </summary>
        /// <param name="serverConfig">Contains server connection information.</param>
        /// <param name="promptforDelete">When True, the user will be prompted to delete all
        /// created entities.</param>
        public void Run(ServerConnection.Configuration serverConfig, bool promptForDelete)
        {
            try
            {
                // Connect to the Organization service.
                // The using statement assures that the service proxy will be properly disposed.
                using (_serviceProxy = ServerConnection.GetOrganizationProxy(serverConfig))
                {
                    // This statement is required to enable early-bound type support.
                    _serviceProxy.EnableProxyTypes();

                    // Call the method to create any data that this sample requires.
                    CreateRequiredRecords();

                    //<snippetAddProductstoBundle1>
                    // Add products to a bundle
                    ProductAssociation newAssociation1 = new ProductAssociation
                    {
                        AssociatedProduct = new EntityReference(Product.EntityLogicalName, _product1Id),
                        ProductId         = new EntityReference(Product.EntityLogicalName, _bundleId),
                        Quantity          = new decimal(15),
                        ProductIsRequired = new OptionSetValue(0), // Adding this as an optional product
                        UoMId             = new EntityReference(UoM.EntityLogicalName, unit.Id)
                    };
                    _product1AssociationId = _serviceProxy.Create(newAssociation1);

                    ProductAssociation newAssociation2 = new ProductAssociation
                    {
                        AssociatedProduct = new EntityReference(Product.EntityLogicalName, _product2Id),
                        ProductId         = new EntityReference(Product.EntityLogicalName, _bundleId),
                        Quantity          = new decimal(20),
                        ProductIsRequired = new OptionSetValue(1), // Adding this as a mandatory product
                        UoMId             = new EntityReference(UoM.EntityLogicalName, unit.Id),
                    };
                    _product2AssociationId = _serviceProxy.Create(newAssociation2);

                    // Verify if the product association is created
                    if ((_product1AssociationId != null) && (_product1AssociationId != null))
                    {
                        Console.WriteLine("\nAdded both the products to the bundle");
                    }

                    //</snippetAddProductstoBundle1>

                    DeleteRequiredRecords(promptForDelete);
                }
            }
            catch
            {
                // You can handle an exception here or pass it back to the calling method.
                throw;
            }
        }
 public static ProductAssociation ToProductAssociation(this catalogDto.ProductAssociation associationDto)
 {
     var result = new ProductAssociation
     {
         Type = associationDto.Type,
         ProductId = associationDto.AssociatedObjectId,
         Priority = associationDto.Priority ?? 0,
         Quantity = associationDto.Quantity,
         Tags = associationDto.Tags
     };
     return result;
 }
        public static ProductAssociation ToWebModel(this VirtoCommerceCatalogModuleWebModelProductAssociation association)
        {
            var retVal = new ProductAssociation();

            retVal.InjectFrom <NullableAndEnumValueInjecter>(association);
            retVal.ProductImage = new Image()
            {
                Url = association.ProductImg
            };
            retVal.ProductName = association.ProductName;
            retVal.ProductSku  = association.ProductCode;
            return(retVal);
        }
        public void ValidateAssociation_ValidItem_Success()
        {
            var validator   = new ProductAssociationValidator();
            var association = new ProductAssociation()
            {
                ItemId               = "ItemId",
                AssociatedObjectId   = "AssociatedObjectId",
                AssociatedObjectType = "AssociatedObjectType"
            };

            //Act

            var result = validator.Validate(association);

            //Assert
            result.IsValid.Should().BeTrue();
            result.Errors.Should().BeEmpty();
        }
        public async Task UpdateAssociationsAsync_AddNewAssociation_Added(dataModel.AssociationEntity entity)
        {
            // Arrange
            var associationServiceMock = CreateProductAssociationServiceMock(new[] { entity });

            var productAssociation = new ProductAssociation()
            {
                Id                   = null,
                ItemId               = entity.ItemId,
                AssociatedObjectId   = "NEW_AssociatedItemId",
                AssociatedObjectType = entity.AssociationType,
                Type                 = entity.AssociationType,
                Priority             = 10
            };
            // Act
            await associationServiceMock.UpdateAssociationsAsync(new[] { productAssociation });

            // Assert
            _catalogRepositoryMock.Verify(x => x.Add(It.Is <dataModel.AssociationEntity>(q => q.AssociatedItemId == "NEW_AssociatedItemId")), Times.Once);
        }
        public async Task UpdateAssociationsAsync_UpdateNotTransientAssociation_Changed(dataModel.AssociationEntity entity)
        {
            // Arrange
            var associationServiceMock = CreateProductAssociationServiceMock(new [] { entity });

            var productAssociation = new ProductAssociation()
            {
                Id                   = entity.Id,
                ItemId               = "new_Item_ID",
                AssociatedObjectId   = "new_object_Id",
                AssociatedObjectType = entity.AssociationType,
            };
            // Act

            await associationServiceMock.UpdateAssociationsAsync(new[] { productAssociation });

            // Assert
            Assert.Equal(productAssociation.ItemId, entity.ItemId);
            _catalogRepositoryMock.Verify(x => x.Add(It.IsAny <dataModel.AssociationEntity>()), Times.Never);
        }
        public void ValidateAssociation_NotValidItem_Failed()
        {
            var validator   = new ProductAssociationValidator();
            var association = new ProductAssociation();

            //Act

            var result = validator.Validate(association);

            //Assert
            result.IsValid.Should().BeFalse();
            result.Errors.Should().HaveCount(6);

            result.Errors.Should().Contain(x => x.PropertyName == "ItemId" && x.ErrorCode == "NotNullValidator");
            result.Errors.Should().Contain(x => x.PropertyName == "ItemId" && x.ErrorCode == "NotEmptyValidator");

            result.Errors.Should().Contain(x => x.PropertyName == "AssociatedObjectId" && x.ErrorCode == "NotNullValidator");
            result.Errors.Should().Contain(x => x.PropertyName == "AssociatedObjectId" && x.ErrorCode == "NotEmptyValidator");

            result.Errors.Should().Contain(x => x.PropertyName == "AssociatedObjectType" && x.ErrorCode == "NotNullValidator");
            result.Errors.Should().Contain(x => x.PropertyName == "AssociatedObjectType" && x.ErrorCode == "NotEmptyValidator");
        }
        public async Task UpdateAssociationsAsync_UpdateTransientAssociation_Changed(dataModel.AssociationEntity entity)
        {
            // Arrange
            var associationServiceMock = CreateProductAssociationServiceMock(new[] { entity });

            var productAssociation = new ProductAssociation
            {
                Id                   = null,
                ItemId               = entity.ItemId,
                AssociatedObjectId   = entity.AssociatedItemId,
                AssociatedObjectType = entity.AssociationType,
                Type                 = entity.AssociationType,
                Priority             = 10
            };

            // Act
            await associationServiceMock.UpdateAssociationsAsync(new[] { productAssociation });

            // Assert
            Assert.Equal(productAssociation.Priority, entity.Priority);
            _catalogRepositoryMock.Verify(x => x.Add(It.IsAny <dataModel.AssociationEntity>()), Times.Never);
        }
예제 #15
0
        /// <summary>
        /// Create and configure the organization service proxy.
        /// Initiate creating all entity records that this sample requires.
        /// Create a bundle record.
        /// Add products to a bundle. 
        /// Optionally delete any entity records that were created for this sample.
        /// </summary>
        /// <param name="serverConfig">Contains server connection information.</param>
        /// <param name="promptforDelete">When True, the user will be prompted to delete all
        /// created entities.</param>
        public void Run(ServerConnection.Configuration serverConfig, bool promptForDelete)
        {
            try
            {
                // Connect to the Organization service. 
                // The using statement assures that the service proxy will be properly disposed.
                using (_serviceProxy = ServerConnection.GetOrganizationProxy(serverConfig))
                {
                    // This statement is required to enable early-bound type support.
                    _serviceProxy.EnableProxyTypes();

                    // Call the method to create any data that this sample requires.
                    CreateRequiredRecords();

                    //<snippetAddProductstoBundle1>
                    // Add products to a bundle
                    ProductAssociation newAssociation1 = new ProductAssociation
                    {
                        AssociatedProduct = new EntityReference(Product.EntityLogicalName, _product1Id),
                        ProductId = new EntityReference(Product.EntityLogicalName, _bundleId),
                        Quantity = new decimal(15),
                        ProductIsRequired = new OptionSetValue(0), // Adding this as an optional product
                        UoMId = new EntityReference(UoM.EntityLogicalName, unit.Id)
                    };
                    _product1AssociationId = _serviceProxy.Create(newAssociation1);                    
                    
                    ProductAssociation newAssociation2 = new ProductAssociation
                    {
                        AssociatedProduct = new EntityReference(Product.EntityLogicalName, _product2Id),
                        ProductId = new EntityReference(Product.EntityLogicalName, _bundleId),
                        Quantity = new decimal(20),
                        ProductIsRequired = new OptionSetValue(1), // Adding this as a mandatory product
                        UoMId = new EntityReference(UoM.EntityLogicalName, unit.Id),                        
                    };
                    _product2AssociationId = _serviceProxy.Create(newAssociation2);

                    // Verify if the product association is created
                    if ((_product1AssociationId != null) && (_product1AssociationId != null))
                    {
                        Console.WriteLine("\nAdded both the products to the bundle");
                    }                        
                    
                    //</snippetAddProductstoBundle1>                   

                    DeleteRequiredRecords(promptForDelete);
                }
            }
            catch
            {
                // You can handle an exception here or pass it back to the calling method.
                throw;
            }
        }