Example #1
0
        private bool SlotDoorFits(string slotReference, ConfiguredProductDto childDto, ConfiguredProduct parent)
        {
            var sum = 0;

            foreach (var part in parent.Parts)
            {
                if (part.ChosenSlotReference == slotReference)
                {
                    ConfiguredProduct configuredProductPart =
                        _configuredProductRepository.GetByReference(part.ConfiguredChildReference);
                    Product  productPart = _productRepository.GetByReference(configuredProductPart.ProductReference);
                    Category category    = _categoryRepository.GetByReference(productPart.CategoryReference);
                    if (category.IsExternal)
                    {
                        sum += configuredProductPart.ConfiguredDimension.Width;
                    }
                }
            }

            foreach (var slot in parent.ConfiguredSlots)
            {
                if (slot.Reference == slotReference)
                {
                    return(slot.Size - sum >= childDto.ConfiguredDimension.Width);
                }
            }

            return(parent.ConfiguredDimension.Width - sum >= childDto.ConfiguredDimension.Width);
        }
Example #2
0
        public ValidationOutput GetAllInfoByReference(string reference)
        {
            ValidationOutput validationOutput = _configuredProductDTOValidator.DTOReferenceIsValid(reference);

            if (validationOutput.HasErrors())
            {
                return(validationOutput);
            }
            ConfiguredProduct configuredProduct = _configuredProductRepository.GetByReference(reference);

            if (configuredProduct == null)
            {
                validationOutput = new ValidationOutputNotFound();
                validationOutput.AddError("Configured Product's reference", "There are no configured products with the given reference");
                return(validationOutput);
            }

            var product = _mapper.Map <ProductOrderDto>(configuredProduct);

            foreach (var part in product.Parts)
            {
                GetAllByReference(part.ConfiguredChildReference, product);
            }
            validationOutput.DesiredReturn = product;
            return(validationOutput);
        }
Example #3
0
        public ValidationOutput GetAvailableProducts(string reference)
        {
            ValidationOutput validationOutput = _configuredProductDTOValidator.DTOReferenceIsValid(reference);

            if (validationOutput.HasErrors())
            {
                return(validationOutput);
            }
            ConfiguredProduct configuredProduct = _configuredProductRepository.GetByReference(reference);

            if (configuredProduct == null)
            {
                validationOutput = new ValidationOutputNotFound();
                validationOutput.AddError("Configured Product's reference", "There are no configured products with the given reference");
                return(validationOutput);
            }

            List <ProductDto> productsDto = (List <ProductDto>)_productService
                                            .GetPartProductsAvailableToProduct(configuredProduct.ProductReference).DesiredReturn;
            List <Product> productsAvailable = new List <Product>();

            foreach (var dto in productsDto)
            {
                productsAvailable.Add(_mapper.Map <Product>(dto));
            }
            List <ProductDto> productsAvailableFinal = new List <ProductDto>();

            foreach (var product in productsAvailable)
            {
                if (ProductFitsConfigured(product.Dimensions, configuredProduct.ConfiguredDimension))
                {
                    productsAvailableFinal.Add(_mapper.Map <ProductDto>(product));
                }
            }
            var category_products = new List <CategoryProductDto>();

            foreach (var product in productsAvailableFinal)
            {
                var categoryName       = _categoryRepository.GetByReference(product.CategoryReference).Name;
                CategoryProductDto cpd = new CategoryProductDto(categoryName);
                if (!category_products.Contains(cpd))
                {
                    cpd.Products.Add(product);
                    category_products.Add(cpd);
                }
                else
                {
                    int index = category_products.IndexOf(cpd);
                    category_products[index].Products.Add(product);
                }
            }

            validationOutput.DesiredReturn = category_products;
            return(validationOutput);
        }
Example #4
0
 private bool ChildrenWidthFits(string slotReference, ConfiguredProduct parentDto, ConfiguredProductDto childDto)
 {
     foreach (var slot in parentDto.ConfiguredSlots)
     {
         if (string.Equals(slot.Reference, slotReference, StringComparison.Ordinal))
         {
             return(slot.Size >= childDto.ConfiguredDimension.Width);
         }
     }
     return(false);
 }
