Ejemplo n.º 1
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);
        }
        /**
         * Method that will add new configured products to the collection with the passed reference.
         * It is assumed that a list with 1 or more objects is received.
         *
         * Validations performed:
         * 1. Validation of the passed collection's reference (database);
         * 2. The received list has 1 or more elements.
         * FOREACH RECEIVED string (Configured Product reference) {
         * 3. Validation of current configured product reference (database);
         * 4. Validation of the existence of current configured product in the collection with the passed reference
         * 5. Validation for duplication between each configured product reference received
         */
        public ValidationOutput AddConfiguredProducts(string reference, IEnumerable <ConfiguredProductDto> enumerableConfiguredProduct)
        {
            //1.
            ValidationOutput validationOutput = new ValidationOutputNotFound();

            if (!CollectionExists(reference))
            {
                validationOutput.AddError("Reference of collection", "No collection with the reference '" + reference + "' exists in the system.");
                return(validationOutput);
            }

            Collection collectionToModify = _collectionRepository.GetByReference(reference);

            List <ConfiguredProductDto> configuredProductList = new List <ConfiguredProductDto>(enumerableConfiguredProduct);

            //2.
            validationOutput = new ValidationOutputBadRequest();
            if (configuredProductList.Count == 0)
            {
                validationOutput.AddError("Selected configured products", "No configured products were selected!");
                return(validationOutput);
            }

            List <string> configuredProductListToAdd = new List <string>();

            foreach (var configuredProduct in configuredProductList)
            {
                //3.
                if (!ConfiguredProductExists(configuredProduct.Reference))
                {
                    validationOutput.AddError("Reference of configured product", "No configured product with the reference '" + configuredProduct.Reference + "' exists in the system.");
                    return(validationOutput);
                }

                //4.
                if (collectionToModify.ConfiguredProductIsInCollection(configuredProduct.Reference))
                {
                    validationOutput.AddError("Configured product", "Configured product '" + configuredProduct.Reference + "' already belongs to collection with reference '" + reference + "'.");
                    return(validationOutput);
                }

                //5.
                if (configuredProductListToAdd.Contains(configuredProduct.Reference))
                {
                    validationOutput.AddError("Configured product", "Configured product '" + configuredProduct.Reference + "' is duplicated in the list of configured product selected!");
                    return(validationOutput);
                }

                configuredProductListToAdd.Add(configuredProduct.Reference);
            }

            foreach (var configuredProductToAdd in configuredProductListToAdd)
            {
                collectionToModify.AddConfiguredProduct(configuredProductToAdd);
            }

            validationOutput.DesiredReturn = configuredProductListToAdd;
            _collectionRepository.Update(collectionToModify);
            return(validationOutput);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 4
0
        /**
         * Method that will return either the catalog in the form of a DTO that has the passed reference OR all the errors found when trying to do so.
         *
         * Validations performed:
         * 1. Validation of the passed catalog's reference (database);
         *
         * This method can return a soft-deleted catalog.
         */
        public ValidationOutput ClientGetByReference(string reference)
        {
            //1.
            ValidationOutput validationOutput = new ValidationOutputNotFound();

            if (!ExistsAndIsActive(reference))
            {
                validationOutput.AddError("Reference of catalog", "No catalog with the reference '" + reference + "' exists in the system.");
                return(validationOutput);
            }

            validationOutput.DesiredReturn = _mapper.Map <CatalogDto>(_catalogRepository.GetByReference(reference));
            return(validationOutput);
        }
        // ============ Methods to GET something ============

        /**
         * Method that will return either the collection in the form of a DTO that has the passed reference OR all the errors found when trying to do so.
         *
         * Validations performed:
         * 1. Validation of the passed collection's reference (database);
         *
         * This method can return a soft-deleted category.
         */
        public ValidationOutput GetByReference(string reference)
        {
            //1.
            ValidationOutput validationOutput = new ValidationOutputNotFound();

            if (!CollectionExists(reference))
            {
                validationOutput.AddError("Reference of collection", "No collection with the reference '" + reference + "' exists in the system.");
                return(validationOutput);
            }

            validationOutput.DesiredReturn = _mapper.Map <CollectionDto>(_collectionRepository.GetByReference(reference));
            return(validationOutput);
        }
        // ============ Methods to GET something ============

        /**
         * Method that will return either the category in the form of a DTO that has the passed reference OR all the errors found when trying to do so.
         *
         * Validations performed:
         * 1. Validation of the passed category's reference (database);
         *
         * This method can return a soft-deleted category.
         */
        public ValidationOutput GetByReference(string refer)
        {
            //1.
            ValidationOutput validationOutput = new ValidationOutputNotFound();

            if (!Exists(refer))
            {
                validationOutput.AddError("Category's reference",
                                          "No category with the reference '" + refer + "' exists in the system.");
                return(validationOutput);
            }

            validationOutput.DesiredReturn = _mapper.Map <CategoryDto>(_categoryRepository.GetByReference(refer));
            return(validationOutput);
        }
Ejemplo n.º 7
0
        /**
         * Method that will add new finishes to the material with the passed reference.
         * It is assumed that a list with 1 or more objects is received.
         *
         * Validations performed:
         * 1. Validation of the passed material's reference (database);
         */
        public ValidationOutput AddFinishesToMaterial(string reference, IEnumerable <FinishDto> enumerableFinishDto)
        {
            //1.
            ValidationOutput validationOutput = new ValidationOutputNotFound();

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

            validationOutput = PrivateAddFinishesToMaterial(reference, enumerableFinishDto);
            return(validationOutput);
        }
Ejemplo n.º 8
0
        // ============ Methods to REMOVE something ============

        /**
         * Method that will soft-delete the catalog with the passed reference OR return all the errors found when trying to do so.
         *
         * Validations performed:
         * 1. Validation of the passed catalog's reference (database);
         */
        public ValidationOutput Remove(string refer)
        {
            //1.
            ValidationOutput validationOutput = new ValidationOutputNotFound();

            if (!CatalogExists(refer))
            {
                validationOutput.AddError("Reference of catalog", "No catalog with the reference '" + refer + "' exists in the system.");
                return(validationOutput);
            }

            Catalog catalogToRemove = _catalogRepository.GetByReference(refer);

            _catalogRepository.Delete(catalogToRemove);
            return(validationOutput);
        }
        // ============ Methods to REMOVE something ============

        /**
         * Method that will soft-delete the collection with the passed reference OR return all the errors found when trying to do so.
         *
         * Validations performed:
         * 1. Validation of the passed collection's reference (database);
         */
        public ValidationOutput Remove(string reference)
        {
            //1.
            ValidationOutput validationOutput = new ValidationOutputNotFound();

            if (!CollectionExists(reference))
            {
                validationOutput.AddError("Reference of collection", "No collection with the reference '" + reference + "' exists in the system.");
                return(validationOutput);
            }

            Collection collectionToRemove = _collectionRepository.GetByReference(reference);

            _collectionRepository.Delete(collectionToRemove);
            return(validationOutput);
        }
Ejemplo n.º 10
0
        // ============ Methods to UPDATE something ============

        /**
         * Method that will update the material (name, description and price) with the passed reference OR return all the errors found when trying to do so.
         *
         * Validations performed:
         * 1. Validation of the passed material's reference (database);
         * 2. Validation of the name, description and price of the passed DTO.
         */
        public ValidationOutput Update(string reference, MaterialDto dto)
        {
            //1.
            ValidationOutput validationOutput = new ValidationOutputNotFound();

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

            validationOutput = new ValidationOutputForbidden();
            if (dto.Reference != null && !dto.Reference.Equals(reference))
            {
                validationOutput.AddError("Reference of material", "It's not allowed to update reference.");
                return(validationOutput);
            }

            //2.
            validationOutput = _materialDTOValidator.DTOIsValidForUpdate(dto);
            if (validationOutput.HasErrors())
            {
                return(validationOutput);
            }

            Material materialToUpdate = _materialRepository.GetByReference(reference);

            if (dto.Name != null)
            {
                materialToUpdate.Name = dto.Name;
            }

            if (dto.Description != null)
            {
                materialToUpdate.Description = dto.Description;
            }

            /*if (dto.Price != null)
             * {
             *  materialToUpdate.AddPriceToHistory(new PriceHistory(_mapper.Map<Price>(dto.Price)));
             * }*/

            validationOutput.DesiredReturn =
                _mapper.Map <MaterialDto>(_materialRepository.Update(materialToUpdate));
            return(validationOutput);
        }
Ejemplo n.º 11
0
        // ============ Methods to REMOVE something ============

        /**
         * Method that will soft-delete the material with the passed reference OR return all the errors found when trying to do so.
         *
         * Validations performed:
         * 1. Validation of the passed material's reference (database);
         */
        public ValidationOutput Remove(string reference)
        {
            //1.
            ValidationOutput validationOutput = new ValidationOutputNotFound();

            if (!MaterialExists(reference))
            {
                validationOutput.AddError("Referece of material",
                                          "No material with the reference '" + reference + "' exists in the system.");
                return(validationOutput);
            }

            Material materialToRemove = _materialRepository.GetByReference(reference);

            _materialRepository.Delete(materialToRemove);
            return(validationOutput);
        }
Ejemplo n.º 12
0
        public ValidationOutput ClientGetByReference(string reference)
        {
            ValidationOutput validationOutput = _configuredProductDTOValidator.DTOReferenceIsValid(reference);

            if (validationOutput.HasErrors())
            {
                return(validationOutput);
            }
            if (!ExistsAndIsActive(reference))
            {
                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>(_configuredProductRepository.GetByReference(reference));
            return(validationOutput);
        }
Ejemplo n.º 13
0
        /**
         * Method that will return either the material in the form of a DTO that has the passed reference OR all the errors found when trying to do so.
         *
         * Validations performed:
         * 1. Validation of the passed material's reference (database);
         *
         * This method can return a soft-deleted material.
         */
        public ValidationOutput ClientGetByReference(string reference)
        {
            //1.
            ValidationOutput validationOutput = new ValidationOutputNotFound();

            if (!ExistsAndIsActive(reference))
            {
                validationOutput.AddError("Reference of material",
                                          "No material with the reference '" + reference + "' exists in the system.");
                return(validationOutput);
            }

            Material materialToReturn = _materialRepository.GetByReference(reference);

            validationOutput.DesiredReturn = _mapper.Map <MaterialDto>(materialToReturn);
            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);
        }
        // ============ Methods to UPDATE something ============

        /**
         * Method that will update the category (name and description), with the passed reference, with the data present in the passed DTO OR return all the errors found when trying to do so.
         *
         * Validations performed:
         * 1. Validation of the passed category's reference (database);
         * 2. Validation of the name and description of the passed DTO.
         */
        public ValidationOutput Update(string refer, CategoryDto dto)
        {
            //1.
            ValidationOutput validationOutput = new ValidationOutputNotFound();

            if (!Exists(refer))
            {
                validationOutput.AddError("Reference of category to update",
                                          "No category with the reference '" + refer + "' exists in the system.");
                return(validationOutput);
            }

            validationOutput = new ValidationOutputForbidden();
            if (dto.Reference != null && !dto.Reference.Equals(refer))
            {
                validationOutput.AddError("Reference of category", "It's not allowed to update reference.");
                return(validationOutput);
            }

            //2.
            validationOutput = _categoryDTOValidator.DTOIsValidForUpdate(dto);
            if (validationOutput.HasErrors())
            {
                return(validationOutput);
            }

            Category categoryToUpdate = _categoryRepository.GetByReference(refer);

            if (dto.Name != null)
            {
                categoryToUpdate.Name = dto.Name;
            }

            if (dto.Description != null)
            {
                categoryToUpdate.Description = dto.Description;
            }

            validationOutput.DesiredReturn = _mapper.Map <CategoryDto>(_categoryRepository.Update(categoryToUpdate));
            return(validationOutput);
        }
Ejemplo n.º 16
0
        public ValidationOutput Remove(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);
            }

            _configuredProductRepository.Delete(configuredProduct);

            return(validationOutput);
        }
        // ============ Business Methods ============

        public ValidationOutput GetProductsCollection(string reference)
        {
            //1.
            ValidationOutput validationOutput = new ValidationOutputNotFound();

            if (!CollectionExists(reference))
            {
                validationOutput.AddError("Reference of collection", "No collection with the reference '" + reference + "' exists in the system.");
                return(validationOutput);
            }
            var collection = _collectionRepository.GetByReference(reference);

            List <ConfiguredProductDto> returnList = new List <ConfiguredProductDto>();

            foreach (var configuredProductCollection in collection.ProductCollectionList)
            {
                var configuredProduct = _configuredProductRepository.GetByReference(configuredProductCollection.ConfiguredProductReference);
                returnList.Add(this._mapper.Map <ConfiguredProductDto>(configuredProduct));
            }
            validationOutput.DesiredReturn = returnList;
            return(validationOutput);
        }
