Exemple #1
0
        public async Task <IActionResult> UpdateSubCategoryName(UpdateProductSubCategoryNameInput input)
        {
            var result = await _service.UpdateSubCategoryNameAsync(input);

            return(new OperationActionResult(result));
        }
Exemple #2
0
        public async Task <OperationResult <ProductCategoryDto> > UpdateSubCategoryNameAsync(UpdateProductSubCategoryNameInput input)
        {
            var validationResult = await _productCategoryValidator.ValidateUpdateProductSubCategoryNameCategory(input);

            if (validationResult.IsSuccess)
            {
                var entity = await _productCategoryRepository.GetByNameAsync(input.CategoryName);

                var list = entity.SubCategories.Where(x => x != input.OldSubCategoryName).ToList();
                list.Add(input.NewSubCategoryName);

                entity.SubCategories = list;

                await _productCategoryRepository.UpdateAsync(entity);

                await _productRepository.UpdateSubCategoryName(input.OldSubCategoryName, input.NewSubCategoryName);

                var productCount = await _productRepository.CountByCategory(entity.Name);

                return(OperationResult <ProductCategoryDto> .Success(entity.ConvertToDto(productCount)));
            }

            return(OperationResult <ProductCategoryDto> .Fail(validationResult));
        }
        public async Task <ValidationResult> ValidateUpdateProductSubCategoryNameCategory(UpdateProductSubCategoryNameInput input)
        {
            ValidationResult validationResult = new();

            if (string.IsNullOrWhiteSpace(input.NewSubCategoryName))
            {
                validationResult.Messages.Add(new(nameof(UpdateProductSubCategoryNameInput.NewSubCategoryName), "Debe ingresar un nombre para la subcategoria."));
            }
            else if (input.NewSubCategoryName.HasFileInvalidChars())
            {
                validationResult.Messages.Add(new(nameof(UpdateProductSubCategoryNameInput.NewSubCategoryName), "El nombre no puede contener caracteres invalidos (<, >, :, \", /, \\, |, ?, *)."));
            }
            else
            {
                var productCategory = await _repository.GetByNameAsync(input.CategoryName);

                if (productCategory is null)
                {
                    validationResult.Messages.Add(new(nameof(UpdateProductSubCategoryNameInput.CategoryName), "La categoria no existe."));
                }
                else if (productCategory.SubCategories.Any(x => x == input.NewSubCategoryName))
                {
                    validationResult.Messages.Add(new(nameof(CreateProductCategoryInput.Name), "La subcategoria ya existe en esta categoria."));
                }
            }

            return(validationResult);
        }