Example #5
0
        private bool ChildrenHeightFits(string slotReference, ConfiguredProduct parentDto, ConfiguredProductDto childDto)
        {
            int sum = 0;

            foreach (var part in parentDto.Parts)
            {
                if (string.Equals(part.ChosenSlotReference, slotReference, StringComparison.Ordinal))
                {
                    sum += _configuredProductRepository.GetByReference(part.ConfiguredChildReference).ConfiguredDimension.Height;
                }
            }
            return(parentDto.ConfiguredDimension.Height - sum >= childDto.ConfiguredDimension.Height);
        }
Example #6
0
        private bool CheckPartAlgorithms(Product baseProduct, ConfiguredProductDto configuredProduct)
        {
            ConfiguredProduct parentConfigured = _configuredProductRepository.GetByReference(((ChildConfiguredProductDto)configuredProduct).ParentReference);
            Product           parentProduct    = _productRepository.GetByReference(parentConfigured.ProductReference);
            Part part = parentProduct.IsProductPart(baseProduct);

            if (part == null)
            {
                return(false);
            }
            foreach (var algorithm in part.Algorithms)
            {
                Dictionary <string, Object> parameters = new Dictionary <string, Object>();
                parameters.Add("ParentConfiguredProduct", parentConfigured);
                parameters.Add("ChildConfiguredProduct", configuredProduct);
                if (!algorithm.validate(parameters))
                {
                    return(false);
                }
            }
            return(true);
            // foreach (var configuredPart in configuredProduct.Parts)
            // {
            //     ConfiguredProduct childConfiguredProduct = _configuredProductRepository.GetByReference(configuredPart.ConfiguredChildReference);
            //     Product baseProductOfChildConfiguredProduct = _productRepository.GetByReference(childConfiguredProduct.ProductReference);

            //     foreach (var part in baseProduct.Parts)
            //     {
            //         if (part.ProductReference == baseProductOfChildConfiguredProduct.Reference)
            //         {
            //             foreach (var restriction in part.Algorithms)
            //             {
            //                 Dictionary<string, Object> parameters = new Dictionary<string, Object>();
            //                 parameters.Add("FatherConfiguredProduct", configuredProduct);
            //                 parameters.Add("ChildConfiguredProduct", childConfiguredProduct);
            //                 parameters.Add("FatherBaseProduct", baseProduct);
            //                 parameters.Add("ChildBaseProduct", baseProductOfChildConfiguredProduct);

            //                 if (!restriction.validate(parameters))
            //                 {
            //                     return false;
            //                 }
            //             }
            //             break;
            //         }
            //     }
            // }
        }
Example #7
0
        private void ChildrenFits(string slotReference, ConfiguredProduct parent, ConfiguredProductDto childDto, ValidationOutput validationOutput)
        {
            Product  product  = _productRepository.GetByReference(childDto.ProductReference);
            Category category = _categoryRepository.GetByReference(product.CategoryReference);

            if (!category.IsExternal)
            {
                if (!ChildrenHeightFits(slotReference, parent, childDto))
                {
                    validationOutput.AddError("Configured Product's height", "Parent Configured Product's height is insufficient!");
                }
                if (!ChildrenWidthFits(slotReference, parent, childDto))
                {
                    validationOutput.AddError("Configured Product's width", "Parent Configured Product's width is insufficient!");
                }
            }
            else if (category.IsExternal)
            {
                if (!(parent.ConfiguredDimension.Height >= childDto.ConfiguredDimension.Height))
                {
                    validationOutput.AddError("Configured Product's height", "Parent Configured Product's height is insufficient!");
                }

                if (slotReference != null)
                {
                    if (!(SlotDoorFits(slotReference, childDto, parent)))
                    {
                        validationOutput.AddError("Configured Product's width",
                                                  "Parent Configured Product's width is insufficient!");
                    }
                }
                else
                {
                    if (!(SlideDoorFits(childDto, parent)))
                    {
                        validationOutput.AddError("Configured Product's width", "Parent Configured Product's width is insufficient!");
                    }
                }
            }
            if (!ChildrenDepthFits(slotReference, parent, childDto))
            {
                validationOutput.AddError("Configured Product's depth", "Parent Configured Product's depth is insufficient!");
            }
        }
