Example #1
0
        // ============ Methods to CREATE something ============

        /**
         * Method that will validate and create a new material in the database.
         *
         * Validations performed:
         * 1. Validation of the new material's reference (business rules);
         * 2. Validation of the new material's reference (database);
         * 3. Validation of the received info. (name, description, colors, finishes) (business rules)
         */
        public ValidationOutput Register(MaterialDto dto)
        {
            //1.
            ValidationOutput validationOutput = _materialDTOValidator.DTOReferenceIsValid(dto.Reference);

            if (validationOutput.HasErrors())
            {
                return(validationOutput);
            }

            //2.
            validationOutput = new ValidationOutputBadRequest();
            if (MaterialExists(dto.Reference))
            {
                validationOutput.AddError("Reference of material",
                                          "A material with the reference '" + dto.Reference + "' already exists in the system!");
                return(validationOutput);
            }

            //3.
            validationOutput = _materialDTOValidator.DTOIsValidForRegister(dto);
            if (validationOutput.HasErrors())
            {
                return(validationOutput);
            }

            if (dto.Colors.Count > 0)
            {
                validationOutput = PrivateAddColorsWithMaterial(dto.Colors);
                if (validationOutput.HasErrors())
                {
                    return(validationOutput);
                }
            }

            if (dto.Finishes.Count > 0)
            {
                validationOutput = PrivateAddFinishesWithMaterial(dto.Finishes);
                if (validationOutput.HasErrors())
                {
                    return(validationOutput);
                }
            }

            //NOTA: Ainda que este método verifique se o atributo Price é != null, nós, aqui no Register, nunca deixamos que seja null devido às validações
            AddNewPriceToMaterialHistory(dto);

            foreach (var finish in dto.Finishes)
            {
                finish.IsActive = true;
            }

            //If we reached here then that means we can add the new material
            validationOutput.DesiredReturn =
                _mapper.Map <MaterialDto>(
                    _materialRepository.Add(_mapper.Map <Material>(dto)));
            return(validationOutput);
        }
Example #2
0
        /**
         * Validations performed:
         *
         * 1. The received list has 1 or more elements.
         * FOREACH RECEIVED Color{
         * 2. Validation of each color's definition (business rules);
         * 3. Validation of the existence of each Color received, in the material with the passed reference
         * 4. Validation for duplication between received colors
         * }
         */
        private ValidationOutput PrivateAddColorsWithMaterial(IEnumerable <ColorDto> enumerableColorDto)
        {
            List <ColorDto>
            listColorDto =
                new List <ColorDto>(
                    enumerableColorDto);     //Since we receive an IEnumerable, we need to have something concrete

            //1.
            ValidationOutput validationOutput = new ValidationOutputBadRequest();

            if (listColorDto.Count == 0)
            {
                validationOutput.AddError("Selected colors", "No colors were selected!");
                return(validationOutput);
            }

            List <ColorDto> colorsToAdd = new List <ColorDto>();

            foreach (var currentColorDto in listColorDto)
            {
                validationOutput = new ValidationOutputBadRequest();

                //2.
                validationOutput = _colorDTOValidator.DTOReferenceIsValid(currentColorDto.HexCode);
                if (validationOutput.HasErrors())
                {
                    return(validationOutput);
                }

                validationOutput = _colorDTOValidator.DTOIsValid(currentColorDto);
                if (validationOutput.HasErrors())
                {
                    return(validationOutput);
                }

                //3.
                validationOutput = _colorDTOValidator.DTOIsValidForRegister(currentColorDto);
                if (validationOutput.HasErrors())
                {
                    return(validationOutput);
                }

                //4.
                if (colorsToAdd.Contains(currentColorDto))
                {
                    validationOutput.AddError("Color",
                                              "Color with the hex code '" + currentColorDto.HexCode +
                                              "' is duplicated in the list of selected colors.");
                    return(validationOutput);
                }

                colorsToAdd.Add(currentColorDto);
            }

            return(validationOutput);
        }