Ejemplo n.º 18
0
        public ValidationOutput GetColors(string reference)
        {
            //1.
            ValidationOutput validationOutput = new ValidationOutputNotFound();

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

            Material        materialToReturn = _materialRepository.GetByReference(reference);
            List <ColorDto> listColors       = new List <ColorDto>();

            foreach (var color in materialToReturn.Colors)
            {
                listColors.Add(_mapper.Map <ColorDto>(color));
            }

            validationOutput.DesiredReturn = listColors;
            return(validationOutput);
        }
Ejemplo n.º 19
0
        // ============ Business Methods ============

        public ValidationOutput GetAllProductCollection(string reference)
        {
            //1.
            ValidationOutput validationOutput = new ValidationOutputNotFound();

            if (!CatalogExists(reference))
            {
                validationOutput.AddError("Reference of catalog", "No catalog with the reference '" + reference + "' exists in the system.");
                return(validationOutput);
            }
            var catalog = _catalogRepository.GetByReference(reference);

            List <ConfiguredProductCollectionDto> returnList = new List <ConfiguredProductCollectionDto>();

            foreach (var catalogProductCollection in catalog.CatalogProductCollectionList)
            {
                var productCollection = catalogProductCollection.ProductCollection;
                var collection        = _mapper.Map <CollectionDto>(_collectionRepository.GetByReference(productCollection.CollectionReference));
                var confProduct       = _mapper.Map <ConfiguredProductDto>(_configuredProductRepository.GetByReference(productCollection.ConfiguredProductReference));
                returnList.Add(new ConfiguredProductCollectionDto(confProduct, collection));
            }
            validationOutput.DesiredReturn = returnList;
            return(validationOutput);
        }
        // ============ Business Methods ============

        /**
         * This method will either return all the categories that have the category with the passed reference as their parent OR return all the errors found when trying to do so.
         * May return an empty list, indicating that there are no child categories (yet).
         *
         * Validations performed:
         * 1. Validation of the passed category's reference (database);
         */
        public ValidationOutput ObtainDirectChildCategories(string refer)
        {
            //1.
            ValidationOutput validationOutput = new ValidationOutputNotFound();

            if (!Exists(refer))
            {
                validationOutput.AddError("Category reference",
                                          "No category with the reference '" + refer + "' exists in the system.");
                return(validationOutput);
            }

            List <CategoryDto> childCategoriesDtoList = new List <CategoryDto>();
            List <Category>    childCategoriesList    = _categoryRepository.DirectChildCategories(refer);

            //For-each just to convert each Category object into a CategoryDto object
            foreach (var category in childCategoriesList)
            {
                childCategoriesDtoList.Add(_mapper.Map <CategoryDto>(category));
            }

            validationOutput.DesiredReturn = childCategoriesDtoList;
            return(validationOutput);
        }
