public int AddProduct(string productName, double price) { using (var db = new Context()) { if (db.FindStoreProduct(Store.StoreName, productName) != null) { return(-1); } Product product; if ((product = db.FindProduct(productName)) == null) { product = new Product() { ProductName = productName }; db.Products.Add(product); } var store = db.Stores.Find(Store.StoreId); var storprod = new StoreProduct() { Price = price, Product = product, ProductId = product.ProductId, Store = store, StoreId = store.StoreId }; db.StoreProducts.Add(storprod); db.SaveChanges(); return(0); } }
public Store FindCheapestStore(string productName) { using (var db = new Database.Context()) { var product = db.FindProduct(productName); if (product == null) { return(null); //Produktet findes ikke i databasen } var cheapest = product.StoreProducts.First(); foreach (var storprod in product.StoreProducts) { if (storprod.Price < cheapest.Price) { cheapest = storprod; } } return(cheapest.Store); } }