Example #8
0
        public ValidationOutput GetByReference(string reference)
        {
            ValidationOutput validationOutput = _configuredProductDTOValidator.DTOReferenceIsValid(reference);

            if (validationOutput.HasErrors())
            {
                return(validationOutput);
            }
            ConfiguredProduct configuredProduct = _configuredProductRepository.GetByReference(reference);

            if (configuredProduct == null)
            {
                validationOutput = new ValidationOutputNotFound();
                validationOutput.AddError("Configured Product's reference", "There are no configured products with the given reference");
                return(validationOutput);
            }
            validationOutput.DesiredReturn = _mapper.Map <ConfiguredProductDto>(configuredProduct);
            return(validationOutput);
        }
Example #9
0
        private bool SlideDoorFits(ConfiguredProductDto childDto, ConfiguredProduct parent)
        {
            var sum = 0;

            foreach (var part in parent.Parts)
            {
                ConfiguredProduct configuredProductPart =
                    _configuredProductRepository.GetByReference(part.ConfiguredChildReference);
                Product  productPart = _productRepository.GetByReference(configuredProductPart.ProductReference);
                Category category    = _categoryRepository.GetByReference(productPart.CategoryReference);
                if (category.IsExternal)
                {
                    sum += configuredProductPart.ConfiguredDimension.Width;
                }
            }

            if (parent.ConfiguredDimension.Width - sum >= childDto.ConfiguredDimension.Width)
            {
                return(true);
            }

            return(false);
        }
Example #10
0
        /**
         * Method that will validate and create a new configured product in the database.
         *
         * Validations performed:
         * 1. Validation of the new configured product's reference (business rules);
         * 2. Validation of the new configured product's reference (database);
         * 3. Validation of the received info. (product reference, configured material, configured dimension) (business rules)
         * 4. Validation of the received info. (origin product, origin material, parent product, slot, dimensions, restrictions) (database)
         * 5. Validation of the received info. (everything fits)
         */
        public ValidationOutput Register(ConfiguredProductDto configuredDto)
        {
            ValidationOutput validationOutput = new ValidationOutputBadRequest();

            if (configuredDto == null)
            {
                validationOutput.AddError("ConfiguredProduct", "Null configured product.");
                return(validationOutput);
            }

            ConfiguredProduct configuredProduct = _configuredProductRepository.GetByReference(configuredDto.Reference);

            if (configuredProduct != null)
            {
                validationOutput = new ValidationOutputBadRequest();
                validationOutput.AddError("Configured Product's reference", "There are already configured product with the given reference");
                return(validationOutput);
            }

            //1.
            validationOutput = _configuredProductDTOValidator.DTOReferenceIsValid(configuredDto.Reference);
            if (validationOutput.HasErrors())
            {
                return(validationOutput);
            }

            //2
            if (_configuredProductRepository.GetByReference(configuredDto.Reference) != null)
            {
                validationOutput.AddError("ConfiguredProduct's reference", "A ConfiguredProduct with the reference '" + configuredDto.Reference + "' already exists in the system!");
                return(validationOutput);
            }

            //3
            validationOutput = _configuredProductDTOValidator.DTOIsValidForRegister(configuredDto);
            if (validationOutput.HasErrors())
            {
                return(validationOutput);
            }

            ChildConfiguredProductDto ccpd = (ChildConfiguredProductDto)configuredDto;

            //4
            validationOutput = DataExists(ccpd);
            if (validationOutput.HasErrors())
            {
                return(validationOutput);
            }

            //5
            validationOutput = ValidateConfiguredProduct(ccpd);
            if (validationOutput.HasErrors())
            {
                return(validationOutput);
            }

            var configured = _mapper.Map <ConfiguredProduct>(configuredDto);

            if (_productRepository.GetByReference(configured.ProductReference).SlotDefinition == null || !configured.ConfiguredSlots.Any())
            {
                configured.ConfiguredSlots.Clear();
                configured.ConfiguredSlots.Add(new ConfiguredSlot(configured.ConfiguredDimension.Width, "default_slot_reference"));
            }

            configured.Price = new Price(CalculatePriceValue(((Price[])validationOutput.DesiredReturn)[2], ((Price[])validationOutput.DesiredReturn)[1], ((Price[])validationOutput.DesiredReturn)[0], configured.ConfiguredDimension));

            if (ccpd.ParentReference != null)
            {
                var parentConfigured = _configuredProductRepository.GetByReference(ccpd.ParentReference);
                parentConfigured.Parts.Add(new ConfiguredPart(ccpd.Reference, ccpd.SlotReference));
                parentConfigured.Price.Value += configured.Price.Value;
                _configuredProductRepository.Update(parentConfigured);
            }

            validationOutput.DesiredReturn = _mapper.Map <ConfiguredProductDto>(_configuredProductRepository.Add(configured));

            return(validationOutput);
        }