Ejemplo n.º 21
0
        /**
         * Method that will remove price history from the material with the passed reference.
         * 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 (database);
         * FOREACH PRICE HISTORY RECEIVED {
         * 4. Validation of the existence of each price history received, in the material with the passed dto.
         * 5. Validation for duplication between received price history.
         * 6. Validation that the date of the price history is future
         * }
         */
        public ValidationOutput DeleteFinishPriceHistoryFromMaterial(string reference, string finishReference,
                                                                     IEnumerable <PriceHistoryDto> enumerableHistoryDto)
        {
            List <PriceHistoryDto>
            listPriceHistoryDto =
                new List <PriceHistoryDto>(
                    enumerableHistoryDto);     //Since we receive an IEnumerable, we need to have something concrete

            ValidationOutput validationOutput = new ValidationOutputBadRequest();

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

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

            Material            materialToModify     = _materialRepository.GetByReference(reference);
            List <PriceHistory> priceHistoryToDelete = new List <PriceHistory>();

            //3.
            if (!materialToModify.ContainsFinish(finishReference))
            {
                validationOutput.AddError("Finish",
                                          "Finish with the reference '" + finishReference + "' does not exist in material '" +
                                          reference + "'!");
                return(validationOutput);
            }

            Finish finishToModify = materialToModify.GetFinish(finishReference);

            foreach (var currentPriceHistoryDto in listPriceHistoryDto)
            {
                validationOutput = new ValidationOutputBadRequest();
                PriceHistory currentPriceHistory = _mapper.Map <PriceHistory>(currentPriceHistoryDto);

                //4.
                if (!finishToModify.ContainsPriceHistory(currentPriceHistory))
                {
                    validationOutput.AddError("Price History",
                                              "Price History with the date '" + currentPriceHistory.Date + "' does not exist in finish '" +
                                              finishReference + "'!");
                    return(validationOutput);
                }

                //5.
                if (priceHistoryToDelete.Contains(currentPriceHistory))
                {
                    validationOutput.AddError("Price History",
                                              "Price History with the date '" + currentPriceHistory.Date +
                                              "' is duplicated in the list of selected price history.");
                    return(validationOutput);
                }

                //6.
                if (currentPriceHistoryDto.Date.CompareTo(DateTime.Now) < 0)
                {
                    validationOutput.AddError("Price History",
                                              "Price History with the date '" + currentPriceHistory.Date +
                                              "' can't be deleted.");
                    return(validationOutput);
                }

                priceHistoryToDelete.Add(currentPriceHistory);
            }

            foreach (var priceToDelete in priceHistoryToDelete)
            {
                finishToModify.RemovePriceHistory(priceToDelete);
            }

            //Removes the old Finish
            materialToModify.RemoveFinish(materialToModify.GetFinish(finishReference));

            //Adds the new one
            materialToModify.AddFinish(finishToModify);


            _materialRepository.Update(materialToModify);
            return(validationOutput);
        }
