public void AddProduct(Product product, string code, string newName, double pp, double sp) { using (var context = new TerminalsDB()) { if (context.Products.Find(product.Code) == null || context.Products.Find(product.Name) == null) { product = new Product { Code = code, Name = newName, Prices = new List <ProductPrice> { new ProductPrice { SellingPrice = sp, PurchasePrice = pp, DateFrom = DateTime.Today } } }; context.Products.Add(product); context.SaveChanges(); ProductsChanged?.Invoke(product); } } }
public void EditTerminal(VMachine vMachine, string newLocation) { using (var context = new TerminalsDB()) { var terminalFromDB = context.VMachines.Find(vMachine.Id); terminalFromDB.Location = newLocation; context.SaveChanges(); VMChanged?.Invoke(terminalFromDB); } }
public void EditProduct(Product product, string newName) { using (var context = new TerminalsDB()) { DateTime dt = DateTime.Today.AddDays(1); var prodFromDB = context.Products.Find(product.Code); prodFromDB.Name = newName; context.SaveChanges(); ProductsChanged?.Invoke(prodFromDB); } }
public void DeleteProduct(Product product) { using (var context = new TerminalsDB()) { if (context.VMachines.Any(vm => vm.Products.Any(p => p.Product.Code == product.Code))) { throw new Exception("This product cannot be deleted beacuse it is included into Terminal!"); } var deletedProduct = context.Products.Include(p => p.Prices).First(p => p.Code == product.Code); deletedProduct.Prices.Clear(); context.Products.Remove(deletedProduct); context.SaveChanges(); } }
public void AddTerminal(string location) { using (var context = new TerminalsDB()) { if (context.VMachines.Any(v => v.Location == location)) { throw new Exception("Vending machine with this location already exists!"); } var vMachine = new VMachine { Location = location }; vMachine.Money = DataForTerminal.GetMoney(); context.VMachines.Add(vMachine); context.SaveChanges(); VMChanged?.Invoke(vMachine); } }
public void EditPrice(Product product, double sp, double pp, DateTime dt) { using (var context = new TerminalsDB()) { var prodFromDB = context.Products.Find(product.Code); if (product.LastPrice.SellingPrice != sp && product.LastPrice.PurchasePrice != pp) { prodFromDB.Prices = new List <ProductPrice> { new ProductPrice { PurchasePrice = pp, SellingPrice = sp, DateFrom = dt } }; context.SaveChanges(); ProductsChanged?.Invoke(prodFromDB); } } }