public List <Product> All() { List <Product> productList = new List <Product>(); try { using (prac5_dbEntities db = new prac5_dbEntities()) { IQueryable <Product> products = from p in db.productSets where p.stock > 0 select new Product { Name = p.name, Stock = p.stock, Price = p.price, Id = p.Id }; productList = products.ToList(); } } catch { throw new Exception("Could not fetch the products"); } return(productList); }
public Customer RegisterCustomer(string newUsername) { var pwd = new Password().IncludeLowercase().IncludeUppercase().IncludeSpecial(); var newPassword = pwd.Next(); // TODO check if customer exists / create customer if so and redirect to product screen logged in? Customer x = this.FindByUsername(newUsername); if (x == null) { customerSet newCustomer = new customerSet { username = newUsername, password = newPassword, balance = new Random().Next(5, 105) }; using (prac5_dbEntities db = new prac5_dbEntities()) { customerSet c = db.customerSets.Add(newCustomer); db.SaveChanges(); return(new Customer { Id = c.Id, UserName = c.username, Balance = c.balance, Password = c.password }); } } else { throw new FaultException <CustomerFaultService>(new CustomerFaultService("User bestaat al...")); } }
public Customer Find(int id) { using (prac5_dbEntities db = new prac5_dbEntities()) { return((from c in db.customerSets where c.Id == id select new Customer { Id = c.Id, UserName = c.username, Balance = c.balance }).FirstOrDefault()); } }
public List <BoughtProduct> PerProductByCustomer(int CustomerId) { using (prac5_dbEntities db = new prac5_dbEntities()) { return(db.orderLineSets .Where(ol => ol.orderSet.customer_Id == CustomerId) .GroupBy(ol => ol.product_Id) .Select(olg => new BoughtProduct { ProductId = olg.FirstOrDefault().product_Id, Name = olg.FirstOrDefault().productSet.name, Amount = olg.Sum(l => l.amount), }).ToList()); } }
public Customer FindByUsername(string username) { using (prac5_dbEntities db = new prac5_dbEntities()) { return((from c in db.customerSets where c.username == username select new Customer { Id = c.Id, UserName = c.username, Balance = c.balance, Password = c.password }).FirstOrDefault()); } }
public Customer LoginCustomer(string username, string password) { using (prac5_dbEntities db = new prac5_dbEntities()) { try { return((from c in db.customerSets where c.username == username && c.password == password select new Customer { Id = c.Id, UserName = c.username, Balance = c.balance, Password = c.password }).First()); } catch (Exception err) { return(null); } } }
public Product Find(int id) { try { using (prac5_dbEntities db = new prac5_dbEntities()) { return((from p in db.productSets where p.Id == id select new Product { Name = p.name, Stock = p.stock, Price = p.price, Id = p.Id }).First()); } } catch { throw new Exception("Could not find the product"); } }
public bool Order(int customerId, int storeId, List <BuyingProduct> products) { orderSet Order = new orderSet { customer_Id = customerId, date = DateTime.Now, }; using (prac5_dbEntities db = new prac5_dbEntities()) { customerSet customer = (from c in db.customerSets where c.Id == customerId select c).First(); double price = ( from p2 in products select p2.Amount * ( from p in db.productSets where p.Id == p2.Id select p.price ).First() ).Sum(); int lowestStock = ( from p2 in products select( from p in db.productSets where p.Id == p2.Id select p.stock ).First() - p2.Amount ).Min(); if (lowestStock < 0) { throw new FaultException <OrderServiceFault>(new OrderServiceFault("Not enough Stok")); } if (price > customer.balance) { throw new FaultException <OrderServiceFault>(new OrderServiceFault("Not enough balance to pay for this.")); } customer.balance -= price; db.customerSets.AddOrUpdate(customer); orderSet SavedOrder = db.orderSets.Add(Order); foreach (BuyingProduct bp in products) { productSet product = (from p in db.productSets where p.Id == bp.Id select p).First(); product.stock -= bp.Amount; orderLineSet orderLine = new orderLineSet { amount = bp.Amount, order_Id = SavedOrder.Id, product_Id = bp.Id, }; db.orderLineSets.Add(orderLine); db.productSets.AddOrUpdate(product); } db.SaveChanges(); } return(true); }