private void buttonSave_Click(object sender, EventArgs e) { try { OrderProduct theOrderProduct = new OrderProduct(); theOrderProduct.Order = this.theOrder; theOrderProduct.Product = this.comboBoxProducts.SelectedItem as Product; theOrderProduct.Count = (int)this.numericUpDown1.Value; theOrder.OrderProducts.Add(theOrderProduct); theOrderProduct.Save(); this.Close(); } catch (Exception ex) { this.comboBoxProducts.Items.Add(ex.Message); } }
public BasketDTO AddToBasket([FromBody] ProductDTO productDto) { if (productDto == null) { return(null); } try { var client = Client.GetClient(new Guid(ClientId)); var product = productDto.MapToProduct(); var openOrder = Order.GetOrders().FirstOrDefault(f => !f.Complete); var order = openOrder ?? new Order { ClientId = client.Id, DateCreated = DateTime.Now, DateUpdated = DateTime.Now }; if (openOrder == null) { order = (Order)order.Save(); //Create an order if none exists so that we can get an Id value back } var orderProducts = (IList <OrderProduct>)OrderProduct.GetOrderProducts(); var openOrderProducts = orderProducts?.Where(w => w.OrderId == order.Id); var orderProductsList = openOrderProducts?.ToList() ?? new List <OrderProduct>(); // { //OrderId = order.Id // }; var matchingProduct = orderProductsList.FirstOrDefault(op => op.OrderId == order.Id && order.ClientId == client.Id && op.ProductId == product.Id); OrderProduct orderProduct; if (matchingProduct != null) { var productIndex = orderProductsList.IndexOf(matchingProduct); matchingProduct.Quantity++; orderProduct = (OrderProduct)matchingProduct.Save(); orderProductsList[productIndex] = orderProduct; } else { orderProduct = new OrderProduct { OrderId = order.Id, ProductId = product.Id, Quantity = 1 }; orderProductsList.Add((OrderProduct)orderProduct.Save()); } order.DateUpdated = DateTime.Now; order = (Order)order.Save(); //Order was updated so save it now after updating the date. var result = new BasketDTO { OrderInfo = order.MapTOrderDto(), OrderProducts = orderProductsList.MapToOrderProductDtoList() }; return(result); } catch (Exception exception) { return(null); } }