Exemple #1
0
        protected override async Task HandleCommand(UpdateProductCommand command)
        {
            Models.Product product = await _readOnlyProductRepository.GetProductAsync(command.ProductId).ConfigureAwait(false);

            if (product == null)
            {
                throw new NotFoundException(new Fault {
                    Reason = "ResourceNotFound", Message = PRODUCT_NOT_FOUND
                });
            }

            bool wasCodeChanged = product.Code != command.Code;

            if (wasCodeChanged)
            {
                var productByCode = await _readOnlyProductRepository.GetProductByCodeAsync(command.Code).ConfigureAwait(false);

                ProductValidator.ValidateProductCode(Faults, productByCode, command.Code);
                if (Faults.Any())
                {
                    var exception = new ValidationException();
                    exception.AddErrors(Faults);
                    throw exception;
                }
            }

            product.LastUpdated = DateTime.Now;
            product.Name        = command.Name;
            product.Code        = command.Code;
            product.Price       = command.Price;

            if (!string.IsNullOrEmpty(command.FileContent) && !string.IsNullOrEmpty(command.FileTitle))
            {
                byte[] imageBytes = Convert.FromBase64String(command.FileContent);

                HandleProductImage(command, product, imageBytes);
                HandleProductImageThumbnail(command, product, imageBytes);
            }

            await _writeOnlyProductRepository.UpdateAsync().ConfigureAwait(false);
        }