Example #3
0
        /**
         * Validations performed:
         *
         * 1. The received list has 1 or more elements.
         * FOREACH FINISH RECEIVED {
         * 2. Validation of each finish's reference (business rules);
         * 3. Validation of each finish's definition (business rules);
         * 4. Validation of the existence of each finish received, in the material with the passed reference.
         * 5. Validation for duplication between received finishes.
         * }
         */
        private ValidationOutput PrivateAddFinishesWithMaterial(IEnumerable <FinishDto> enumerableFinishDto)
        {
            List <FinishDto>
            listFinishDto =
                new List <FinishDto>(
                    enumerableFinishDto);     //Since we receive an IEnumerable, we need to have something concrete

            //1.
            ValidationOutput validationOutput = new ValidationOutputBadRequest();

            if (listFinishDto.Count == 0)
            {
                validationOutput.AddError("Finishes selected", "No finishes were selected!");
                return(validationOutput);
            }

            List <FinishDto> finishesToAdd = new List <FinishDto>();

            foreach (var currentFinishDto in listFinishDto)
            {
                validationOutput = new ValidationOutputBadRequest();

                //2.
                validationOutput = _finishDTOValidator.DTOReferenceIsValid(currentFinishDto.Reference);
                if (validationOutput.HasErrors())
                {
                    return(validationOutput);
                }

                //3.
                validationOutput = _finishDTOValidator.DTOIsValidForRegister(currentFinishDto);
                if (validationOutput.HasErrors())
                {
                    return(validationOutput);
                }

                //5.
                if (finishesToAdd.Contains(currentFinishDto))
                {
                    validationOutput.AddError("Finish",
                                              "Finish with the reference '" + currentFinishDto.Reference +
                                              "' is duplicated in the list of selected finishes.");
                    return(validationOutput);
                }

                AddNewPriceToFinishHistory(currentFinishDto);
                finishesToAdd.Add(currentFinishDto);
            }

            return(validationOutput);
        }
        public override ValidationOutput DTOIsValid(ModelGroupDto consideredDto)
        {
            ValidationOutput validationOutput = new ValidationOutputBadRequest();

            if (!RelativeURLIsValid(consideredDto.RelativeURL))
            {
                validationOutput.AddError("RelativeURL", "URL to OBJ file is invalid!");
                return(validationOutput);
            }
            var components = consideredDto.Components;

            if (!(components.Count > 0))
            {
                validationOutput.AddError("Components", "There are no components");
                return(validationOutput);
            }
            foreach (var component in components)
            {
                validationOutput = _componentDTOValidator.DTOIsValid(component);
                if (validationOutput.HasErrors())
                {
                    return(validationOutput);
                }
            }
            return(validationOutput);
        }
Example #5
0
        private ValidationOutput ValidateConfiguredProduct(ChildConfiguredProductDto configuredDto)
        {
            ValidationOutput validationOutput = new ValidationOutputBadRequest();
            var product = _productRepository.GetByReference(configuredDto.ProductReference);

            if (!product.ChosenDimensionDtoIsValid(configuredDto.ConfiguredDimension))
            {
                validationOutput.AddError("Configured Product's dimension", "Chosen Dimension does not belong to any possible dimension");
                return(validationOutput);
            }

            if (!product.ConfiguredMaterialDtoExists(configuredDto.ConfiguredMaterial))
            {
                validationOutput.AddError("Configured Material's reference", "Product does not support the chosen material");
                return(validationOutput);
            }

            ValidateMaterial(configuredDto, validationOutput);
            if (validationOutput.HasErrors())
            {
                return(validationOutput);
            }

            ValidateSlotDefinition(product, configuredDto, validationOutput);
            if (validationOutput.HasErrors())
            {
                return(validationOutput);
            }

            var parentConfiguredProduct = _configuredProductRepository.GetByReference(configuredDto.ParentReference);

            if (parentConfiguredProduct != null)
            {
                ChildrenFits(configuredDto.SlotReference, parentConfiguredProduct, configuredDto, validationOutput);
                if (validationOutput.HasErrors())
                {
                    return(validationOutput);
                }
            }

            if (!ObeysRestrictions(configuredDto))
            {
                validationOutput.AddError("Restriction", "Configured Product does not obey to defined restrictions!");
            }
            validationOutput.DesiredReturn = new Price[] { product.Price, ((Price[])validationOutput.DesiredReturn)[1], ((Price[])validationOutput.DesiredReturn)[0] };
            return(validationOutput);
        }