Ejemplo n.º 22
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);
        }
Ejemplo n.º 23
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);
        }
Ejemplo n.º 24
0
        /**
         * Method that will add new configured products, that belong to a certain, existing collection, (in the form of ProductCollection objects) to the catalog with the passed reference.
         * It is assumed that a list with 1 or more objects is received.
         *
         * Validations performed:
         * 1. Validation of the passed catalog's reference (database);
         * 2. The received list has 1 or more elements.
         * FOREACH RECEIVED ProductCollection {
         * 3. Validation of collection's reference in current ProductCollection (database);
         * 4. Validation of configured product's reference in current ProductCollection (database);
         * 5. Validation to assert whether the indicated configured product actually belongs to the indicated collection
         * 6. Validation of the existence of current ProductCollection received, in the catalog with the passed reference
         * 7. Validation for duplication between each ProductCollection received
         * }
         */
        public ValidationOutput AddVariousProductCollection(string reference, IEnumerable <ProductCollectionDto> enumerableProductCollectionDto)
        {
            //1.
            ValidationOutput validationOutput = new ValidationOutputBadRequest();

            if (!CatalogExists(reference))
            {
                validationOutput.AddError("Reference of catalog", "No catalog with the '" + reference + "' exists in the system!");
                return(validationOutput);
            }

            Catalog catalogToModify = _catalogRepository.GetByReference(reference);

            List <ProductCollectionDto> productCollectionDtoList = new List <ProductCollectionDto>(enumerableProductCollectionDto);

            //2.
            validationOutput = new ValidationOutputBadRequest();
            if (productCollectionDtoList.Count == 0)
            {
                validationOutput.AddError("'Configured product - collection' items", "No 'configured product - collection' items were selected!");
                return(validationOutput);
            }

            List <ProductCollection> productCollectionListToAdd = new List <ProductCollection>();

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

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

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

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

                //5.
                if (catalogToModify.ContainsProductCollection(currentProdCollection))
                {
                    validationOutput.AddError("'Configured product - collection' item", "'Configured product - collection' item with configured product '" + currentProdCollection.ConfiguredProductReference + "' that is associated with the collection '" + currentProdCollection.CollectionReference + "' already belongs to catalog with reference '" + reference + "'");
                    return(validationOutput);
                }

                //6.
                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 'configured product - collection' items selected!");
                    return(validationOutput);
                }

                productCollectionListToAdd.Add(currentProdCollection);
            }

            foreach (var productCollectionToAdd in productCollectionListToAdd)
            {
                catalogToModify.AddProductCollection(productCollectionToAdd);
            }

            validationOutput.DesiredReturn = enumerableProductCollectionDto;
            _catalogRepository.Update(catalogToModify);
            return(validationOutput);
        }
