private Manufacture CreateModel(ManufactureBindingModel model, Manufacture product) { product.ProductName = model.ProductName; product.Price = model.Price; // удаляем убранные foreach (var key in product.ProductComponents.Keys.ToList()) { if (!model.ProductComponents.ContainsKey(key)) { product.ProductComponents.Remove(key); } } // обновляем существуюущие и добавляем новые foreach (var component in model.ProductComponents) { if (product.ProductComponents.ContainsKey(component.Key)) { product.ProductComponents[component.Key] = model.ProductComponents[component.Key].Item2; } else { product.ProductComponents.Add(component.Key, model.ProductComponents[component.Key].Item2); } } return(product); }
public void Delete(ManufactureBindingModel model) { for (int i = 0; i < source.Products.Count; ++i) { if (source.Products[i].Id == model.Id) { source.Products.RemoveAt(i); return; } } throw new Exception("Элемент не найден"); }
public void Delete(ManufactureBindingModel model) { var element = _manufactureStorage.GetElement(new ManufactureBindingModel { Id = model.Id }); if (element == null) { throw new Exception("Продукт не найден"); } _manufactureStorage.Delete(model); }
public List <ManufactureViewModel> Read(ManufactureBindingModel model) { if (model == null) { return(_manufactureStorage.GetFullList()); } if (model.Id.HasValue) { return(new List <ManufactureViewModel> { _manufactureStorage.GetElement(model) }); } return(_manufactureStorage.GetFilteredList(model)); }
public ManufactureViewModel GetElement(ManufactureBindingModel model) { if (model == null) { return(null); } foreach (var product in source.Products) { if (product.Id == model.Id || product.ProductName == model.ProductName) { return(CreateModel(product)); } } return(null); }
public void Update(ManufactureBindingModel model) { Manufacture tempProduct = null; foreach (var product in source.Products) { if (product.Id == model.Id) { tempProduct = product; } } if (tempProduct == null) { throw new Exception("Элемент не найден"); } CreateModel(model, tempProduct); }
public List <ManufactureViewModel> GetFilteredList(ManufactureBindingModel model) { if (model == null) { return(null); } List <ManufactureViewModel> result = new List <ManufactureViewModel>(); foreach (var product in source.Products) { if (product.ProductName.Contains(model.ProductName)) { result.Add(CreateModel(product)); } } return(result); }
public void Insert(ManufactureBindingModel model) { Manufacture tempProduct = new Manufacture { Id = 1, ProductComponents = new Dictionary <int, int>() }; foreach (var product in source.Products) { if (product.Id >= tempProduct.Id) { tempProduct.Id = product.Id + 1; } } source.Products.Add(CreateModel(model, tempProduct)); }
public void CreateOrUpdate(ManufactureBindingModel model) { var element = _manufactureStorage.GetElement(new ManufactureBindingModel { ProductName = model.ProductName }); if (element != null && element.Id != model.Id) { throw new Exception("Уже есть продукт с таким названием"); } if (model.Id.HasValue) { _manufactureStorage.Update(model); } else { _manufactureStorage.Insert(model); } }