public async Task <IActionResult> AddItem(Guid id, int quantity) { var product = await _productAppService.GetById(id); if (product == null) { return(BadRequest()); } if (product.StockQuantity < quantity) { TempData["Error"] = "The product has insufficient stock."; return(RedirectToAction("ProductDetails", "ShopWindow", new { id })); } var command = new AddOrderItemCommand(ClientId, product.Id, product.Name, quantity, product.Price); await _mediatorHandler.SendCommand(command).ConfigureAwait(true); if (IsOperationValid()) { return(RedirectToAction("Index")); } TempData["Errors"] = GetErrorMessages(); return(RedirectToAction("ProductDetails", "ShopWindow", new { id })); }
public async Task <IActionResult> AddItem(Guid id, int quantity) { var product = await _productAppService.GetById(id); if (product == null) { return(BadRequest()); } if (product.StockQuantity < quantity) { TempData["Erro"] = "Product on stock insuficient"; return(RedirectToAction("ProductDetail", "ShopWindow", new { id })); } var command = new AddOrderItemCommand(ClientId, product.Id, product.Name, quantity, product.Value); await _mediatrHandler.SendCommand(command); if (ValidOperation()) { return(RedirectToAction("Index")); } TempData["Erros"] = GetErroMessage(); return(RedirectToAction("ProductDetail", "ShopWindow", new { id })); }
public void AddOrderItemCommand_ValidCommand_ShouldBeValid() { // Arrange var command = new AddOrderItemCommand(Guid.NewGuid(), Guid.NewGuid(), "order item x", 2, 100); // Act var result = command.IsValid(); // Assert Assert.True(result); }
public async Task Handle(AddOrderItemCommand message, IMessageHandlerContext context) { var @event = mapper.Map <CreatedOrderItemEvent>(message); var result = await eventContext.AddAsync($"Order {message.OrderId}", @event); @event.LogPosition = result.LogPosition; @event.NextExpectedVersion = result.NextExpectedVersion; await context.Publish(@event); }
public void AddOrderItemCommand_ValidCommand_ShouldPassWithoutErrors() { // Arrange var orderCommand = new AddOrderItemCommand(Guid.NewGuid(), Guid.NewGuid(), "Test Product", 2, 50); // Act var result = orderCommand.IsValid(); // Assert result.Should().BeTrue(); }
public async Task CreateOrderItem(Guid orderId, int quantity, decimal price) { var command = new AddOrderItemCommand { Id = Guid.NewGuid(), OrderId = orderId, Price = price, Quantity = quantity }; await messageSession.Send(command); }
public void AddOrderItemCommand_CommandWithQuantityAboveAllowed_ShouldShowValidationErrors() { // Arrange var orderCommand = new AddOrderItemCommand(Guid.NewGuid(), Guid.NewGuid(), "Test product", Order.MAX_PRODUCT_QUANTITY + 1, 100); // Act var result = orderCommand.IsValid(); // Assert result.Should().BeFalse(); orderCommand.ValidationResult.Errors.Any(e => e.ErrorMessage.Equals(AddOrderItemValidation.QuantityMaxErrorMessage)).Should().BeTrue(); }
public async void AddOrderItem_PassInvalidCommand_ShouldReturnFalseAndThrowException() { // Arrange var command = new AddOrderItemCommand(Guid.Empty, Guid.Empty, string.Empty, 0, 0); // Act var result = await _orderCommandHandler.Handle(command, CancellationToken.None); // Assert Assert.False(result); _mocker.GetMock <IMediator>().Verify(r => r.Publish(It.IsAny <INotification>(), CancellationToken.None), Times.Exactly(5)); }
public void AddOrderItemCommand_IsValidCommand_ShouldPass() { //Arrange var customerId = Guid.NewGuid(); var productId = Guid.NewGuid(); var orderCommand = new AddOrderItemCommand(customerId, productId, "Product A", 2, 100); //Act var result = orderCommand.IsValid(); //Assert Assert.True(result); }
public void AddOrderItemCommand_MaxQtyIsInvalidCommand_ShouldNotPass() { //Arrange var customerId = Guid.NewGuid(); var productId = Guid.NewGuid(); var orderCommand = new AddOrderItemCommand(customerId, productId, "Product A", Domain.Models.Order.MAX_QTY_PER_ITEM + 1, 100); //Act var result = orderCommand.IsValid(); //Assert Assert.False(result); Assert.Contains(AddOrderItemValidation.MaxQtyErrorMsg, orderCommand.ValidationResult.Errors.Select(s => s.ErrorMessage)); }
public async void AddOrderItem_NewOrder_ShouldAddItem() { // Arrange var command = new AddOrderItemCommand(Guid.NewGuid(), Guid.NewGuid(), "item x", 2, 100); _mocker.GetMock <IOrderRepository>().Setup(r => r.UnitOfWork.Commit()).Returns(Task.FromResult(true)); // Act var result = await _orderCommandHandler.Handle(command, CancellationToken.None); // Assert Assert.True(result); _mocker.GetMock <IOrderRepository>().Verify(r => r.Add(It.IsAny <Order>()), Times.Once); _mocker.GetMock <IOrderRepository>().Verify(r => r.UnitOfWork.Commit(), Times.Once); }
public void AddOrderItemCommand_InvalidCommand_ShouldShowValidationErrors() { // Arrange var orderCommand = new AddOrderItemCommand(Guid.Empty, Guid.Empty, "", 0, 0); // Act var result = orderCommand.IsValid(); // Assert result.Should().BeFalse(); orderCommand.ValidationResult.Errors.Any(e => e.ErrorMessage.Equals(AddOrderItemValidation.ClientIdErrorMessage)).Should().BeTrue(); orderCommand.ValidationResult.Errors.Any(e => e.ErrorMessage.Equals(AddOrderItemValidation.ProductIdErrorMessage)).Should().BeTrue(); orderCommand.ValidationResult.Errors.Any(e => e.ErrorMessage.Equals(AddOrderItemValidation.ProductNameErrorMessage)).Should().BeTrue(); orderCommand.ValidationResult.Errors.Any(e => e.ErrorMessage.Equals(AddOrderItemValidation.QuantityMinErrorMessage)).Should().BeTrue(); orderCommand.ValidationResult.Errors.Any(e => e.ErrorMessage.Equals(AddOrderItemValidation.ValueErrorMessage)).Should().BeTrue(); }
public void AddOrderItemCommand_InvalidCommand_ShouldBeValid() { // Arrange var command = new AddOrderItemCommand(Guid.Empty, Guid.Empty, "", 0, 0); // Act var result = command.IsValid(); // Assert Assert.False(result); Assert.Contains(AddOrderItemCommandValidation.IdClienteErroMsg, command.ValidationResult.Errors.Select(c => c.ErrorMessage)); Assert.Contains(AddOrderItemCommandValidation.IdProdutoErroMsg, command.ValidationResult.Errors.Select(c => c.ErrorMessage)); Assert.Contains(AddOrderItemCommandValidation.NomeErroMsg, command.ValidationResult.Errors.Select(c => c.ErrorMessage)); Assert.Contains(AddOrderItemCommandValidation.QtdMinErroMsg, command.ValidationResult.Errors.Select(c => c.ErrorMessage)); Assert.Contains(AddOrderItemCommandValidation.ValorErroMsg, command.ValidationResult.Errors.Select(c => c.ErrorMessage)); }
public void AddOrderItemCommand_IsInvalidCommand_ShouldNotPass() { //Arrange var orderCommand = new AddOrderItemCommand(Guid.Empty, Guid.Empty, string.Empty, 0, 0); //Act var result = orderCommand.IsValid(); //Assert Assert.False(result); Assert.Contains(AddOrderItemValidation.CustomerIdErrorMsg, orderCommand.ValidationResult.Errors.Select(s => s.ErrorMessage)); Assert.Contains(AddOrderItemValidation.ProductNameErrorMsg, orderCommand.ValidationResult.Errors.Select(s => s.ErrorMessage)); Assert.Contains(AddOrderItemValidation.ProductNameErrorMsg, orderCommand.ValidationResult.Errors.Select(s => s.ErrorMessage)); Assert.Contains(AddOrderItemValidation.MinQtyErrorMsg, orderCommand.ValidationResult.Errors.Select(s => s.ErrorMessage)); Assert.Contains(AddOrderItemValidation.PriceErrorMsg, orderCommand.ValidationResult.Errors.Select(s => s.ErrorMessage)); }
public async Task AddItem_NewOrder_ShouldExecuteSuccessfully() { // Arrange var orderCommand = new AddOrderItemCommand(Guid.NewGuid(), Guid.NewGuid(), "Test product", 2, 50); var mocker = new AutoMocker(); var handler = mocker.CreateInstance <OrderCommandHandler>(); mocker.GetMock <IOrderRepository>().Setup(r => r.UnitOfWork.Commit()).Returns(Task.FromResult(true)); // Act var result = await handler.Handle(orderCommand, CancellationToken.None); // Assert result.Should().BeTrue(); mocker.GetMock <IOrderRepository>().Verify(r => r.Add(It.IsAny <Order>()), Times.Once); mocker.GetMock <IOrderRepository>().Verify(r => r.UnitOfWork.Commit(), Times.Once); //mocker.GetMock<IMediator>().Verify(m => m.Publish(It.IsAny<INotification>(), CancellationToken.None), Times.Once); }
public async Task <IActionResult> AddItem(Guid id) { var course = await _courseAppService.GetById(id); if (course == null) { return(BadRequest()); } var command = new AddOrderItemCommand(ClientId, course.Id, course.Name, course.Price); await _mediatorHandler.SendCommand(command); if (IsValidOperation()) { return(RedirectToAction("Index")); } TempData["Errors"] = this.GetErrors(); return(RedirectToAction("CourseDetail", "ShowRoom", new { id })); }
public async void AddOrderItem_ExistingItemInDraftOrder_ShouldAddItem() { // Arrange var existingItem = new Item(_itemId, "item x", 1, 100); _order.AddItem(existingItem); var command = new AddOrderItemCommand(_itemId, _itemId, "item x", 2, 50); _mocker.GetMock <IOrderRepository>().Setup(r => r.GetDraftOrderByClientId(_itemId)).Returns(Task.FromResult(_order)); _mocker.GetMock <IOrderRepository>().Setup(r => r.UnitOfWork.Commit()).Returns(Task.FromResult(true)); // Act var result = await _orderCommandHandler.Handle(command, CancellationToken.None); // Assert Assert.True(result); _mocker.GetMock <IOrderRepository>().Verify(r => r.UpdateOrderItem(It.IsAny <Item>()), Times.Once); _mocker.GetMock <IOrderRepository>().Verify(r => r.UpdateOrder(It.IsAny <Order>()), Times.Once); _mocker.GetMock <IOrderRepository>().Verify(r => r.UnitOfWork.Commit(), Times.Once); }
public async Task <JsonResult> AddOrderItem(AddOrderItemCommand addOrder) { return(new JsonResult(await Mediator.Send(addOrder))); }
public async Task GenerateTestEvents() { int iterations = 2000; Random rndItems = new Random(); for (int i = 0; i < iterations; i++) { var orderId = Guid.NewGuid(); var customerId = Guid.NewGuid(); var customerCommand = new CreateCustomerCommand { Id = customerId, FullName = customerId.ToString(), CreatedDate = DateTime.Now, Email = $"{customerId}@test.com", Phone = "123564" }; await messageSession.Send(customerCommand); Console.WriteLine($"Sent customer with id {customerId}"); Thread.Sleep(2000); var order = new CreateOrderCommand { Id = orderId, CustomerId = customerId, ItemsCount = 0, Status = 1, Total = 0, Vat = 0 }; await messageSession.Send(order); Console.WriteLine($"Sent order with id {orderId}"); int countItems = rndItems.Next(6, 12); for (int j = 0; j < countItems; j++) { var quantity = rndItems.Next(1, 12); var orderItem = new AddOrderItemCommand { Id = Guid.NewGuid(), OrderId = orderId, Quantity = quantity, Price = quantity * 10 }; await messageSession.Send(orderItem); Console.WriteLine($"Sent order item with id {orderItem}"); } if (i % 2 == 0) { var place = new PlaceOrderCommand(orderId); await messageSession.Send(place); Console.WriteLine($"Sent place order with id {orderId}"); } else { var cancel = new CancelOrderCommand(orderId); await messageSession.Send(cancel); Console.WriteLine($"Sent cancel order with id {orderId}"); } } }