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 ProductViewModel() { ProductsAPI = new ProductsAPI(); UnitAPI = new UnitAPI(); ProductCategoryAPI = new ProductCategoryAPI(); var products = ProductsAPI.GetAll(); var productviewModel = products.Select(x => new Model.Product() { ProductId = x.Id, Name = x.Name, Description = x.Description, UnitId = x.UnitId, Unit = x.Unit, CategoryId = x.ProductCategoryId, Category = x.ProductCategory, Detail = new Command(() => { DetailShow(x.Id); } ), Delete = new Command(() => { Detele(x.Id); }), Edit = new Command(() => { Edit(x.Id); }) }); Products = new ObservableCollection <Model.Product>(productviewModel); }
public RecipeCreateViewModel() { productApi = new ProductsAPI(); var products = productApi.GetAll().Select(x => new ProductRecipe() { ProductId = x.Id, ProductName = x.Name, Value = 0, AddCommand = new Command((() => { AddProduct(x.Id); })), RemoveCommand = new Command((() => { RemoveProduct(x.Id); })) }); Products = new ObservableCollection <ProductRecipe>(products); ProductsRecipe = new ObservableCollection <ProductRecipe>(); locationApi = new LocationAPI(); var locations = locationApi.GetAll().Select(x => new Model.Location() { Id = x.Id, Name = x.Name, AddCommand = new Command(() => { AddLocation(x.Id); }), RemoveCommand = new Command((() => { RemoveLocation(x.Id); })) }); Locations = new ObservableCollection <Model.Location>(locations); LocationsRecipe = new ObservableCollection <Model.Location>(); SaveCommand = new Command((() => { this.Save(); })); }
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("Во время сохранения произошла ошибка."); } }); }
public RecipeEditViewModel(int recipeId) { this.recipeId = recipeId; recipeApi = new RecipeAPI(); var recipeProduct = recipeApi.Get(this.recipeId); this.name = recipeProduct.Name; this.description = recipeProduct.Description; SaveCommand = new Command(Save); #region Продукты var productsRecipe = recipeProduct.Products.Select(x => new Model.ProductRecipe() { ProductId = x.ProductId, ProductName = x.ProductName, Value = x.Value, AddCommand = new Command((() => AddProduct(x.ProductId))), RemoveCommand = new Command((() => RemoveProduct(x.ProductId))) }); ProductsRecipe = new ObservableCollection <ProductRecipe>(productsRecipe); var productsApi = new ProductsAPI(); var products = productsApi.GetAll().Select(x => new ProductRecipe() { ProductId = x.Id, ProductName = x.Name, Value = 0, AddCommand = new Command((() => AddProduct(x.Id))), RemoveCommand = new Command((() => RemoveProduct(x.Id))) }); Products = new ObservableCollection <ProductRecipe>(); foreach (var product in products) { if (ProductsRecipe.All(p => p.ProductId != product.ProductId)) { Products.Add(product); } } #endregion #region Локации var recipeLocations = recipeApi.GetLocations(this.recipeId); var locationsRecipe = recipeLocations.Locations.Select(x => new Model.Location() { Id = x.Id, Name = x.Name, AddCommand = new Command((() => AddLocation(x.Id))), RemoveCommand = new Command((() => RemoveLocation(x.Id))) }); LocationsRecipe = new ObservableCollection <Model.Location>(locationsRecipe); var locationApi = new LocationAPI(); var locations = locationApi.GetAll().Select(x => new Model.Location() { Id = x.Id, Name = x.Name, AddCommand = new Command((() => AddLocation(x.Id))), RemoveCommand = new Command((() => RemoveLocation(x.Id))) }); Locations = new ObservableCollection <Model.Location>(); foreach (var location in locations) { if (LocationsRecipe.All(l => l.Id != location.Id)) { Locations.Add(location); } } #endregion }
public ProductsController(ProductsAPI products, CategoriesAPI categories) { productsContext = products; categoriesContext = categories; }