public async Task <int> AddOrderItemAsync(AddProductToOrderInputModel input) { var order = this.context.Orders.FirstOrDefault(x => x.Id == input.OrderId); var orderItem = this.context.OrderItems.FirstOrDefault(x => x.ProductId == input.ProductId && x.OrderId == input.OrderId); if (orderItem == null) { orderItem = new OrderItem() { ProductId = input.ProductId, Qty = input.Qty }; var productPrices = this.context.Products.Where(x => x.Id == orderItem.ProductId).Select(x => new { x.WebsitePrice, x.WholesalePrice }).FirstOrDefault(); orderItem.Price = order.Channel == Channel.Wholesale ? productPrices.WholesalePrice : productPrices.WebsitePrice; order.OrderItems.Add(orderItem); } else { orderItem.Qty += input.Qty; if (orderItem.Qty <= 0) { this.context.OrderItems.Remove(orderItem); } } await this.context.SaveChangesAsync(); await this.ordersService.RecalculateOrderStatusesAsync(input.OrderId); return(order.Id); }
public IActionResult AddProductToOrder(int productId) { var model = new AddProductToOrderInputModel() { ProductId = productId }; return(this.View(model)); }
public async Task <IActionResult> AddProductToOrder(AddProductToOrderInputModel input) { if (!this.ModelState.IsValid) { return(this.View(input)); } await this.orderItemsService.AddOrderItemAsync(input); return(this.Redirect("/Orders/OrderDetails/" + input.OrderId)); }
public async Task AddOrderItemsShouldUpdateQtyIfItemsIsAlreadyAddedWhenAddingItemsFromProduct() { var options = new DbContextOptionsBuilder <WHMSDbContext>().UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; using var context = new WHMSDbContext(options); var warehouse = new Warehouse { Address = new Address { }, Name = "Test", }; var product = new Product { ProductName = "Test Product", }; var productWarehouse = new ProductWarehouse { Product = product, Warehouse = warehouse, AggregateQuantity = 0, TotalPhysicalQuanitiy = 10, ReservedQuantity = 5, }; context.Warehouses.Add(warehouse); context.Products.Add(product); context.ProductWarehouses.Add(productWarehouse); var order = new Order { WarehouseId = warehouse.Id }; context.Orders.Add(order); var orderItem = new OrderItem { OrderId = order.Id, ProductId = product.Id, Qty = 3 }; context.OrderItems.Add(orderItem); await context.SaveChangesAsync(); var mockInventoryService = new Mock <IInventoryService>(); var mockOrdersService = new Mock <IOrdersService>(); var service = new OrderItemsService(context, mockInventoryService.Object, mockOrdersService.Object); var model = new AddProductToOrderInputModel { OrderId = order.Id, ProductId = product.Id, Qty = -3 }; var id = await service.AddOrderItemAsync(model); var orderItemDB = context.OrderItems.FirstOrDefault(); Assert.Null(orderItemDB); }
public IActionResult AddingProduct(AddProductToOrderInputModel model) { if (!this.ModelState.IsValid) { return(this.View(model)); } var addProductToOrderServiceModel = new AddProductToOrderServiceModel { OrderId = model.OrderId, ProductName = model.ProductName, ProductId = model.ProductId, Quantity = model.Quantity }; this.orders.AddProductToOrderList(addProductToOrderServiceModel); return(this.RedirectToAction("Details", "Orders", new { id = model.OrderId })); }