public async Task <Unit> Handle(UpdateProductCommand request, CancellationToken cancellationToken) { if (!request.IsValid()) { foreach (var error in request.ValidationResult.Errors) { await _eventDispatcher.RaiseEvent(new DomainNotification(request.MessageType, error.ErrorMessage)); } return(Unit.Value); } var product = new Product(); if (string.IsNullOrEmpty(request.Code)) { product = Product.UpdateWhithoutCode(request.Id, request.Name, request.Quantity, request.Cost); _productRepository.UpdateWhithoutCode(product); } else { product = Product.Update(request.Id, request.Code, request.Name, request.Quantity, request.Cost); _productRepository.Update(product); } _unitOfWork.Commit(); //raise events send email & store event await _eventDispatcher.RaiseEvent(new ProductCreatedEvent { Product = product }); return(Unit.Value); }
public async Task <Unit> Handle(UpdateProductCommand message, CancellationToken cancellationToken) { if (!message.IsValid()) { NotifyValidationErrors(message); return(await Unit.Task); } var product = new Product(message.Id, message.Name, message.Description, message.Price, message.RestaurantId, message.CategoryId); var existingProduct = await _productRepository.GetByName(product.Name); if (existingProduct != null && existingProduct.Id != product.Id) { if (!existingProduct.Equals(product)) { await Bus.RaiseEvent(new DomainNotification(message.MessageType, "Já existe um produto com este nome")); return(await Unit.Task); } } _productRepository.Update(product); if (Commit()) { await Bus.RaiseEvent(new ProductUpdatedEvent(product.Id, product.Name, product.Description, product.Price, product.RestaurantId, product.CategoryId)); } return(await Unit.Task); }
public async Task <ValidationResult> Handle(UpdateProductCommand message, CancellationToken cancellationToken) { if (!message.IsValid()) { return(message.ValidationResult); } var product = new Product(message.Id, message.Name, message.Description, message.Note, message.Price, message.StockQuantity, message.ValidateDate); var existingProduct = await _productRepository.GetByName(product.Name); if (existingProduct != null && existingProduct.Id != product.Id) { if (!existingProduct.Equals(product)) { AddError("The product name has already been taken."); return(ValidationResult); } } product.AddDomainEvent(new ProductUpdatedEvent(product.Id, product.Name, product.Description, product.Note, product.Price, product.StockQuantity, product.ValidateDate)); _productRepository.Update(product); return(await Commit(_productRepository.UnitOfWork)); }
public Task <object> Handle(UpdateProductCommand command, CancellationToken cancellationToken) { if (!command.IsValid()) { NotifyValidationErrors(command); } else { Dictionary <Entities.Unit, decimal> otherUnits = new Dictionary <Entities.Unit, decimal>(); foreach (ProductUnitModel productUnitModel in command.ProductUnitModels) { Entities.Unit unit = new Entities.Unit(new Identity((uint)productUnitModel.ID), null); otherUnits.Add(unit, productUnitModel.WPU); } Entities.Product product = new Entities.Product ( new Identity((uint)command.Id), new Name(command.Name), new Code(command.Code), new Note(command.Note), otherUnits, new Entities.Unit(new Identity((uint)command.CommonUnitId), null), command.WeightPerUnit, new Entities.Preservation((uint)command.PreservationId, null) ); bool result = _ProductRepository.Update(product); if (!result) { _bus.RaiseEvent(new DomainNotification("Product", "Server error", NotificationCode.Error)); } return(Task.FromResult(result as object)); } return(Task.FromResult(null as object)); }
public void Handle(UpdateProductCommand message) { if (!message.IsValid()) { NotifyValidationErrors(message); return; } var Product = new Product(message.Id, message.Name, message.Category, message.Code); var existingProduct = _ProductRepository.GetByCode(Product.Code); if (existingProduct != null && existingProduct.Id != Product.Id) { if (!existingProduct.Equals(Product)) { Bus.RaiseEvent(new DomainNotification(message.MessageType, "The Product e-Code has already been taken.")); return; } } _ProductRepository.Update(Product); if (Commit()) { Bus.RaiseEvent(new ProductUpdatedEvent(Product.Id, Product.Name, Product.Category, Product.Code)); } }
public Task <bool> Handle(UpdateProductCommand request, CancellationToken cancellationToken) { if (!request.IsValid()) { NotifyValidationErrors(request); Task.FromResult(false); } var product = new Product(request.ProductId, request.ProductName, request.Category, request.Detail, request.Seller, request.Images); var existingCategory = _productRepository.GetById(product.ProductId); if (existingCategory != null && existingCategory.ProductId == product.ProductId) { if (!existingCategory.Equals(product)) { _bus.RaiseEvent(new DomainNotification(request.MessageType, "The product has already been created.")); return(Task.FromResult(false)); } } _productRepository.Update(product); if (Commit()) { _bus.RaiseEvent(new ProductUpdatedEvent(product.ProductId, product.ProductName, product.Category, product.Detail, product.Seller, product.Images)); } return(Task.FromResult(true)); }
public Task <bool> Handle(UpdateProductCommand command, CancellationToken cancellationToken) { if (!command.IsValid()) { NotifyErrors(command); return(Task.FromResult(false)); } var product = new Product(command.Id, command.Name, command.Price, command.QuantityOnHand); _productRepository.Update(product); if (!Commit()) { return(Task.FromResult(false)); } return(Task.FromResult(true)); }
public Task <bool> Handle(UpdateProductCommand request, CancellationToken cancellationToken) { var product = new Product(request.Id, request.Name, request.Description, request.ShortDescription, request.Price); if (!request.IsValid()) { NotifyValidationErrors(request); return(Task.FromResult(false)); } _repository.Update(product); if (Commit()) { Bus.RaiseEvent(new ProductUpdatedEvent()); } return(Task.FromResult(true)); }
public Task <bool> Handle(UpdateProductCommand request, CancellationToken cancellationToken) { if (!request.IsValid()) { return(Task.FromResult(false)); } var produto = new Produto(request.id, request.nome, request.fornecedor, request.marca, request.valor); _repository.Update(produto); _repository.Save(); if (produto == null) { throw new Exception("Valor não pode ser nulo"); } return(Task.FromResult(true)); }
public Task <bool> Handle(UpdateProductCommand message, CancellationToken cancellationToken) { if (!message.IsValid()) { NotifyValidationErrors(message); return(Task.FromResult(false)); } var product = new Product(message.Id, message.Name); _productRepository.Update(product); if (Commit()) { Bus.RaiseEvent(new ProductUpdatedEvent(product.Id, product.Name)); } return(Task.FromResult(true)); }
public void UpdateProduct_ShouldBeSuccess() { var command = new UpdateProductCommand(Guid.NewGuid(), "Pipoca", 4, 5); Assert.True(command.IsValid()); }
public void UpdateProduct_ShouldReturnErrorWhenCommandIsInvalid() { var command = new UpdateProductCommand(Guid.NewGuid(), "P", 2, 5); Assert.False(command.IsValid()); }
public void Should_Not_Validate_When_Command_Is_Valid() { Assert.AreEqual(false, _invalidCommand.IsValid()); }
public void Should_Validate_When_Command_Is_Valid() { Assert.AreEqual(true, _validCommand.IsValid()); }