示例#1
0
        public async Task <OperationResult <ProductBadgeDto> > GetByProductAsync(string productId)
        {
            ProductBadge badge = await _productBadgeRepository.GetByProduct(new Product
            {
                Id = productId
            });

            if (badge is null)
            {
                return(OperationResult <ProductBadgeDto> .NotFound());
            }

            return(OperationResult <ProductBadgeDto> .Success(badge.ConvertToDto()));
        }
        public async Task <OperationResult <ProductDto> > UpdateProductBadgeAsync(UpdateProductBadgeInput input)
        {
            ValidationResult validationResult = await _validator.ValidateUpdateProductBadge(input);

            if (validationResult.IsSuccess)
            {
                Product product = await _repository.GetAsync(input.ProductId);

                product.PercentageOff = input.PercentageOff;

                await _repository.UpdateAsync(product);

                ProductBadge badge = await _badgeRepository.GetByProduct(product);

                if (badge is null)
                {
                    badge = new()
                    {
                        ProductId = input.ProductId,
                        Badges    = input.Badges
                    };

                    await _badgeRepository.CreateAsync(badge);
                }
                else
                {
                    badge.Badges = input.Badges;
                    await _badgeRepository.UpdateAsync(badge);
                }

                return(OperationResult <ProductDto> .Success(product.ConvertToDto()));
            }
            else
            {
                return(OperationResult <ProductDto> .Fail(validationResult));
            }
        }