Example #6
0
        public override ValidationOutput DTOIsValid(DimensionValuesDto consideredDto)
        {
            ValidationOutput validationOutput = new ValidationOutputBadRequest();

            if (string.IsNullOrEmpty(consideredDto.Reference))
            {
                validationOutput.AddError("Dimension's Reference", "There is no reference.");
                return(validationOutput);
            }

            //================== PossibleHeights attribute ==================
            if (consideredDto.PossibleHeights == null || consideredDto.PossibleHeights.Count <= 0)
            {
                validationOutput.AddError("Dimension's heights", "There are no heights.");
                return(validationOutput);
            }

            validationOutput = this.PossibleHeightsIsValid(consideredDto);
            if (validationOutput.HasErrors())
            {
                return(validationOutput);
            }

            //================== PossibleWidths attribute ==================
            if (consideredDto.PossibleWidths == null || consideredDto.PossibleWidths.Count <= 0)
            {
                validationOutput.AddError("Dimension's widths", "There are no widths.");
                return(validationOutput);
            }

            validationOutput = this.PossibleWidthsIsValid(consideredDto);
            if (validationOutput.HasErrors())
            {
                return(validationOutput);
            }

            //================== PossibleDepths attribute ==================
            if (consideredDto.PossibleDepths == null || consideredDto.PossibleDepths.Count <= 0)
            {
                validationOutput.AddError("Dimension's depths", "There are no depths");
                return(validationOutput);
            }
            validationOutput = this.PossibleDepthsIsValid(consideredDto);

            return(validationOutput);
        }
