static void Main(string[] args) { using (PizzaShopDBEntities dbDriver = new PizzaShopDBEntities()) { Product p1 = new Product { Name = "Pizza Margherita", Price = 7.50, Amount = 100 }; Product p2 = new Product { Name = "Pizza Marinara", Price = 7.00, Amount = 100 }; Product p3 = new Product { Name = "Pizza Pepperoni", Price = 8.00, Amount = 100 }; Product p4 = new Product { Name = "Pizza Hawaii", Price = 8.00, Amount = 10 }; dbDriver.ProductSet.Add(p1); dbDriver.ProductSet.Add(p2); dbDriver.ProductSet.Add(p3); dbDriver.ProductSet.Add(p4); dbDriver.SaveChanges(); Customer c1 = new Customer { Username = "******", Password = "******", Money = 20 }; Customer c2 = new Customer { Username = "******", Password = "******", Money = 100 }; Customer c3 = new Customer { Username = "******", Password = "******", Money = 100 }; dbDriver.CustomerSet.Add(c1); dbDriver.CustomerSet.Add(c2); dbDriver.CustomerSet.Add(c3); dbDriver.SaveChanges(); } }
public OrderEntry Buy(int orderid, int productid, int custid) { using (PizzaShopDBEntities dbDriver = new PizzaShopDBEntities()) { dbDriver.Configuration.ProxyCreationEnabled = false; OrderEntry orderentry = (from o in dbDriver.OrderEntrySet where o.OrderId == orderid && o.Product_Id == productid select o).SingleOrDefault(); Customer cust = (from c in dbDriver.CustomerSet where c.Id == custid select c).SingleOrDefault(); Product product = (from p in dbDriver.ProductSet where p.Id == productid select p).SingleOrDefault(); if (product != null && cust.Money >= product.Price && product.Amount > 0) { if (orderentry == null) { OrderEntry oe = new OrderEntry { Amount = 1, OrderId = orderid, Product_Id = productid }; dbDriver.OrderEntrySet.Add(oe); cust.Money = cust.Money - product.Price; product.Amount -= 1; dbDriver.SaveChanges(); return(oe); } else { orderentry.Amount += 1; cust.Money = cust.Money - product.Price; product.Amount -= 1; dbDriver.SaveChanges(); return(orderentry); } } else { return(null); } } }
public string Register(string username) { using (PizzaShopDBEntities dbDriver = new PizzaShopDBEntities()) { var customer = (from c in dbDriver.CustomerSet where c.Username == username select c).SingleOrDefault(); if (customer == null) { char[] charArray = username.ToCharArray(); Array.Reverse(charArray); string password = new string(charArray); Customer c1 = new Customer { Username = username, Password = password, Money = 20 }; dbDriver.CustomerSet.Add(c1); dbDriver.SaveChanges(); return(password); } else { return("Username already exists!"); } } }
public Order NewOrder(int custid) { using (PizzaShopDBEntities dbDriver = new PizzaShopDBEntities()) { Order o1 = new Order { OrderDate = DateTime.Now, CustomerId = custid }; dbDriver.OrderSet.Add(o1); dbDriver.SaveChanges(); return(o1); } }