private OrderModel createOrderModel(Orders order) { var orderModel = new OrderModel(); using (var db = new CustomerContext()) { if (order == null)//tester om orderen finnes return null; try { orderModel.orderNr = order.orderNr; orderModel.customerID = order.personID; var productOrders = (from p in db.ProductOrders where p.orderNr == order.orderNr select p).ToList(); orderModel.total = 0; var productDAL = new ProductDAL(); foreach (var o in productOrders)//legger produktene til i order modellen { var productModel = productDAL.find(o.products.productID); productModel.quantity = o.quantity; orderModel.products.Add(productModel); orderModel.total += o.price; } return orderModel; } catch (Exception ex) { _logging.logToDatabase(ex); } } return null; }
/*Legger ProductOrders inn i databasen. OrderNr og pruductID-ene må være med i modellen*/ private bool addProductOrders(OrderModel orderModel) { using (var db = new CustomerContext()) { try { var order = db.Orders.Find(orderModel.orderNr); if (order == null)//tester om ordren eksisterer return false; var productDAL = new ProductDAL(); foreach (var p in orderModel.products)//Opretter ProductOrders fra modellen { p.stock -= p.quantity; productDAL.updateQuantity(p); var productOrder = new ProductOrders(); productOrder.orders = order; productOrder.price = p.price; productOrder.products = db.Products.Find(p.productID); productOrder.quantity = p.quantity; productOrder.price = p.price * p.quantity; db.ProductOrders.Add(productOrder); } db.SaveChanges(); return true; } catch (Exception ex) { _logging.logToDatabase(ex); } return false; } }