Example #7
0
        public override ValidationOutput DTOIsValidForUpdate(ProductDto consideredDto)
        {
            ValidationOutput validationOutput = new ValidationOutputBadRequest();

            if (consideredDto.Name != null)
            {
                if (!NameIsValid(consideredDto.Name))
                {
                    validationOutput.AddError("Name of product", "New name '" + consideredDto.Name + "' is not valid!");
                }
            }

            if (consideredDto.Description != null)
            {
                if (!DescriptionIsValid(consideredDto.Description))
                {
                    validationOutput.AddError("Description of product",
                                              "New description '" + consideredDto.Description + "' is not valid!");
                }
            }

            if (consideredDto.Price != null)
            {
                ValidationOutput priceDTOValidationOutput = _priceDTOValidator.DTOIsValid(consideredDto.Price);
                if (priceDTOValidationOutput.HasErrors())
                {
                    priceDTOValidationOutput.AppendToAllkeys("Product '" + consideredDto.Reference + "' > ");
                    validationOutput.Join(priceDTOValidationOutput);
                }
            }

            if (consideredDto.SlotDefinition != null)
            {
                ValidationOutput slotDefinitionDTOValidationOutput =
                    _slotDefinitionDTOValidator.DTOIsValid(consideredDto.SlotDefinition);
                if (slotDefinitionDTOValidationOutput.HasErrors())
                {
                    slotDefinitionDTOValidationOutput.AppendToAllkeys("Product '" + consideredDto.Reference + "' > ");
                    validationOutput.Join(slotDefinitionDTOValidationOutput);
                }
            }

            var modelGroup = consideredDto.ModelGroup;

            if (modelGroup != null)
            {
                ValidationOutput modelGroupValidationOutput = _modelGroupDTOValidator.DTOIsValid(modelGroup);
                if (validationOutput.HasErrors())
                {
                    validationOutput.Join(modelGroupValidationOutput);
                }
            }

            return(validationOutput);
        }
        public override ValidationOutput DTOIsValid(ValuesDto consideredDto)
        {
            ValidationOutput validationOutput = new ValidationOutputBadRequest();

            if (consideredDto is DiscreteValueDto)
            {
                validationOutput = _discreteValueDTOValidator.DTOIsValid((DiscreteValueDto)consideredDto);
                if (validationOutput.HasErrors())
                {
                    return(validationOutput);
                }
            }

            if (consideredDto is ContinuousValueDto)
            {
                validationOutput = _continuousValueDTOValidator.DTOIsValid((ContinuousValueDto)consideredDto);
                if (validationOutput.HasErrors())
                {
                    return(validationOutput);
                }
            }
            return(validationOutput);
        }
        // ============ Methods to CREATE something ============

        /**
         * Method that will validate and create a new category in the database.
         *
         * Validations performed:
         * 1. Validation of the new category's reference (business rules);
         * 2. Validation of the new category's reference (database);
         * 3. Validation of the received info. (name, description, parent category) (business rules)
         * 4. Validation of the category's parent category reference (database)
         */
        public ValidationOutput Register(CategoryDto dto)
        {
            //1.
            ValidationOutput validationOutput = _categoryDTOValidator.DTOReferenceIsValid(dto.Reference);

            if (validationOutput.HasErrors())
            {
                return(validationOutput);
            }

            //2.
            validationOutput = new ValidationOutputBadRequest();
            if (Exists(dto.Reference))
            {
                validationOutput.AddError("Category's reference",
                                          "A category with the reference '" + dto.Reference + "' already exists in the system!");
                return(validationOutput);
            }

            //3.
            validationOutput = _categoryDTOValidator.DTOIsValidForRegister(dto);
            if (validationOutput.HasErrors())
            {
                return(validationOutput);
            }

            //4.
            validationOutput = new ValidationOutputNotFound();
            if (!string.IsNullOrEmpty(dto.ParentCategoryReference))
            {
                if (!Exists(dto.ParentCategoryReference))
                {
                    validationOutput.AddError("Category's parent category reference",
                                              "No category with the reference '" + dto.ParentCategoryReference + "' exists in the system.");
                    return(validationOutput);
                }
            }

            //If we reached here than that means we can add the new category
            Category passedCategory = _mapper.Map <Category>(dto);

            validationOutput.DesiredReturn = _mapper.Map <CategoryDto>(_categoryRepository.Add(passedCategory));

            return(validationOutput);
        }
