public BasketDTO GetClientBasket() { try { var client = Client.GetClient(new Guid(ClientId)); if (client == null) { return(null); } var orders = Order.GetOrders(); var openOrder = orders.FirstOrDefault(f => f.ClientId == client.Id && !f.Complete); if (openOrder == null) { return(null); } var orderProducts = OrderProduct.GetOrderProducts(); var openOrderProducts = orderProducts.Where(w => w.OrderId == openOrder.Id); return(new BasketDTO { OrderInfo = openOrder.MapTOrderDto(), OrderProducts = openOrderProducts.MapToOrderProductDtoList() }); } catch (Exception exception) { return(null); } }
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); } }