/// <summary> /// Retrieves the BusinessOrder with the given order id /// </summary> /// <param name="orderId">The id of the order</param> /// <returns>The BusinessOrder object with the given order id</returns> public static BusinessOrder GetOrderWithId(int orderId) { Log.Information($"Called the Data Access method to get the order with order id {orderId}"); using var context = new TThreeTeasContext(SQLOptions.options); Orders order = context.Orders.FirstOrDefault(o => o.Id == orderId); if (order is null) { return(null); } BusinessLocation bLocation = LocationData.GetLocationWithId(order.LocationId); BusinessCustomer bCustomer = CustomerData.GetCustomerWithId(order.CustomerId); BusinessOrder bOrder = new BusinessOrder { Id = order.Id, StoreLocation = bLocation, Customer = bCustomer, OrderTime = order.OrderTime }; Dictionary <BusinessProduct, int> lineItems = new Dictionary <BusinessProduct, int>(); foreach (LineItem item in context.LineItem.Where(l => l.OrdersId == order.Id).ToList()) { Product product = context.Product.Where(p => p.Id == item.ProductId).FirstOrDefault(); BusinessProduct bProduct = new BusinessProduct() { Id = product.Id, Name = product.Name, Price = product.Price }; lineItems.Add(bProduct, item.Quantity); } bOrder.AddLineItems(lineItems); return(bOrder); }
/// <summary> /// Handles the prompting, input validation, and console output for a customer creating an order. /// </summary> private static void HandleRequestPlaceOrder() { Log.Information("Handling request placing order"); int line = 0; Dictionary <int, int> lineItemsParsed = new Dictionary <int, int>(); Dictionary <BusinessProduct, int> lineItems = new Dictionary <BusinessProduct, int>(); Console.WriteLine("[?] What is the customer ID"); string inputCustomerId = Console.ReadLine(); Log.Information($"User entered '{inputCustomerId}' for customer id"); if (!Int32.TryParse(inputCustomerId, out int customerId)) { throw new FormatException($"[!] Input for customer ID is not an integer"); } Console.WriteLine("[?] What is the location ID"); string inputLocationId = Console.ReadLine(); Log.Information($"User entered '{inputLocationId}' for location id"); if (!Int32.TryParse(inputLocationId, out int locationId)) { throw new FormatException($"[!] Input for location ID is not an integer"); } while (++line > 0) { Console.WriteLine($"[?] Line {line} - Enter product ID or press enter to finish your order"); string inputProductId = Console.ReadLine(); Log.Information($"User entered '{inputProductId}' for product id on line {line}"); if (inputProductId == "") { Log.Information($"User entered nothing for product id on line {line}"); break; } if (!Int32.TryParse(inputProductId, out int productId)) { throw new FormatException($"[!] Input for product ID is not an integer"); } Console.WriteLine($"[?] Line {line} - Enter quantity"); string inputQuantity = Console.ReadLine(); Log.Information($"User entered '{inputQuantity}' for quantity on line {line}"); if (!Int32.TryParse(inputQuantity, out int quantity)) { throw new FormatException($"[!] Input for quantity is not an integer"); } lineItemsParsed.Add(productId, quantity); } if (CustomerData.GetCustomerWithId(customerId) is null) { throw new BusinessCustomerException($"[!] Customer does not exist"); } if (LocationData.GetLocationWithId(locationId) is null) { throw new BusinessLocationException($"[!] Location does not exist"); } foreach (KeyValuePair <int, int> lineItemParsed in lineItemsParsed) { BusinessProduct bProduct = ProductData.GetProductWithId(lineItemParsed.Key); if (bProduct is null) { throw new BusinessProductException($"[!] Product {lineItemParsed.Key} does not exist"); } lineItems.Add(bProduct, lineItemParsed.Value); } BusinessCustomer bCustomer = CustomerData.GetCustomerWithId(customerId); BusinessLocation bLocation = LocationData.GetLocationWithId(locationId); BusinessOrder bOrder = new BusinessOrder() { StoreLocation = bLocation, Customer = bCustomer, OrderTime = DateTime.Now }; bOrder.AddLineItems(lineItems); foreach (KeyValuePair <BusinessProduct, int> lineItem in lineItems) { bOrder.StoreLocation.DecrementStock(lineItem.Key, lineItem.Value); } OrderData.CreateOrder(bOrder); }