public void GetsAllLocations() { //Arrange var options = BuildInMemoryDb("GetsAllLocations"); //Act using (var context = new P0DbContext(options)) { var store = new Store { Location = "Location1" }; context.Add(store); store = new Store { Location = "Location2" }; context.Add(store); store = new Store { Location = "Location3" }; context.Add(store); context.SaveChanges(); } //Assert using (var context = new P0DbContext(options)) { var backend = new StoreBackend(context); var stores = backend.GetAllLocations(); Assert.Equal(3, stores.Count); } }
public void newCustomer() { using (P0DbContext db = new P0DbContext()) { try { Console.WriteLine("Please enter First and Last Name with space seperating them."); string input = Console.ReadLine(); string[] name = input.Split(' '); Console.WriteLine("Please enter a valid phone number"); input = Console.ReadLine(); int.TryParse(input, out int result); Customer newCustomer = new Customer(); newCustomer.FirstName = name[0]; newCustomer.LastName = name[1]; newCustomer.PhoneNumber = result; db.Add <Customer>(newCustomer); db.SaveChanges(); Console.WriteLine("Customer added. Press Enter to return to main menu."); Console.ReadLine(); } catch (Exception e) { Console.WriteLine($"Exception occurred: {e}"); Console.ReadLine(); return; } } }
public void AddsCustomerToDb() { //Arrange var options = BuildInMemoryDb("AddsCustomer"); string fName = "Bob", lName = "Dole", email = "*****@*****.**"; CustomerInfo customerInfo = null; //Act using (var context = new P0DbContext(options)) { var backend = new StoreBackend(context); customerInfo = backend.AddNewCustomer(fName, lName, email); } //Assert using (var context = new P0DbContext(options)) { var customer = (from c in context.Customers where c.FirstName == fName && c.LastName == lName select c).Take(1).FirstOrDefault(); Assert.Equal(customer.CustomerId, customerInfo.CustomerId); Assert.Equal(fName, customer.FirstName); Assert.Equal(lName, customer.LastName); Assert.Equal(email, customer.Email); } }
public void addSamples() { using (P0DbContext db = new P0DbContext()){ db.Add(new Store { Location = "Seattle" }); db.Add(new Store { Location = "Los Santos" }); db.Add(new Store { Location = "Velen" }); db.SaveChanges(); db.Add(new Product { Name = "SampleProduct11", StoreID = 1, Inventory = 5, Price = 2.50 }); db.Add(new Product { Name = "SampleProduct12", StoreID = 1, Inventory = 10, Price = 5.00 }); db.Add(new Product { Name = "SampleProduct21", StoreID = 2, Inventory = 15, Price = 10.00 }); db.Add(new Product { Name = "SampleProduct22", StoreID = 2, Inventory = 20, Price = 20.00 }); db.Add(new Product { Name = "SampleProduct31", StoreID = 3, Inventory = 25, Price = 50.00 }); db.Add(new Product { Name = "SampleProduct32", StoreID = 3, Inventory = 30, Price = 100.00 }); db.SaveChanges(); } }
public void GetsCustomerByEmail() { //Arrange var options = BuildInMemoryDb("GetsCustomer"); string fName = "Bob", lName = "Dole", email = "*****@*****.**"; //Act using (var context = new P0DbContext(options)) { var customer = new Customer { FirstName = fName, LastName = lName, Email = email }; context.Add(customer); context.SaveChanges(); } //Assert using (var context = new P0DbContext(options)) { var backend = new StoreBackend(context); var customerInfo = backend.GetCustomerInfo(email); Assert.Equal(fName, customerInfo.FirstName); Assert.Equal(lName, customerInfo.LastName); Assert.Equal(email, customerInfo.Email); } }
public bool existsProduct(int id) { using (P0DbContext db = new P0DbContext()){ try{ var check = db.Products .Where(c => c.ProductID == id); var inventoryCheck = db.Products .AsNoTracking() .Where(c => c.ProductID == id) .FirstOrDefault(); if (check.Count() == 0 || inventoryCheck.Inventory == 0) { return(false); } else { return(true); } } catch (Exception e) { Console.WriteLine($"Exception occurred: {e}"); return(false); } } }
public void CancelsOrderOnNegativeInventory() { //Arrange var options = BuildInMemoryDb("CancelsOrder"); //Act using (var context = new P0DbContext(options)) { CreateOneCustomer(context); CreateTwoproducts(context); var store = new Store { StoreId = 1, Location = "Location1", AvailableProducts = new List <Inventory> { new Inventory { ProductId = 1, StoreId = 1, Quantity = 10 }, new Inventory { ProductId = 2, StoreId = 1, Quantity = 50 } } }; context.Add(store); context.SaveChanges(); try { var backend = new StoreBackend(context); var prods = new List <ProductQuantity>() { new ProductQuantity() { ProductId = 1, Quantity = 12 } }; backend.PlaceNewOrder(1, 1, prods); } catch { } } //Assert using (var context = new P0DbContext(options)) { var orders = (from o in context.Orders select o).ToList(); Assert.Empty(orders); } }
public void DecrementsInventoryOnOrder() { //Arrange var options = BuildInMemoryDb("DecrementsInventory"); int orderId; //Act using (var context = new P0DbContext(options)) { CreateOneCustomer(context); CreateTwoproducts(context); var store = new Store { StoreId = 1, Location = "Location1", AvailableProducts = new List <Inventory> { new Inventory { ProductId = 1, StoreId = 1, Quantity = 10 }, new Inventory { ProductId = 2, StoreId = 1, Quantity = 50 } } }; context.Add(store); context.SaveChanges(); var backend = new StoreBackend(context); var prods = new List <ProductQuantity>() { new ProductQuantity() { ProductId = 1, Quantity = 2 } }; orderId = backend.PlaceNewOrder(1, 1, prods).OrderId; } //Assert using (var context = new P0DbContext(options)) { var item = (from inv in context.StoreInventories where inv.StoreId == 1 && inv.ProductId == 1 select inv).Take(1).FirstOrDefault(); Assert.Equal(8, item.Quantity); } }
private void CreateOneCustomer(P0DbContext context) { var customer = new Customer { CustomerId = 1, FirstName = "Jim", LastName = "Bob", Email = "*****@*****.**" }; context.Add(customer); context.SaveChanges(); }
public bool existsOrder(int id) { using (P0DbContext db = new P0DbContext()){ try{ var ret = db.Orders .Where(o => o.OrderID == id); return(!(ret.Count() == 0)); } catch (Exception e) { Console.WriteLine($"Exception occurred: {e}"); return(false); } } }
public void ThrowsOnNegativeInventory() { //Arrange var options = BuildInMemoryDb("ThrowsException"); //Act using (var context = new P0DbContext(options)) { CreateOneCustomer(context); CreateTwoproducts(context); var store = new Store { StoreId = 1, Location = "Location1", AvailableProducts = new List <Inventory> { new Inventory { ProductId = 1, StoreId = 1, Quantity = 10 }, new Inventory { ProductId = 2, StoreId = 1, Quantity = 50 } } }; context.Add(store); context.SaveChanges(); } //Assert using (var context = new P0DbContext(options)) { var backend = new StoreBackend(context); var prods = new List <ProductQuantity>() { new ProductQuantity() { ProductId = 1, Quantity = 12 } }; Assert.Throws <ArgumentOutOfRangeException>(() => backend.PlaceNewOrder(1, 1, prods)); } }
public Product getName(int id) { using (P0DbContext db = new P0DbContext()){ try{ return(db.Products .AsNoTracking() .Where(p => p.ProductID == id) .FirstOrDefault()); } catch (Exception e) { Console.WriteLine($"Exception occurred: {e}"); return(null); } } }
public ICollection <Customer> getCustomersAll() { using (P0DbContext db = new P0DbContext()) { try{ return(db.Customers .AsNoTracking() .ToList()); } catch (Exception e) { Console.WriteLine($"Exception: {e}"); return(null); } } }
public ICollection <Customer> findCustomer(string first, string last) { using (P0DbContext db = new P0DbContext()){ try{ return(db.Customers .AsNoTracking() .Where(c => c.FirstName.Contains(first) && c.LastName.Contains(last)) .OrderBy(c => c.FirstName) .ToList()); } catch (Exception e) { Console.WriteLine($"Exception: {e}"); return(null); } } }
public ICollection <Product> getProducts() { using (P0DbContext db = new P0DbContext()) { try{ return(db.Products .AsNoTracking() .Include(p => p.Store) .ToList()); } catch (Exception e) { Console.WriteLine($"Exception occurred: {e}"); return(null); } } }
public Customer getCustomer(int id) { using (P0DbContext db = new P0DbContext()) { try{ return(db.Customers .AsNoTracking() .Where(c => c.CustomerID == id) .FirstOrDefault()); } catch (Exception e) { Console.WriteLine($"Exception: {e}"); return(null); } } }
public void GetsAllOrdersForCustomer() { //Arrange var options = BuildInMemoryDb("GetsCustomersOrders"); //Act using (var context = new P0DbContext(options)) { CreateOneCustomer(context); CreateTwoproducts(context); var store = new Store { StoreId = 1, Location = "Location1" }; context.Add(store); context.SaveChanges(); var order = new Order { CusomerId = 1, OrderDateTime = DateTime.Now, OrderId = 1, StoreId = 1, }; context.Add(order); order = new Order { CusomerId = 1, OrderDateTime = DateTime.Now, OrderId = 2, StoreId = 1, }; context.Add(order); context.SaveChanges(); } //Assert using (var context = new P0DbContext(options)) { var backend = new StoreBackend(context); var orders = backend.GetCustomerOrderHistory(1); Assert.Equal(2, orders.Count); } }
public bool existsCustomer(int id) { using (P0DbContext db = new P0DbContext()) { try { var ret = db.Customers .Where(c => c.CustomerID == id); return(!(ret.Count() == 0)); } catch (Exception e) { Console.WriteLine($"Exception: {e}"); return(false); } } }
public ICollection <Order> GetOrders() { using (P0DbContext db = new P0DbContext()) { try { return(db.Orders .AsNoTracking() .ToList()); } catch (Exception e) { Console.WriteLine($"Exception occurred: {e}"); return(null); } } }
public string getLocation(int id) { using (P0DbContext db = new P0DbContext()){ try{ var store = db.Stores .AsNoTracking() .Where(s => s.StoreID == id) .FirstOrDefault(); return(store.Location); } catch (Exception e) { Console.WriteLine($"Exception occurred: {e}"); return(null); } } }
public ICollection <Store> getStores() { using (P0DbContext db = new P0DbContext()){ try{ return(db.Stores .AsNoTracking() .ToList()); } catch (Microsoft.Data.Sqlite.SqliteException) { Console.WriteLine($"There is no customer table currently."); return(null); } catch (Exception e) { Console.WriteLine($"Exception occurred: {e}"); return(null); } } }
public void placeOrder(Order newOrder) { using (P0DbContext db = new P0DbContext()) { try { var product = db.Products .Where(p => p.ProductID == newOrder.ProductID) .FirstOrDefault(); product.Inventory -= newOrder.Count; db.SaveChanges(); } catch (Exception e) { Console.WriteLine($"Exception occurred: {e}"); } } }
public ICollection <Order> getHistory(int id) { using (P0DbContext db = new P0DbContext()){ try{ return(db.Orders .AsNoTracking() .Where(o => o.Product.StoreID == id) .Include(customer => customer.Customer) .Include(order => order.Product) .OrderBy(o => o.Time) .ToList()); } catch (Exception e) { Console.WriteLine($"Exception occurred: {e}"); return(null); } } }
private void CreateTwoproducts(P0DbContext context) { var product = new Product { PoductId = 1, Price = 2.99, ProductDescription = "Prod1" }; context.Add(product); product = new Product { PoductId = 2, Price = 5.99, ProductDescription = "Prod2" }; context.Add(product); context.SaveChanges(); }
public void GetsCorrectInventoryCount() { //Arrange var options = BuildInMemoryDb("GetsInventoryCount"); //Act using (var context = new P0DbContext(options)) { CreateTwoproducts(context); var store = new Store { StoreId = 1, Location = "Location1", AvailableProducts = new List <Inventory> { new Inventory { ProductId = 1, StoreId = 1, Quantity = 10 }, new Inventory { ProductId = 2, StoreId = 1, Quantity = 50 } } }; context.Add(store); context.SaveChanges(); } //Assert using (var context = new P0DbContext(options)) { var backend = new StoreBackend(context); var items = backend.GetAllLocations().FirstOrDefault().AvailableProducts; Assert.Equal(2, items.Count); } }
public void newStore() { using (P0DbContext db = new P0DbContext()) { try { Console.WriteLine("Please enter a store location"); string loc = Console.ReadLine(); Store newStore = new Store(); newStore.Location = loc; db.Add <Store>(newStore); db.SaveChanges(); Console.WriteLine("Store added. Press Enter to return to main menu."); Console.ReadLine(); } catch (Exception e) { Console.WriteLine($"Exception occurred: {e}"); return; } } }
public bool checkCount(int productId, int count) { using (P0DbContext db = new P0DbContext()){ try{ var check = db.Products .Where(p => p.ProductID == productId) .FirstOrDefault(); if (check.Inventory < count) { return(false); } else { return(true); } } catch (Exception e) { Console.WriteLine($"Exception occurred: {e}"); return(false); } } }
public bool existsStore(int id) { using (P0DbContext db = new P0DbContext()){ try{ var check = db.Stores .AsNoTracking() .Where(s => s.StoreID == id); if (check.Count() == 0) { return(false); } else { return(true); } } catch (Exception e) { Console.WriteLine($"Exception occurred: {e}"); return(false); } } }
public ICollection <Order> orderDetails(int id) { using (P0DbContext db = new P0DbContext()){ try{ var order = db.Orders .AsNoTracking() .Where(o => o.OrderID == id) .FirstOrDefault(); var time = order.Time; return(db.Orders .AsNoTracking() .Where(o => o.Time == time) .Include(customer => customer.Customer) .Include(order => order.Product) .ThenInclude(product => product.Store) .ToList()); } catch (Exception e) { Console.WriteLine($"Exception occurred: {e}"); return(null); } } }
public StoreBackend(P0DbContext context) { db = context; }