Ejemplo n.º 25
0
        /**
         * Method that will remove colors from the material with the passed reference.
         * 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.
         * FOREACH COLOR RECEIVED {
         * 3. Validation of the existence of each Color received, in the material with the passed reference.
         * 4. Validation for duplication between received colors.
         * }
         */
        public ValidationOutput RemoveColorsFromMaterial(string reference, IEnumerable <ColorDto> enumerableColorDto)
        {
            List <ColorDto>
            listColorDto =
                new List <ColorDto>(
                    enumerableColorDto);     //Since we receive an IEnumerable, we need to have something concrete

            ValidationOutput validationOutput = new ValidationOutputBadRequest();

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

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

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

            foreach (var currentColorDto in listColorDto)
            {
                validationOutput = new ValidationOutputBadRequest();
                Color currentColor = _mapper.Map <Color>(currentColorDto);

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

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


                colorsToDelete.Add(currentColor);
            }

            foreach (var colorToDelete in colorsToDelete)
            {
                materialToModify.RemoveColor(colorToDelete);
            }

            _materialRepository.Update(materialToModify);
            return(validationOutput);
        }
Ejemplo n.º 26
0
        /**
         * Method that will remove finishes from the material with the passed reference.
         * 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.
         * FOREACH FINISH RECEIVED {
         * 3. Validation of the existence of each finish received, in the material with the passed reference.
         * 4. Validation for duplication between received finishes.
         * }
         */
        public ValidationOutput RemoveFinishesFromMaterial(string reference, IEnumerable <FinishDto> enumerableFinishDto)
        {
            ValidationOutput validationOutput = new ValidationOutputBadRequest();
            List <FinishDto>
            listFinishDto =
                new List <FinishDto>(
                    enumerableFinishDto);     //Since we receive an IEnumerable, we need to have something concrete

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

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

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

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

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

                //3.
                if (!materialToModify.ContainsFinish(currentFinish))
                {
                    validationOutput.AddError("Finish",
                                              "Finish with the reference '" + currentFinish.Reference + "' does not exist in material '" +
                                              reference + "'!");
                    return(validationOutput);
                }

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

                finishesToDelete.Add(currentFinish);
            }

            foreach (var finishToDelete in finishesToDelete)
            {
                materialToModify.RemoveFinish(finishToDelete);
            }

            _materialRepository.Update(materialToModify);
            return(validationOutput);
        }
