public async Task <ValidationResult> ValidateDeleteProductSubCategory(DeleteProductSubCategoryInput input)
        {
            ValidationResult validationResult = new();

            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.SubCategoryName))
            {
                validationResult.Messages.Add(new(nameof(CreateProductCategoryInput.Name), "La subcategoria no existe en esta categoria."));
            }

            return(validationResult);
        }
Пример #2
0
        public async Task <OperationResult> DeleteSubCategoryAsync(DeleteProductSubCategoryInput input)
        {
            var validationResult = await _productCategoryValidator.ValidateDeleteProductSubCategory(input);

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

                entity.SubCategories = entity.SubCategories.Where(x => x != input.SubCategoryName).ToList();

                await _productCategoryRepository.UpdateAsync(entity);

                await _productRepository.InactiveProductsByCategory(input.CategoryName, input.SubCategoryName);

                return(OperationResult.Success());
            }

            return(OperationResult.Fail(validationResult));
        }