public PurchaseOrder GetPurchaseOrder(string parameters) { var arguments = this.ParseArguments(parameters); if (arguments != null) { // Check if we can find at least 1 customer and order argument and does not have any other arguments that are not properties of customer and order. // arguments only contain customer and order arguments. var argumentsThatAreForCustomerOrOrder = arguments.Keys.Where( key => key == Customer_custID || key == Customer_firstName || key == Customer_lastName || key == Order_custID || key == Order_orderDate || key == Order_orderID || key == Order_poNumber); if (argumentsThatAreForCustomerOrOrder.Count() == arguments.Count) { // Check if we have at least one of both if ((arguments.ContainsKey(Order_orderID) || arguments.ContainsKey(Order_custID) || arguments.ContainsKey(Order_poNumber) || arguments.ContainsKey(Order_orderDate)) && (arguments.ContainsKey(Customer_custID) || arguments.ContainsKey(Customer_firstName) || arguments.ContainsKey(Customer_lastName))) { var matchingCustomers = this.GetCustomerBy( arguments.ContainsKey(Customer_custID) ? int.Parse(arguments[Customer_custID]) as int? : null, arguments.ContainsKey(Customer_firstName) ? arguments[Customer_firstName] : null, arguments.ContainsKey(Customer_lastName) ? arguments[Customer_lastName] : null, arguments.ContainsKey(Customer_phoneNumber) ? arguments[Customer_phoneNumber] : null); if (matchingCustomers.Count > 1) { throw new WebFaultException <Error>(new Error("Cannot get order for more then 1 customer.", ""), System.Net.HttpStatusCode.BadRequest); } else { var matchingCustomer = matchingCustomers.First(); var ordersMadeByCustomer = this.GetOrderBy( arguments.ContainsKey(Order_orderID) ? int.Parse(arguments[Order_orderID]) as int? : null, matchingCustomer.custID, arguments.ContainsKey(Order_poNumber) ? arguments[Order_poNumber] : null, arguments.ContainsKey(Order_orderDate) ? DateTime.Parse(arguments[Order_orderDate]) as DateTime? : null); if (ordersMadeByCustomer == null && ordersMadeByCustomer.Count == 0) { throw new WebFaultException <Error>(new Error("No such order.", ""), System.Net.HttpStatusCode.BadRequest); } else { var po = new PurchaseOrder() { Customer = matchingCustomer, Order = ordersMadeByCustomer.First(), OrderedProducts = new List <OrderedProduct>() }; // We need to get EF Model again because above order was not tracked by EF. using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities()) { var order = context.Orders.Where(o => o.orderID == po.Order.orderID).First(); foreach (var cart in order.Carts) { po.OrderedProducts.Add(new OrderedProduct() { Product = new Product(cart.Product), quantity = cart.quantity }); } } return(po); } } } else { throw new WebFaultException <Error>(new Error("Minimum 1 customer and 1 order argument required!", "(custID OR lastName OR firstName) and (orderID OR poNumber OR orderDate)"), System.Net.HttpStatusCode.BadRequest); } } else { throw new WebFaultException <Error>(new Error("Only customer and order arguments allowed.", "(custID OR lastName OR firstName) and (orderID OR poNumber OR orderDate)"), System.Net.HttpStatusCode.BadRequest); } } else { throw new WebFaultException <Error>(new Error("Argument formatting invalid!", ""), System.Net.HttpStatusCode.BadRequest); } }
public PurchaseOrder GetPurchaseOrder(string parameters) { var arguments = this.ParseArguments(parameters); if (arguments != null) { // Check if we can find at least 1 customer and order argument and does not have any other arguments that are not properties of customer and order. // arguments only contain customer and order arguments. var argumentsThatAreForCustomerOrOrder = arguments.Keys.Where( key => key == Customer_custID || key == Customer_firstName || key == Customer_lastName || key == Order_custID || key == Order_orderDate || key == Order_orderID || key == Order_poNumber); if (argumentsThatAreForCustomerOrOrder.Count() == arguments.Count) { // Check if we have at least one of both if ((arguments.ContainsKey(Order_orderID) || arguments.ContainsKey(Order_custID) || arguments.ContainsKey(Order_poNumber) || arguments.ContainsKey(Order_orderDate)) && (arguments.ContainsKey(Customer_custID) || arguments.ContainsKey(Customer_firstName) || arguments.ContainsKey(Customer_lastName))) { var matchingCustomers = this.GetCustomerBy( arguments.ContainsKey(Customer_custID) ? int.Parse(arguments[Customer_custID]) as int? : null, arguments.ContainsKey(Customer_firstName) ? arguments[Customer_firstName] : null, arguments.ContainsKey(Customer_lastName) ? arguments[Customer_lastName] : null, arguments.ContainsKey(Customer_phoneNumber) ? arguments[Customer_phoneNumber] : null); if (matchingCustomers.Count > 1) { throw new WebFaultException<Error>(new Error("Cannot get order for more then 1 customer.", ""), System.Net.HttpStatusCode.BadRequest); } else { var matchingCustomer = matchingCustomers.First(); var ordersMadeByCustomer = this.GetOrderBy( arguments.ContainsKey(Order_orderID) ? int.Parse(arguments[Order_orderID]) as int? : null, matchingCustomer.custID, arguments.ContainsKey(Order_poNumber) ? arguments[Order_poNumber] : null, arguments.ContainsKey(Order_orderDate) ? DateTime.Parse(arguments[Order_orderDate]) as DateTime? : null); if (ordersMadeByCustomer == null && ordersMadeByCustomer.Count == 0) { throw new WebFaultException<Error>(new Error("No such order.", ""), System.Net.HttpStatusCode.BadRequest); } else { var po = new PurchaseOrder() { Customer = matchingCustomer, Order = ordersMadeByCustomer.First(), OrderedProducts = new List<OrderedProduct>() }; // We need to get EF Model again because above order was not tracked by EF. using (var context = new Models.CrazyMelvinsShoppingEmporiumDbEntities()) { var order = context.Orders.Where(o => o.orderID == po.Order.orderID).First(); foreach (var cart in order.Carts) { po.OrderedProducts.Add(new OrderedProduct() { Product = new Product(cart.Product), quantity = cart.quantity }); } } return po; } } } else { throw new WebFaultException<Error>(new Error("Minimum 1 customer and 1 order argument required!", "(custID OR lastName OR firstName) and (orderID OR poNumber OR orderDate)"), System.Net.HttpStatusCode.BadRequest); } } else { throw new WebFaultException<Error>(new Error("Only customer and order arguments allowed.", "(custID OR lastName OR firstName) and (orderID OR poNumber OR orderDate)"), System.Net.HttpStatusCode.BadRequest); } } else { throw new WebFaultException<Error>(new Error("Argument formatting invalid!", ""), System.Net.HttpStatusCode.BadRequest); } }