/// <summary> /// Creates an order /// </summary> /// <param name="bOrder"> /// The BusinessOrder which contains all the necessary information to create an order such as /// line items and updated inventory /// </param> public static void CreateOrder(BusinessOrder bOrder) { Log.Information($"Called the Data Access method to create an order with business order {bOrder}"); using var context = new TThreeTeasContext(SQLOptions.options); Orders additionalOrder = new Orders() { LocationId = bOrder.StoreLocation.Id, CustomerId = bOrder.Customer.Id, OrderTime = bOrder.OrderTime }; context.Orders.Add(additionalOrder); context.SaveChanges(); Log.Information($"Saved the additional order to the database"); List <LineItem> additionalLineItems = new List <LineItem>(); foreach (KeyValuePair <BusinessProduct, int> lineItem in bOrder.LineItems) { LineItem additionalLineItem = new LineItem() { OrdersId = additionalOrder.Id, ProductId = lineItem.Key.Id, Quantity = lineItem.Value }; additionalLineItems.Add(additionalLineItem); } foreach (LineItem additionalLineItem in additionalLineItems) { context.LineItem.Add(additionalLineItem); } context.SaveChanges(); Log.Information($"Saved the additional line items to the database"); List <Inventory> updatedInventories = new List <Inventory>(); foreach (KeyValuePair <BusinessProduct, int> inventory in bOrder.StoreLocation.inventory) { Inventory updatedInventory = new Inventory() { LocationId = bOrder.StoreLocation.Id, ProductId = inventory.Key.Id, Stock = inventory.Value }; updatedInventories.Add(updatedInventory); } foreach (Inventory updatedInventory in updatedInventories) { context.Inventory.Update(updatedInventory); } context.SaveChanges(); Log.Information($"Saved the updated inventories to the database"); }
/// <summary> /// Retrieves all orders of a customer /// </summary> /// <param name="customerId">The id of the customer</param> /// <returns>All BusinessOrders of a customer</returns> public static ICollection <BusinessOrder> GetOrdersWithCustomerId(int customerId) { Log.Information($"Called the Data Access method to get the orders with customer id {customerId}"); using var context = new TThreeTeasContext(SQLOptions.options); List <BusinessOrder> bOrders = new List <BusinessOrder>(); foreach (int orderId in context.Orders.Where(o => o.CustomerId == customerId).Select(o => o.Id).ToList()) { bOrders.Add(GetOrderWithId(orderId)); } return(bOrders); }
/// <summary> /// Retrieves all of the locations in the database. /// </summary> /// <returns>All BusinessLocation objects in the database</returns> public static ICollection <BusinessLocation> GetLocations() { Log.Information($"Called the Data Access method to get the all locations"); using var context = new TThreeTeasContext(SQLOptions.options); List <BusinessLocation> bLocations = new List <BusinessLocation>(); foreach (Location location in context.Location) { BusinessLocation bLocation = GetLocationWithId(location.Id); bLocations.Add(bLocation); } return(bLocations); }
/// <summary> /// Adds a customer to the database. /// </summary> /// <param name="bCustomer">The customer to add to the database</param> public static void AddCustomer(BusinessCustomer bCustomer) { Log.Information($"Called the Data Access method to add the customer {bCustomer}"); using var context = new TThreeTeasContext(SQLOptions.options); Customer additionalCustomer = new Customer() { FirstName = bCustomer.FirstName, LastName = bCustomer.LastName }; context.Customer.Add(additionalCustomer); context.SaveChanges(); }
/// <summary> /// Retrieves the customers with the given last name /// </summary> /// <param name="lastName">The last name of the customer</param> /// <returns>BusinessCustomer object that the Customer maps to with the given last name</returns> public static ICollection <BusinessCustomer> GetCustomersWithLastName(string lastName) { Log.Information($"Called the Data Access method to get the customers with last name {lastName}"); using var context = new TThreeTeasContext(SQLOptions.options); List <BusinessCustomer> customersWithLastName = new List <BusinessCustomer>(); foreach (Customer customer in context.Customer.Where(c => c.LastName.ToLower() == lastName.ToLower())) { BusinessCustomer bCustomer = GetCustomerWithId(customer.Id); customersWithLastName.Add(bCustomer); } return(customersWithLastName); }
/// <summary> /// Retrieves the product with the given id /// </summary> /// <param name="productId">The id of the product</param> /// <returns>The BusinessProduct with the given id</returns> public static BusinessProduct GetProductWithId(int productId) { Log.Information($"Called the Data Access method to get the product with product id {productId}"); using var context = new TThreeTeasContext(SQLOptions.options); Product product = context.Product.Where(p => p.Id == productId).FirstOrDefault(); if (product is null) { return(null); } BusinessProduct bProduct = new BusinessProduct() { Id = product.Id, Name = product.Name, Price = product.Price }; return(bProduct); }
/// <summary> /// Retrieves the customer with the given customer id. /// </summary> /// <param name="customerId">The id of the customer</param> /// <returns> /// BusinessCustomer object that the Customer maps to with the given customer id /// </returns> public static BusinessCustomer GetCustomerWithId(int customerId) { Log.Information($"Called the Data Access method to get the customer with customer id {customerId}"); using var context = new TThreeTeasContext(SQLOptions.options); Customer customer = context.Customer.Where(c => c.Id == customerId).FirstOrDefault(); if (customer is null) { return(null); } BusinessCustomer bCustomer = new BusinessCustomer() { Id = customer.Id, FirstName = customer.FirstName, LastName = customer.LastName }; return(bCustomer); }
/// <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> /// Retrieves the location with the given location id. /// </summary> /// <param name="locationId">The id of the location</param> /// <returns> /// The BusinessLocation object that maps to the location with the given location id /// </returns> public static BusinessLocation GetLocationWithId(int locationId) { Log.Information($"Called the Data Access method to get the location with location id {locationId}"); using var context = new TThreeTeasContext(SQLOptions.options); Location location = context.Location.Where(l => l.Id == locationId).FirstOrDefault(); if (location is null) { return(null); } BusinessLocation bLocation = new BusinessLocation() { Id = location.Id, Address = location.Address, City = location.City, Zipcode = location.Zipcode, State = location.State }; List <Inventory> inventories = context.Inventory.Where(i => i.LocationId == location.Id).ToList(); foreach (Inventory inventory in inventories) { Product product = context.Product.Where(p => p.Id == inventory.ProductId).FirstOrDefault(); BusinessProduct bProduct = new BusinessProduct() { Id = product.Id, Name = product.Name, Price = product.Price }; bLocation.AddProduct(bProduct, inventory.Stock); } return(bLocation); }