Example #10
0
        // ============ Methods to CREATE something ============

        /**
         * Method that will validate and create a new catalog in the database.
         *
         * Validations performed:
         * 1. Validation of the new catalog's reference (business rules);
         * 2. Validation of the new catalog's reference (database);
         * 3. Validation of the received info. (name, description) (business rules)
         * 4. The received CatalogProductCollection list has 1 or more elements.
         * FOREACH RECEIVED CatalogProductCollection {
         * 5. Validation of the collection's reference in ProductCollection of current CatalogProductCollection (database);
         * 6. Validation of the configured product's reference in ProductCollection of current CatalogProductCollection (database);
         * 7. Validation to assert whether the indicated configured product actually belongs to the indicated collection (ProductCollection in current CatalogProductCollection)
         * 8. Validation for duplication between each CatalogProductCollection
         * }
         */
        public ValidationOutput Register(CatalogDto dto)
        {
            //1.
            ValidationOutput validationOutput = _catalogDTOValidator.DTOReferenceIsValid(dto.Reference);

            if (validationOutput.HasErrors())
            {
                return(validationOutput);
            }

            //2.
            validationOutput = new ValidationOutputBadRequest();
            if (CatalogExists(dto.Reference))
            {
                validationOutput.AddError("Reference of catalog", "A catalog with the reference '" + dto.Reference + "' already exists in the system!");
                return(validationOutput);
            }

            //3.
            validationOutput = _catalogDTOValidator.DTOIsValidForRegister(dto);
            if (validationOutput.HasErrors())
            {
                return(validationOutput);
            }

            //4.
            validationOutput = new ValidationOutputBadRequest();

            /*if (dto.CatalogProductCollectionList.Count == 0)
             * {
             *  validationOutput.AddError("Selected 'configured product - collection' items", "No 'configured product - collection' items were selected!");
             *  return validationOutput;
             * }*/
            if (dto.CatalogProductCollectionList.Count > 0)
            {
                List <ProductCollection> productCollectionListToAdd = new List <ProductCollection>();

                foreach (var currentCatalogProductCollectionDto in dto.CatalogProductCollectionList)
                {
                    ProductCollectionDto
                             productCollectionDto =
                        currentCatalogProductCollectionDto.ProductCollection;     //Just to simplify the code

                    //5.
                    validationOutput = new ValidationOutputNotFound();
                    if (!CollectionExists(productCollectionDto.CollectionReference))
                    {
                        validationOutput.AddError("Reference of collection of a 'configured product - collection' item",
                                                  "No collection with the reference '" + productCollectionDto.CollectionReference +
                                                  "' exists in the system.");
                        return(validationOutput);
                    }

                    //6.
                    if (!ConfiguredProductExists(productCollectionDto.ConfiguredProductReference))
                    {
                        validationOutput.AddError(
                            "Reference of configured product of a 'configured product - collection' item",
                            "No configured product with the reference '" +
                            productCollectionDto.ConfiguredProductReference + "' exists in the system.");
                        return(validationOutput);
                    }

                    Collection currentCollection =
                        _collectionRepository.GetByReference(productCollectionDto.CollectionReference);

                    //7.
                    validationOutput = new ValidationOutputBadRequest();
                    if (!currentCollection.ConfiguredProductIsInCollection(productCollectionDto
                                                                           .ConfiguredProductReference))
                    {
                        validationOutput.AddError("'Configured product - collection' item",
                                                  "The configured product with reference '" +
                                                  productCollectionDto.ConfiguredProductReference +
                                                  "' does not belong to the collection with reference '" +
                                                  productCollectionDto.ConfiguredProductReference + "'.");
                        return(validationOutput);
                    }

                    ProductCollection currentProdCollection = _mapper.Map <ProductCollection>(productCollectionDto);

                    //8.
                    if (productCollectionListToAdd.Contains(currentProdCollection))
                    {
                        validationOutput.AddError("'Configured product - collection' item",
                                                  "'Configured product - collection' item with configured product '" +
                                                  currentProdCollection.ConfiguredProductReference +
                                                  "' that is associated with the collection '" + currentProdCollection.CollectionReference +
                                                  "' is duplicated in the list of selected 'configured product - collection' items!");
                        return(validationOutput);
                    }

                    productCollectionListToAdd.Add(currentProdCollection);
                }
            }

            Catalog catalogToRegister = _mapper.Map <Catalog>(dto);

            validationOutput.DesiredReturn = _mapper.Map <CatalogDto>(_catalogRepository.Add(catalogToRegister));

            return(validationOutput);
        }
