/// <summary> /// Saves the order to the database /// </summary> /// <param name="order">The checked out order to be saved</param> public void saveOrder(lib.Order order) { try { var locationDict = getLocationDict(); using var context = new StoreDBContext(_options); var dataOrder = new DataAccess.Order(); dataOrder.CustomerId = order.CustomerId; dataOrder.OrderTime = order.Time; dataOrder.LocationId = context.Locations.Where(l => l.Name == order.Location).Select(l => l.LocationId).FirstOrDefault(); context.Add(dataOrder); context.SaveChanges(); foreach (var p in order.Products) { var dataOrderLine = new DataAccess.OrderLine(); dataOrderLine.OrderId = dataOrder.OrderId; dataOrderLine.ProductId = p.ProductId; dataOrderLine.Amount = p.Amount; context.Add(dataOrderLine); } context.SaveChanges(); } catch (Exception) { Console.WriteLine("Error creating new orders"); } }
/// <summary> /// Saves all the inventory to the database /// </summary> /// <param name="inventory">The inventory stored as a dictionary with LocationName:List<Product> as its Key/Value</param> public void saveAllInventory(Dictionary <string, List <lib.Product> > inventory) { try { foreach (var dataProduct in _context.Inventories.Include(i => i.Location).Include(i => i.Product)) { dataProduct.Amount = inventory[dataProduct.Location.Name] .FirstOrDefault(prod => prod.ProductId == dataProduct.ProductId) .Amount; _context.Update(dataProduct); } _context.SaveChanges(); } catch (Exception) { Console.WriteLine("Error creating new orders"); } }
/// <summary> /// Saves the customer to the database /// </summary> /// <param name="customer">The customer with changes to be saved</param> public void saveCustomer(lib.Customer customer) { try { using var context = new StoreDBContext(_options); var dataCustomer = context.Customers.Where(c => c.CustomerId == customer.CustomerId).FirstOrDefault(); dataCustomer.Balance = customer.Balance; context.Update(dataCustomer); context.SaveChanges(); } catch (Exception) { Console.WriteLine("Error saving customer's."); } }
/// <summary> /// Adds a new customer to the database /// </summary> /// <param name="customer">The newly created customer</param> public void addCustomer(lib.Customer customer) { try { using var context = new StoreDBContext(_options); var dataCustomer = new DataAccess.Customer(); dataCustomer.Balance = customer.Balance; dataCustomer.FirstName = customer.FirstName; dataCustomer.LastName = customer.LastName; context.Add(dataCustomer); context.SaveChanges(); } catch (Exception) { Console.WriteLine("Error saving customer."); } }