public async Task EditOrderCommandHandler_UpdatesExistingOrder() { //Arrange Shop seller = new Shop(); Customer buyer = new Customer(); AllMarktContextIM.Shops.Add(seller); AllMarktContextIM.Customers.Add(buyer); await AllMarktContextIM.SaveChangesAsync(); var order = new AllMarkt.Entities.Order { Seller = seller, Buyer = buyer, AdditionalNotes = "Test Notes", DeliveryAddress = "Test Address", DeliveryPhoneNumber = "01234567890", TotalPrice = 1, TimeOfOrder = DateTime.Now, OrderStatus = Status.Registered, AWB = "Test AWB", }; OrderItem orderItem = new OrderItem { Order = order }; order.OrderItems = new List <OrderItem> { orderItem }; AllMarktContextIM.Orders.Add(order); AllMarktContextIM.OrderItems.Add(orderItem); await AllMarktContextIM.SaveChangesAsync(); var existingOrder = AllMarktContextIM.Orders.Where(o => o.Id == order.Id).FirstOrDefault(); var editOrderCommand = new EditOrderCommand { Id = existingOrder.Id, AdditionalNotes = "Test Notes", DeliveryAddress = "Edit Address", DeliveryPhoneNumber = "0123456789", TotalPrice = 1, OrderStatus = Status.Registered, AWB = "Edit AWB" }; //Act await _editOrderCommandHandler.Handle(editOrderCommand, CancellationToken.None); // AllMarktContextIM.Orders.Should().Contain(x => x.Id == editOrderCommand.Id); order.DeliveryAddress.Should().Be(editOrderCommand.DeliveryAddress); order.AWB.Should().Be(editOrderCommand.AWB); }
public async Task <IActionResult> PutOrder(Guid id, EditOrderCommand command) { command.OrderId = id; await mediator.Send(command); return(NoContent()); }
public async Task <IActionResult> EditOrder(EditOrderCommand command) { if (ModelState.IsValid) { await Mediator.Handle(command); return(RedirectToAction(nameof(OrderList))); } return(View(command)); }
public async Task <IActionResult> EditOrder(int orderId) { var order = await Mediator.Handle(new GetOrderByIdQuery() { OrderId = orderId }); if (order is null) { return(NotFound()); } return(View(EditOrderCommand.FromOrder(order))); }
public async Task <ActionResult> EditOrder([CustomizeValidator(Interceptor = typeof(API.Middleware.ValidatorInterceptor))] OrderViewModel order) { var getOrderQuery = new GetOrderQuery(order.Id); var orderQuery = await Mediator.Send(getOrderQuery); if (orderQuery == null) { return(NotFound("Order not found")); } var editOrderCommand = new EditOrderCommand(orderQuery, order.Product, order.Amount, order.Price, order.Notes); await Mediator.Send(editOrderCommand); return(NoContent()); }
public async Task EditOrder_NonExistentOrder_BusinessValidationException() { // Arrange var command = new EditOrderCommand { OrderId = Guid.NewGuid(), Amount = 1000 }; fixture.OrderRepositoryMock.Setup(foo => foo.GetAsync(command.OrderId)).ReturnsAsync((Order)null); // Act await Assert.ThrowsAsync <NotFoundException>(async() => await fixture.Instance.Send(command)); // Assert fixture.OrderRepositoryMock.Verify(m => m.UpdateAsync(It.IsAny <Order>()), Times.Never()); }
public async void Edits_New_Order_Successfully() { //Arrange var order = Context.Orders.FirstOrDefault(); Mediator.Setup(x => x.Send(It.IsAny <EditOrderCommand>(), new CancellationToken())) .ReturnsAsync(Unit.Value); //Act var editOrderCommand = new EditOrderCommand(order, "test product", 20, 50.5, ""); var handler = new EditOrderCommandHandler(Context); var result = await handler.Handle(editOrderCommand, new CancellationToken()); //Assert result.Should() .BeOfType <Unit>() .Equals(Unit.Value); DbContextFactory.Destroy(Context); }
public async Task EditOrder_ValidData_Success() { // Arrange var command = new EditOrderCommand { OrderId = Guid.NewGuid(), Amount = 1000 }; var order = new Order { Id = command.OrderId, Status = OrderStatus.Validation }; fixture.OrderRepositoryMock.Setup(foo => foo.GetAsync(order.Id)).ReturnsAsync(order); // Act await fixture.Instance.Send(command); // Assert fixture.OrderRepositoryMock.Verify(m => m.UpdateAsync(It.IsAny <Order>()), Times.Once()); }
public async Task EditOrder_OrderAproved_BusinessValidationException() { // Arrange var command = new EditOrderCommand { OrderId = Guid.NewGuid(), Amount = 1000 }; var order = new Order { Id = command.OrderId, Status = OrderStatus.Approved }; fixture.OrderRepositoryMock.Setup(foo => foo.GetAsync(order.Id)).ReturnsAsync(order); // Act await Assert.ThrowsAsync <BusinessValidationException>(async() => await fixture.Instance.Send(command)); // Assert fixture.OrderRepositoryMock.Verify(m => m.UpdateAsync(It.IsAny <Order>()), Times.Never()); }
public void Creates_CorrectCommand_fromOrder() { var customer = new CustomerVM() { Name = "TestName", Adress = new AdressVm() { City = "TestCity" } }; var order = new OrderVm() { OrderId = 1, Customer = customer, GiftWrap = true, Shipped = false }; var act = EditOrderCommand.FromOrder(order); Assert.AreEqual(order.OrderId, act.OrderId); Assert.AreEqual(order.Customer.Name, act.Customer.Name); Assert.AreEqual(order.GiftWrap, act.IsGiftWrap); Assert.AreEqual(order.Shipped, act.Shipped); Assert.AreEqual(order.Customer.Adress.City, act.Customer.Adress.City); }
public Task <ObjectClientResponse <EditOrderCommandResponse> > EditOrderAsync(EditOrderCommand request, HeaderData header) { return(Client.PutByIdAsync <Guid, EditOrderCommand, EditOrderCommandResponse>(request.Id, request, GetHeaders(header))); }
public async Task UpdateOrder_ValidRequest_ReturnsResponse() { // Arrange var header = await GetDefaultHeaderDataAsync(); var createCustomerResponses = await CreateSampleCustomersAsync(); var createProductResponses = await CreateSampleProductsAsync(); var createOrderResponses = await CreateSampleOrdersAsync(createCustomerResponses, createProductResponses); var productId_1 = createProductResponses[2].Data.Id; var productId_2 = createProductResponses[3].Data.Id; var createOrderResponse = createOrderResponses[1].Data; var id = createOrderResponse.Id; var orderCustomerId = createOrderResponse.CustomerId; var orderStatus = createOrderResponse.Status; var updateRequest = new EditOrderCommand { Id = id, OrderItems = new List <EditOrderItemCommand> { new EditOrderItemCommand { Id = createOrderResponse.OrderItems[0].Id, ProductId = productId_1, Quantity = 72, }, new EditOrderItemCommand { Id = null, ProductId = productId_2, Quantity = 84, } }, }; // Act var updateHttpResponse = await Fixture.Api.Orders.EditOrderAsync(updateRequest, header); // Assert updateHttpResponse.StatusCode.Should().Be(HttpStatusCode.OK); var updateResponse = updateHttpResponse.Data; updateResponse.Id.Should().Be(updateRequest.Id); updateResponse.CustomerId.Should().Be(orderCustomerId); updateResponse.Status.Should().Be(orderStatus); updateResponse.OrderItems.Should().NotBeNull(); updateResponse.OrderItems.Count.Should().Be(updateRequest.OrderItems.Count); for (int i = 0; i < updateRequest.OrderItems.Count; i++) { var updateRequestOrderDetail = updateRequest.OrderItems[i]; var updateResponseOrderDetail = updateResponse.OrderItems[i]; if (updateRequestOrderDetail.Id != null) { updateResponseOrderDetail.Id.Should().Be(updateRequestOrderDetail.Id.Value); } else { updateResponseOrderDetail.Id.Should().NotBeEmpty(); } updateResponseOrderDetail.ProductId.Should().Be(updateRequestOrderDetail.ProductId); updateResponseOrderDetail.Quantity.Should().Be(updateRequestOrderDetail.Quantity); updateResponseOrderDetail.Status.Should().Be(OrderItemStatus.Pending); } // TODO: VC: Fix test /* * var findRequest = new FindOrderQuery { Id = updateResponse.Id }; * * var findHttpResponse = await Fixture.Api.Orders.FindOrderAsync(findRequest); * * findHttpResponse.StatusCode.Should().Be(HttpStatusCode.OK); * * var findResponse = findHttpResponse.Data; * * findResponse.Should().BeEquivalentTo(updateResponse); */ }
public async Task <ActionResult <EditOrderCommandResponse> > UpdateOrderAsync(Guid id, EditOrderCommand request) { if (id != request.Id) { return(BadRequest("Mismatching id in route and request")); } var response = await _messageBus.SendAsync(request); return(Ok(response)); }
public async Task HandleAsync_Valid() { // Arrange var id = Guid.Parse("926a4480-61f5-416a-a16f-5c722d8463f7"); var customerId = Guid.Parse("711d5de4-628a-4acd-98c4-8ce099b149bc"); var orderItemId1 = Guid.Parse("c58b1858-2d67-4aee-9bfd-df1f9ac37227"); var orderItemId2 = Guid.Parse("f7319c98-a3e0-42be-9e4f-dcc1e3e1dea1"); var orderItemId3 = Guid.Parse("4455370b-0351-4cd8-a191-3c28841ff6be"); var productId1 = Guid.Parse("4e12839c-b431-4a4d-a7e0-80949d478945"); var productId2 = Guid.Parse("a9fbe198-edcd-4b27-9a0d-38420df95b02"); var productId3 = Guid.Parse("cbb3650a-3c47-4608-ae88-20ef603d9d25"); var productId1Price = 20.46m; var productId2Price = 25.68m; var productId3Price = 34.96m; var products = new List <Product> { new Product(new ProductIdentity(productId2), "code", "name", productId2Price, true), new Product(new ProductIdentity(productId3), "code", "name", productId3Price, true), }; var order = new Order(new OrderIdentity(id), new CustomerIdentity(customerId), new DateTime(2019, 12, 15), OrderStatus.Draft, new List <OrderItem> { new OrderItem(new OrderItemIdentity(orderItemId1), new ProductIdentity(productId1), productId1Price, 50, OrderItemStatus.Allocated), new OrderItem(new OrderItemIdentity(orderItemId2), new ProductIdentity(productId2), productId2Price, 60, OrderItemStatus.Unavailable), }); var command = new EditOrderCommand { Id = id, OrderItems = new List <EditOrderItemCommand> { new EditOrderItemCommand { Id = orderItemId1, ProductId = productId2, Quantity = 72, }, new EditOrderItemCommand { Id = null, ProductId = productId3, Quantity = 84, } }, }; var expectedUpdatedOrder = new Order(new OrderIdentity(id), new CustomerIdentity(customerId), new DateTime(2019, 12, 15), OrderStatus.Draft, new List <OrderItem> { new OrderItem(new OrderItemIdentity(orderItemId1), new ProductIdentity(productId2), productId2Price, 72, OrderItemStatus.Allocated), new OrderItem(new OrderItemIdentity(orderItemId3), new ProductIdentity(productId3), productId3Price, 84, OrderItemStatus.Allocated), }); var expectedResponse = new EditOrderCommandResponse { Id = id, CustomerId = customerId, Status = order.Status, OrderItems = new List <UpdateOrderItemCommandResponse> { new UpdateOrderItemCommandResponse { Id = orderItemId1, ProductId = productId2, Quantity = 72, }, new UpdateOrderItemCommandResponse { Id = orderItemId3, ProductId = productId3, Quantity = 84, }, }, }; var productIds = new List <ProductIdentity> { new ProductIdentity(productId2), new ProductIdentity(productId3), }; _orderRepositoryMock .Setup(e => e.FindAsync(new OrderIdentity(id))) .ReturnsAsync(order); _productReadonlyRepositoryMock .Setup(e => e.FindReadonlyAsync(productIds)) .ReturnsAsync(products); _orderFactoryMock .Setup(e => e.CreateOrderItem(new ProductIdentity(productId3), productId3Price, 84)) .Returns(new OrderItem(new OrderItemIdentity(orderItemId3), new ProductIdentity(productId3), productId3Price, 60, OrderItemStatus.Allocated)); _orderRepositoryMock .Setup(e => e.UpdateAsync(expectedUpdatedOrder)) .Returns(Task.CompletedTask); _mapperMock .Setup(e => e.Map <Order, EditOrderCommandResponse>(expectedUpdatedOrder)) .Returns(expectedResponse); // Act var handler = new EditOrderCommandHandler(_orderRepositoryMock.Object, _productReadonlyRepositoryMock.Object, _orderFactoryMock.Object, _mapperMock.Object); // Assert var response = await handler.HandleAsync(command); _orderRepositoryMock.Verify(e => e.FindAsync(new OrderIdentity(id)), Times.Once()); _productReadonlyRepositoryMock.Verify(e => e.FindReadonlyAsync(productIds), Times.Once()); _orderFactoryMock.Verify(e => e.CreateOrderItem(new ProductIdentity(productId3), productId3Price, 84), Times.Once()); _orderRepositoryMock.Verify(e => e.UpdateAsync(expectedUpdatedOrder), Times.Once()); _mapperMock.Verify(e => e.Map <Order, EditOrderCommandResponse>(expectedUpdatedOrder), Times.Once()); response.Should().BeEquivalentTo(expectedResponse); }