public async Task <OrderSM> AddProductAsync(OrderSM order, string barcode, int quantity = 1) { if (order == null) { throw new ArgumentNullException(nameof(order)); } if (barcode == null) { throw new ArgumentNullException(nameof(barcode)); } if (quantity < 1) { throw new ArgumentOutOfRangeException(nameof(quantity), "Quantity should be more positive number."); } OrderSM orderSm; var orderProductSm = order.FirstOrDefault(x => x.Product.Barcode == barcode); if (orderProductSm is null) { var productSm = await _productRack.GetAsync(barcode); if (productSm is null) { return(order); } orderProductSm = new OrderProductSM(productSm) { Quantity = quantity }; orderSm = new OrderSM(order.Session, order.Append(orderProductSm)); } else { orderProductSm.Quantity += quantity; orderSm = new OrderSM(order.Session, order); } orderProductSm.Order = orderSm; return(orderSm); }
public Task <OrderSM> ChangeQuantityAsync(OrderSM order, int productId, int quantity) { if (order == null) { throw new ArgumentNullException(nameof(order)); } if (quantity < 1) { throw new ArgumentOutOfRangeException(nameof(quantity), "Quantity should be more positive number."); } var product = order.FirstOrDefault(x => x.Product?.Id == productId); if (product != null) { product.Quantity = quantity; } return(Task.FromResult(order)); }