예제 #1
0
        public async Task <IActionResult> CreateProductCollection(string key, [FromBody] ProductCollectionDto productCollection)
        {
            if (productCollection == null)
            {
                return(BadRequest());
            }

            if (!await _permissionService.Authorize(PermissionSystemName.Products))
            {
                return(Forbid());
            }

            var product = await _mediator.Send(new GetQuery <ProductDto>() { Id = key });

            if (!product.Any())
            {
                return(NotFound());
            }


            var pm = product.FirstOrDefault().ProductCollections.Where(x => x.CollectionId == productCollection.CollectionId).FirstOrDefault();

            if (pm != null)
            {
                ModelState.AddModelError("", "Product collection mapping found with the specified collectionid");
            }

            if (ModelState.IsValid)
            {
                var result = await _mediator.Send(new AddProductCollectionCommand()
                {
                    Product = product.FirstOrDefault(), Model = productCollection
                });

                return(Ok(result));
            }
            return(BadRequest(ModelState));
        }
예제 #2
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);
        }