Example #11
0
        /**
         * Method that will add prices that the finish with the passed reference in the material with, equally, the passed reference will have, in the future.
         * It is assumed that a list with 1 or more objects is received.
         *
         * Validations performed:
         * 1. Validation of the passed material's reference (database);
         * 2. The received list has 1 or more elements.
         * 3. Validation of the passed finish's reference (existence in the material);
         * FOREACH PRICE HISTORY RECEIVED {
         * 4. Validation of each price history's definition (business rules);
         * 6. Validation of the existence of each price history received, in the the finish of material with the passed reference.
         * 5. Validation for duplication between received price history items.
         * }
         */
        public ValidationOutput AddPriceHistoryItemsToFinishOfMaterial(string materialReference, string finishReference,
                                                                       IEnumerable <PriceHistoryDto> enumerablePriceHistoryDto)
        {
            ValidationOutput validationOutput = new ValidationOutputBadRequest();

            List <PriceHistoryDto> listPriceHistoryDto = new List <PriceHistoryDto>(enumerablePriceHistoryDto);

            //1.
            validationOutput = new ValidationOutputNotFound();
            if (!MaterialExists(materialReference))
            {
                validationOutput.AddError("Reference of material",
                                          "No material with the reference '" + materialReference + "' exists in the system.");
                return(validationOutput);
            }

            //2.
            if (listPriceHistoryDto.Count == 0)
            {
                validationOutput.AddError("Price history items defined", "No price history items were defined!");
                return(validationOutput);
            }

            Material materialToModify = _materialRepository.GetByReference(materialReference);

            //3.
            validationOutput = new ValidationOutputNotFound();
            if (!materialToModify.ContainsFinish(finishReference))
            {
                validationOutput.AddError("Reference of finish",
                                          "No finish with the reference '" + finishReference + "' exists in the material '" +
                                          materialReference + "'.");
                return(validationOutput);
            }

            Finish finishToModify = materialToModify.GetFinish(finishReference);

            List <PriceHistory> priceHistoryItemsToAdd = new List <PriceHistory>();

            validationOutput = new ValidationOutputBadRequest();
            foreach (var currentPriceHistoryDto in listPriceHistoryDto)
            {
                //4.
                validationOutput = _priceHistoryDTOValidator.DTOIsValid(currentPriceHistoryDto);
                if (validationOutput.HasErrors())
                {
                    return(validationOutput);
                }

                PriceHistory currentPriceHistory = _mapper.Map <PriceHistory>(currentPriceHistoryDto);

                //5.
                if (finishToModify.ContainsPriceHistory(currentPriceHistory))
                {
                    validationOutput.AddError("Price history item",
                                              "A price history item set to the date " + currentPriceHistory.Date + " with the price " +
                                              currentPriceHistory.Price.Value + " has already been defined in the finish '" +
                                              finishReference +
                                              "' present in the material '" + materialReference + "'!");
                    return(validationOutput);
                }

                //6.
                if (priceHistoryItemsToAdd.Contains(currentPriceHistory))
                {
                    validationOutput.AddError("Price history item",
                                              "A price history item is duplicated in the list of defined price history items.");
                    return(validationOutput);
                }

                priceHistoryItemsToAdd.Add(currentPriceHistory);
            }

            foreach (var priceHistoryItemToAdd in priceHistoryItemsToAdd)
            {
                finishToModify.AddPriceToHistory(priceHistoryItemToAdd);
            }

            validationOutput.DesiredReturn = enumerablePriceHistoryDto;
            _materialRepository.Update(materialToModify);
            return(validationOutput);
        }