Ejemplo n.º 27
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);
        }
        // ============ Methods to REMOVE something ============

        /**
         * Method that will soft-delete the category with the passed reference OR return all the errors found when trying to do so.
         *
         * Validations performed:
         * 1. Validation of the passed category's reference (database);
         */
        public ValidationOutput Remove(string refer)
        {
            //1.
            ValidationOutput validationOutput = new ValidationOutputNotFound();

            if (!Exists(refer))
            {
                validationOutput.AddError("Reference of category to remove",
                                          "No category with the reference '" + refer + "' exists in the system.");
                return(validationOutput);
            }

            Category categoryToRemove = _categoryRepository.GetByReference(refer);

            ///////////////
            // Lista de todas as categorias a apagar
            var listCategories = new List <Category>();
            // Lista de todos os produtos a apagar
            var listProducts = new List <Product>();


            // Lista de categorias filhas da categoria a apagar
            var validationOutputChildCategories = ObtainDirectChildCategories(refer);

            /*if (!validationOutputChildCategories.HasErrors())
             * {
             *  var childCategories = (List<CategoryDto>) validationOutputChildCategories.DesiredReturn;
             *  foreach (var categoryChild in childCategories)
             *  {
             *      Console.WriteLine("Category-> " + categoryToRemove.Reference + "----- categoryChild -> " + categoryChild.Reference);
             *      listCategories.Add(_mapper.Map<Category>(categoryChild));
             *      //GetChildCategories(_mapper.Map<Category>(categoryChild), listCategories, listProducts);
             *  }
             * }*/

            //Preenche as listas de produtos e categorias
            //GetChildCategories(categoryToRemove, listCategories, listProducts);

            /*foreach (var VARIABLE in listProducts)
             * {
             *  Console.WriteLine(VARIABLE.Reference);
             * }
             *
             * listCategories.Reverse();
             * foreach (var VARIABLE in listCategories)
             * {
             *  Console.WriteLine(VARIABLE.Reference);
             * }*/

            //_categoryRepository.Delete(listCategories[0]);

/*
 *
 *          // Adiciona à lista de produtos a remover os produtos da categoria que se pretende apagar
 *          foreach (var product in GetProductsOfCategory(_mapper.Map<Category>(categoryToRemove)))
 *          {
 *              listProducts.Add(_mapper.Map<Product>(product));
 *          }
 *
 *          /*Console.WriteLine("N Produtos: " + listProducts.Count);
 *          foreach (var product in listProducts)
 *          {
 *              Console.WriteLine("A Remover Produto " + product.Reference);
 *              _productService.Remove(product.Reference);
 *              Console.WriteLine("Já Removeu Produto " + product.Reference);
 *          }*/

            /*_categoryRepository.Delete(categoryToRemove);
             * Console.WriteLine("Já Removeu Categoria " + categoryToRemove.Reference);*/
/*
 *          Console.WriteLine("N Categorias: " + listCategories.Count);
 *          listCategories.Reverse();
 *          foreach (var category in listCategories)
 *          {
 *              Console.WriteLine(_categoryRepository.Update(category).Reference);
 *              /*
 *              Console.WriteLine("A remover categoria " + category.Reference + " da lista");
 *              listCategories.Remove(category);
 *              Console.WriteLine("Já removeu categoria " + category.Reference + " da lista");
 *              Console.WriteLine(category.Version);
 *              Console.WriteLine("A remover categoria " + category.Reference);
 *              _categoryRepository.Delete(category);
 *              Console.WriteLine("Já removeu categoria " + category.Reference);
 *          }*/
            ///////////////

            /*Console.WriteLine("||||||||||||||||||||||||||||");
            *
            *
            *  _productService.Remove("pro3");
            *  _productService.Remove("pro3");
            *  _productService.Remove("pro4");
            *
            *  _categoryRepository.Delete(_categoryRepository.GetByReference("cat4"));
            *  _categoryRepository.Delete(_categoryRepository.GetByReference("cat3"));
            *  _categoryRepository.Delete(_categoryRepository.GetByReference("cat2"));
            *
            *  Console.WriteLine("||||||||||||||||||||||||||||");*/

            _categoryRepository.Delete(categoryToRemove);
            //_categoryRepository.Delete(_categoryRepository.GetByReference("cat4"));*/
            return(validationOutput);
        }