예제 #1
0
        public async Task <ProductDetailsResponse> Handle(GetProductQuery query)
        {
            if (query.ProductId != default(int))
            {
                DomainCore.Models.Product product = await _readOnlyProductRepository.GetProductAsync(query.ProductId);

                if (product != null)
                {
                    return(CreateProductResponse(product));
                }

                throw new NotFoundException(new Fault {
                    Reason = "NotFoundResource", Message = $"Product with id:{query.ProductId} not found."
                });
            }

            if (!string.IsNullOrEmpty(query.Code))
            {
                DomainCore.Models.Product product = await _readOnlyProductRepository.GetProductByCodeAsync(query.Code);

                if (product != null)
                {
                    return(CreateProductResponse(product));
                }

                throw new NotFoundException(new Fault {
                    Reason = "NotFoundResource", Message = $"Product with code:{query.ProductId} not found."
                });
            }

            throw new ValidationException(new Fault {
                Reason = "NotValid", Message = $"Request is not valid."
            });
        }
예제 #2
0
        protected override async Task HandleCommand(DeleteProductCommand 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
                });
            }

            await _writeOnlyProductRepository.DeleteAsync(product).ConfigureAwait(false);
        }
예제 #3
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);
        }