public UnitViewModel() { unitApi = new UnitAPI(); var units = unitApi.GetAll().Select(x => new Model.Unit() { Name = x.Name, Symbol = x.Symbol, UnitId = x.Id, Delete = new Command(() => { Delete(x.Id); }), Edit = new Command(() => { Edit(x.Id); }) }); Units = new ObservableCollection <Model.Unit>(units); Create = new Command((() => { var view = new UnitCreateView(); view.Show(); })); }
public ProductCreateViewModel() { unitAPI = new UnitAPI(); productCategoryAPI = new ProductCategoryAPI(); productsAPI = new ProductsAPI(); Units = unitAPI.GetAll().Select(x => new Model.Unit() { Name = x.Symbol, UnitId = x.Id }).ToList(); ProductCategories = productCategoryAPI.GetAll().Select(x => new ProductCategory() { Name = x.Name, Id = x.Id }).ToList(); Create = new Command(() => { var model = new ProductDTO(); model.Name = this.name; model.Description = this.description; model.UnitId = SelectedUnit.UnitId; model.ProductCategoryId = SelectProductCategory.Id; var resultFlag = productsAPI.Create(model); if (resultFlag) { MessageBox.Show("Продукт успешно создан"); } if (!resultFlag) { MessageBox.Show("Во время создания произошла ошибка"); } }); }
public ProductEditViewModel(Product product) { UnitAPI = new UnitAPI(); ProductCategoryAPI = new ProductCategoryAPI(); this.id = product.ProductId; this.name = product.Name; this.description = product.Description; this.Units = UnitAPI.GetAll().Select(x => new Model.Unit() { UnitId = x.Id, Name = x.Name }).ToList(); this.SelectedUnit = Units.Single(x => x.UnitId == product.UnitId); this.ProductCategory = ProductCategoryAPI.GetAll().Select(x => new ProductCategory() { Name = x.Name, Id = x.Id }).ToList(); this.SelectedCategory = ProductCategory.Single(x => x.Id == product.CategoryId); Update = new Command(() => { ProductsAPI productAPI = new ProductsAPI(); var productDTO = new ProductDTO(); productDTO.Name = Name; productDTO.Description = Description; productDTO.Id = id; productDTO.UnitId = SelectedUnit.UnitId; productDTO.ProductCategoryId = SelectedCategory.Id; var resultFlag = productAPI.Update(productDTO); if (resultFlag) { MessageBox.Show("Успешно сохранено."); } if (!resultFlag) { MessageBox.Show("Во время сохранения произошла ошибка."); } }); }