Example #11
0
        private ValidationOutput DataExists(ChildConfiguredProductDto dto)
        {
            ValidationOutput validationOutput = new ValidationOutputNotFound();
            Product          product          = _productRepository.GetByReference(dto.ProductReference);

            if (product == null)
            {
                validationOutput.AddError("Origin Product", "There are no product with the given reference!");
                return(validationOutput);
            }
            Category category = _categoryRepository.GetByReference(product.CategoryReference);

            if (_materialRepository.GetByReference(dto.ConfiguredMaterial.OriginMaterialReference) == null)
            {
                validationOutput.AddError("Origin Material", "There are no material with the given reference!");
                return(validationOutput);
            }

            if (dto.ParentReference != null)
            {
                if (_configuredProductRepository.GetByReference(dto.ParentReference) == null)
                {
                    validationOutput.AddError("Parent Configured Product", "There are no configured product with the given parent reference");
                    return(validationOutput);
                }
                ConfiguredProduct cpd = _configuredProductRepository.GetByReference(dto.ParentReference);
                validationOutput = new ValidationOutputBadRequest();
                if (category.IsExternal && dto.SlotReference == null)
                {
                    var parentProduct = _productRepository.GetByReference(cpd.ProductReference);
                    foreach (var part in parentProduct.Parts)
                    {
                        if (string.Equals(part.ProductReference, dto.ProductReference))
                        {
                            return(validationOutput);
                        }
                    }
                    validationOutput.AddError("Product Reference", "Parent Product does not support this product as child!");
                    return(validationOutput);
                }
                if ((category.IsExternal && dto.SlotReference != null) || (!category.IsExternal))
                {
                    foreach (var slot in cpd.ConfiguredSlots)
                    {
                        if (string.Equals(slot.Reference, dto.SlotReference, StringComparison.Ordinal))
                        {
                            var parentProduct = _productRepository.GetByReference(cpd.ProductReference);
                            foreach (var part in parentProduct.Parts)
                            {
                                if (string.Equals(part.ProductReference, dto.ProductReference))
                                {
                                    return(validationOutput);
                                }
                            }

                            validationOutput.AddError("Product Reference",
                                                      "Parent Product does not support this product as child!");
                            return(validationOutput);
                        }
                    }
                }
                validationOutput.AddError("Slot Reference", "The selected slot reference does not exists in parent configured product");
                return(validationOutput);
            }
            return(validationOutput);
        }
Example #12
0
 private bool ChildrenDepthFits(string slotReference, ConfiguredProduct parentDto, ConfiguredProductDto childDto)
 {
     return(parentDto.ConfiguredDimension.Depth >= childDto.ConfiguredDimension.Depth);
 }