示例#1
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);
        }