public RecipeDetailViewModel(int recipeId)
        {
            recipeApi = new RecipeAPI();
            var recipe = recipeApi.Get(recipeId);

            this.recipeId    = recipe.Id;
            this.Name        = recipe.Name;
            this.Description = recipe.Description;
            var products = recipe.Products.Select(x => new ProductRecipe()
            {
                ProductName = x.ProductName,
                Value       = x.Value
            });

            Products = new ObservableCollection <ProductRecipe>(products);
            var recipeLocation = recipeApi.GetLocations(recipeId);
            var locations      = recipeLocation.Locations.Select(x => new Model.Location()
            {
                Name = x.Name
            });

            Locations   = new ObservableCollection <Model.Location>(locations);
            EditCommand = new Command(Edit);
        }
Exemple #2
0
        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
        }