public async Task <ICommandResult> Handle(UpdateProductCommand request, CancellationToken cancellationToken)
        {
            request.Validate();

            if (!request.IsValid)
            {
                return(new CommandResult(false, "Problemas ao atualizar o produto..", request.Notifications));
            }

            var requestedProduct = await _productRepository.GetProductAsync(request.Id);

            if (requestedProduct is null)
            {
                return(new CommandResult(false, "O Produto não foi encontrado", null));
            }

            var productData = new ProductData(request.Name, request.Description);

            var productImage = _productFileService.ProcessProductImage(request.ImageBase64);

            productData.ChangeImage(productImage);

            var datetimeProduct    = new DateTimeProduct(request.PurchaseDate, requestedProduct.DateTimeProduct.ExpirationDate);
            var productInformation = new ProductInformation(request.Quantity, request.MeasurementUnit);

            requestedProduct.ChangeProductData(productData);
            requestedProduct.ChangeDateTimeProduct(datetimeProduct);
            requestedProduct.ChangeProductInformation(productInformation);
            requestedProduct.ChangeStatus(request.ProductStatus);
            requestedProduct.ChangeCategory(request.Category);

            var updatedProduct = await _productRepository.UpdateProductAsync(requestedProduct);

            if (updatedProduct is null)
            {
                return(new CommandResult(false, "Problemas ao atualizar o produto..", null));
            }

            if (!updatedProduct.VerifyQuantity())
            {
                var buyingProduct      = true;
                var productDomainEvent = new ProductOperationDomainEvent(EventAction.UPDATE, updatedProduct, buyingProduct);

                await _domainEntityEventHandler.Raise(productDomainEvent);
            }

            return(new CommandResult(true, "Produto atualizado com sucesso!", request));
        }
Пример #2
0
        public async Task <ICommandResult> Handle(CreateProductCommand request, CancellationToken cancellationToken)
        {
            request.Validate();

            if (!request.IsValid)
            {
                return(new CommandResult(false, "Problemas ao cadastrar o produto..", request.Notifications));
            }

            var productExists = await _productRepository.VerifyProductoAsync(request.Name, request.User);

            if (productExists)
            {
                return(new CommandResult(false, "O produto já esta cadastrado.", null));
            }

            var productData         = new ProductData(request.Name, request.Description);
            var datetimeProduct     = new DateTimeProduct(request.PurchaseDate, request.ExpirationDate);
            var produtctInformation = new ProductInformation(request.Quantity, request.MeasurementUnit);

            var productImage = _productFileService.ProcessProductImage(request.ImageBase64);

            productData.ChangeImage(productImage);

            var newProduct = new Product(productData, datetimeProduct, produtctInformation, request.Category, request.User);

            var createdProduct = await _productRepository.CreateProductAsync(newProduct);

            if (createdProduct is null)
            {
                return(new CommandResult(false, "O produto não foi cadastrado.", null));
            }

            var buyingProduct      = false;
            var productDomainEvent = new ProductOperationDomainEvent(EventAction.INSERT, createdProduct, buyingProduct);

            await _domainEntityEventHandler.Raise(productDomainEvent);

            return(new CommandResult(true, "Produto cadastrado com sucesso!", request));
        }