Example #12
0
        /**
         * Validations performed:
         *
         * 1. The received list has 1 or more elements.
         * FOREACH FINISH RECEIVED {
         * 2. Validation of each finish's reference (business rules);
         * 3. Validation of each finish's definition (business rules);
         * 4. Validation of the existence of each finish received, in the material with the passed reference.
         * 5. Validation for duplication between received finishes.
         * }
         */
        private ValidationOutput PrivateAddFinishesToMaterial(string reference,
                                                              IEnumerable <FinishDto> enumerableFinishDto)
        {
            List <FinishDto>
            listFinishDto =
                new List <FinishDto>(
                    enumerableFinishDto);     //Since we receive an IEnumerable, we need to have something concrete

            //1.
            ValidationOutput validationOutput = new ValidationOutputBadRequest();

            if (listFinishDto.Count == 0)
            {
                validationOutput.AddError("Finishes selected", "No finishes were selected!");
                return(validationOutput);
            }

            Material      materialToModify = _materialRepository.GetByReference(reference);
            List <Finish> finishesToAdd    = new List <Finish>();

            foreach (var currentFinishDto in listFinishDto)
            {
                validationOutput = new ValidationOutputBadRequest();

                //2.
                validationOutput = _finishDTOValidator.DTOReferenceIsValid(currentFinishDto.Reference);
                if (validationOutput.HasErrors())
                {
                    return(validationOutput);
                }

                //3.
                validationOutput = _finishDTOValidator.DTOIsValidForRegister(currentFinishDto);
                if (validationOutput.HasErrors())
                {
                    return(validationOutput);
                }

                Finish currentFinish = _mapper.Map <Finish>(currentFinishDto);

                //4.
                if (materialToModify.ContainsFinish(currentFinish))
                {
                    validationOutput.AddError("Finish",
                                              "Finish with the reference '" + currentFinish.Reference + "' already exists in material '" +
                                              reference + "'!");
                    return(validationOutput);
                }

                //5.
                if (finishesToAdd.Contains(currentFinish))
                {
                    validationOutput.AddError("Finish",
                                              "Finish with the reference '" + currentFinish.Reference +
                                              "' is duplicated in the list of selected finishes.");
                    return(validationOutput);
                }

                finishesToAdd.Add(currentFinish);
            }

            foreach (var finishToAdd in listFinishDto)
            {
                AddNewPriceToFinishHistory(finishToAdd);
                finishToAdd.IsActive = true;
                Finish currentFinish = _mapper.Map <Finish>(finishToAdd);
                materialToModify.AddFinish(currentFinish);
            }

            validationOutput.DesiredReturn = enumerableFinishDto;
            _materialRepository.Update(materialToModify);
            return(validationOutput);
        }
Example #13
0
        /**
         * Validations performed:
         *
         * 1. The received list has 1 or more elements.
         * FOREACH RECEIVED Color{
         * 2. Validation of each color's definition (business rules);
         * 3. Validation of the existence of each Color received, in the material with the passed reference
         * 4. Validation for duplication between received colors
         * }
         */
        private ValidationOutput PrivateAddColorsToMaterial(string reference, IEnumerable <ColorDto> enumerableColorDto)
        {
            List <ColorDto>
            listColorDto =
                new List <ColorDto>(
                    enumerableColorDto);     //Since we receive an IEnumerable, we need to have something concrete

            //1.
            ValidationOutput validationOutput = new ValidationOutputBadRequest();

            if (listColorDto.Count == 0)
            {
                validationOutput.AddError("Selected colors", "No colors were selected!");
                return(validationOutput);
            }

            Material     materialToModify = _materialRepository.GetByReference(reference);
            List <Color> colorsToAdd      = new List <Color>();

            foreach (var currentColorDto in listColorDto)
            {
                validationOutput = new ValidationOutputBadRequest();

                //2.
                validationOutput = _colorDTOValidator.DTOReferenceIsValid(currentColorDto.HexCode);
                if (validationOutput.HasErrors())
                {
                    return(validationOutput);
                }

                //3
                validationOutput = _colorDTOValidator.DTOIsValidForRegister(currentColorDto);
                if (validationOutput.HasErrors())
                {
                    return(validationOutput);
                }

                validationOutput = _colorDTOValidator.DTOIsValid(currentColorDto);
                if (validationOutput.HasErrors())
                {
                    return(validationOutput);
                }


                Color currentColor = _mapper.Map <Color>(currentColorDto);

                //4.
                if (materialToModify.ContainsColor(currentColor))
                {
                    validationOutput.AddError("Color",
                                              "Color with the hex code '" + currentColor.HexCode + "' already exists in material '" +
                                              reference + "'!");
                    return(validationOutput);
                }

                //5.
                if (colorsToAdd.Contains(currentColor))
                {
                    validationOutput.AddError("Color",
                                              "Color with the hex code '" + currentColor.HexCode +
                                              "' is duplicated in the list of selected colors.");
                    return(validationOutput);
                }

                colorsToAdd.Add(currentColor);
            }

            foreach (var color in colorsToAdd)
            {
                Color currentColor = _mapper.Map <Color>(color);
                materialToModify.AddColor(currentColor);
            }

            validationOutput.DesiredReturn = enumerableColorDto;
            _materialRepository.Update(materialToModify);
            return(validationOutput);
        }