public async Task <int> AddOrderItemAsync(AddOrderItemsInputModel input) { var order = this.context.Orders.FirstOrDefault(x => x.Id == input.OrderId); foreach (var item in input.OrderItems) { var orderItem = this.context.OrderItems.FirstOrDefault(x => x.ProductId == item.ProductId && x.OrderId == input.OrderId); if (orderItem == null) { orderItem = new OrderItem() { ProductId = item.ProductId, Qty = item.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 += item.Qty; if (orderItem.Qty <= 0) { this.context.OrderItems.Remove(orderItem); } } } await this.context.SaveChangesAsync(); await this.ordersService.RecalculateOrderStatusesAsync(input.OrderId); return(order.Id); }
public async Task AddOrderItemsShouldUpdateQtyIfItemsIsAlreadyAdded() { 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.Orders.Add(order); 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 AddOrderItemsInputModel { OrderId = order.Id, OrderItems = new List <AddOrderItemViewModel> { new AddOrderItemViewModel { ProductId = product.Id, Price = 100, Qty = -30 } } }; var id = await service.AddOrderItemAsync(model); var orderItemDB = context.OrderItems.FirstOrDefault(); Assert.Null(orderItemDB); }
public IActionResult AddOrderItems(int id) { var model = new AddOrderItemsInputModel() { OrderId = id }; return(this.View(model)); }
public async Task <IActionResult> AddOrderItems([ModelBinder(BinderType = typeof(OrderItemsBinder))] AddOrderItemsInputModel input) { if (!this.ModelState.IsValid) { return(this.View(input)); } await this.orderItemsService.AddOrderItemAsync(input); return(this.RedirectToAction(nameof(this.OrderDetails), new { id = input.OrderId })); }
public Task BindModelAsync(ModelBindingContext bindingContext) { try { var model = new AddOrderItemsInputModel(); model.OrderId = int.Parse(bindingContext.HttpContext.Request.Form["orderId"].FirstOrDefault() ?? "0"); var orderItems = bindingContext.HttpContext.Request.Form["productId"]; var orderItemsQty = bindingContext.HttpContext.Request.Form["Qty"]; model.OrderItems = orderItems.Zip(orderItemsQty, (item, qty) => new AddOrderItemViewModel() { ProductId = int.Parse(item), Qty = int.Parse(qty) }).ToList(); bindingContext.Result = ModelBindingResult.Success(model); return(Task.CompletedTask); } catch (Exception) { } bindingContext.Result = ModelBindingResult.Failed(); return(Task.CompletedTask); }