private void ValidateMaterial(ChildConfiguredProductDto configuredDto, ValidationOutput validationOutput) { var material = _materialRepository.GetByReference(configuredDto.ConfiguredMaterial.OriginMaterialReference); if (!material.ChosenColorIsValid(configuredDto.ConfiguredMaterial.ColorReference)) { validationOutput.AddError("Chosen Color", "Material does not support the chosen color!"); } Price finishPrice = material.ChosenFinishIsValid(configuredDto.ConfiguredMaterial.FinishReference); if (finishPrice == null) { validationOutput.AddError("Chosen Finish", "Material does not support the chosen finish!"); } else { validationOutput.DesiredReturn = new Price[] { material.CurrentPrice(), finishPrice }; } }
private void ValidateSlotDefinition(Product product, ChildConfiguredProductDto configuredDto, ValidationOutput validationOutput) { Category category = _categoryRepository.GetByReference(product.CategoryReference); if (category.IsExternal && configuredDto.SlotReference == null) { return; } if (product.SlotDefinition != null) { if (configuredDto.ConfiguredSlots == null || configuredDto.ConfiguredSlots.Count < 1) { validationOutput.AddError("Slots", "Configured Slots required"); return; } var sum = 0; List <string> references = new List <string>(); foreach (var slot in configuredDto.ConfiguredSlots) { if (!(slot.Size >= product.SlotDefinition.MinSize && slot.Size <= product.SlotDefinition.MaxSize)) { validationOutput.AddError("Slot Size", "Slot Size (" + slot.Size + ") is not between min size (" + product.SlotDefinition.MinSize + ") and max size (" + product.SlotDefinition.MaxSize + ")!"); return; } if (references.Contains(slot.Reference)) { validationOutput.AddError("Slot Reference", "Slot Reference repeated"); return; } references.Add(slot.Reference); sum += slot.Size; } if (sum != configuredDto.ConfiguredDimension.Width) { validationOutput.AddError("Sum of Slot Sizes", "The sum of Slot Sizes is not equal to configured dimension width!"); } } }
/** * Method that will validate and create a new configured product in the database. * * Validations performed: * 1. Validation of the new configured product's reference (business rules); * 2. Validation of the new configured product's reference (database); * 3. Validation of the received info. (product reference, configured material, configured dimension) (business rules) * 4. Validation of the received info. (origin product, origin material, parent product, slot, dimensions, restrictions) (database) * 5. Validation of the received info. (everything fits) */ public ValidationOutput Register(ConfiguredProductDto configuredDto) { ValidationOutput validationOutput = new ValidationOutputBadRequest(); if (configuredDto == null) { validationOutput.AddError("ConfiguredProduct", "Null configured product."); return(validationOutput); } ConfiguredProduct configuredProduct = _configuredProductRepository.GetByReference(configuredDto.Reference); if (configuredProduct != null) { validationOutput = new ValidationOutputBadRequest(); validationOutput.AddError("Configured Product's reference", "There are already configured product with the given reference"); return(validationOutput); } //1. validationOutput = _configuredProductDTOValidator.DTOReferenceIsValid(configuredDto.Reference); if (validationOutput.HasErrors()) { return(validationOutput); } //2 if (_configuredProductRepository.GetByReference(configuredDto.Reference) != null) { validationOutput.AddError("ConfiguredProduct's reference", "A ConfiguredProduct with the reference '" + configuredDto.Reference + "' already exists in the system!"); return(validationOutput); } //3 validationOutput = _configuredProductDTOValidator.DTOIsValidForRegister(configuredDto); if (validationOutput.HasErrors()) { return(validationOutput); } ChildConfiguredProductDto ccpd = (ChildConfiguredProductDto)configuredDto; //4 validationOutput = DataExists(ccpd); if (validationOutput.HasErrors()) { return(validationOutput); } //5 validationOutput = ValidateConfiguredProduct(ccpd); if (validationOutput.HasErrors()) { return(validationOutput); } var configured = _mapper.Map <ConfiguredProduct>(configuredDto); if (_productRepository.GetByReference(configured.ProductReference).SlotDefinition == null || !configured.ConfiguredSlots.Any()) { configured.ConfiguredSlots.Clear(); configured.ConfiguredSlots.Add(new ConfiguredSlot(configured.ConfiguredDimension.Width, "default_slot_reference")); } configured.Price = new Price(CalculatePriceValue(((Price[])validationOutput.DesiredReturn)[2], ((Price[])validationOutput.DesiredReturn)[1], ((Price[])validationOutput.DesiredReturn)[0], configured.ConfiguredDimension)); if (ccpd.ParentReference != null) { var parentConfigured = _configuredProductRepository.GetByReference(ccpd.ParentReference); parentConfigured.Parts.Add(new ConfiguredPart(ccpd.Reference, ccpd.SlotReference)); parentConfigured.Price.Value += configured.Price.Value; _configuredProductRepository.Update(parentConfigured); } validationOutput.DesiredReturn = _mapper.Map <ConfiguredProductDto>(_configuredProductRepository.Add(configured)); return(validationOutput); }
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); }
public async Task <IActionResult> Create([FromHeader(Name = "Authorization")] string authorization, ChildConfiguredProductDto receivedDto) { if (!_userValidationService.CheckAuthorizationToken(authorization)) { return(Unauthorized()); } var userRef = await _userValidationService.GetUserRef(authorization.Split(" ")[1]); _logger.logInformation(userRef, LoggingEvents.PostItem, "Creating By Dto: {0}", receivedDto.Reference); ValidationOutput validationOutput = _configuredProductService.Register(receivedDto); if (validationOutput.HasErrors()) { if (validationOutput is ValidationOutputBadRequest) { _logger.logCritical(userRef, LoggingEvents.PostBadRequest, "Creating Configured Product Failed: {0}", ((ValidationOutputBadRequest)validationOutput).ToString()); return(BadRequest(validationOutput.FoundErrors)); } if (validationOutput is ValidationOutputNotFound) { _logger.logCritical(userRef, LoggingEvents.PostNotFound, "Creating Configured Product Failed: {0}", ((ValidationOutputNotFound)validationOutput).ToString()); return(NotFound(validationOutput.FoundErrors)); } _logger.logCritical(userRef, LoggingEvents.PostInternalError, "Type of validation output not recognized. Please contact your software provider."); return(BadRequest("Type of validation output not recognized. Please contact your software provider.")); } else { ConfiguredProductDto dto = (ConfiguredProductDto)validationOutput.DesiredReturn; _logger.logInformation(userRef, LoggingEvents.PostOk, "Creating Configured Product Succeeded: {0}", dto.ToString()); return(CreatedAtRoute("GetConfiguredProduct", new { reference = receivedDto.Reference }, dto)); } }