Exemple #1
0
        public bool EditProduct(int productId, ProductViewModel productModel)
        {
            var product = _context.Products.Find(productId);

            if (product != null)
            {
                product.ImageUrl = productModel.ImageUrl;
                product.Name     = productModel.Name;
                product.Price    = productModel.Price;
                product.Type     = productModel.Type;

                _context.Products.Update(product);

                _context.SaveChanges();

                foreach (var ingrId in productModel.Ingredients)
                {
                    ProductIngredient prodIng = new ProductIngredient();
                    prodIng.ProductId      = product.Id;
                    prodIng.IngredientId   = ingrId.IngredientId;
                    prodIng.IngredientName = _context.Ingredients.Find(ingrId.IngredientId).Name;
                    prodIng.Quantity       = ingrId.Quantity;

                    _context.ProductIngredients.Update(prodIng);
                    _context.SaveChanges();
                }

                return(true);
            }
            return(false);
        }
Exemple #2
0
 public IActionResult AddIngredientToProduct(ProductIngredient data)
 {
     try
     {
         if (ModelState.IsValid)
         {
             _repository.AddProductIngredient(data);
             ViewBag.Ingredients = _repository.GetAllIngredients().Select(x => new SelectListItem {
                 Text = x.Name, Value = x.Id.ToString()
             }).ToList();
             return(Json(new { status = "ok", productId = data.ProductId })); //PartialView("_AddDish", new Dish());
         }
         else
         {
             ViewBag.Ingredients = _repository.GetAllIngredients().Select(x => new SelectListItem {
                 Text = x.Name, Value = x.Id.ToString()
             }).ToList();
             return(PartialView("_AddIngredient", data));
         }
     }
     catch (Exception ex)
     {
         return(View("Error", ex));
     }
 }
Exemple #3
0
        public async Task <IActionResult> PostProduct([FromBody] Product product)
        {
            ModelState.Remove("User");

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            string userName = User.Identity.Name;
            User   user     = _context.User.Single(u => u.UserName == userName);

            product.User = user;

            _context.Product.Add(product);
            foreach (Ingredient ingredient in product.Ingredients)
            {
                var productIngredient = new ProductIngredient()
                {
                    IngredientId = ingredient.IngredientId,
                    ProductId    = product.ProductId
                };
                _context.ProductIngredient.Add(productIngredient);
            }
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetProduct", new { id = product.ProductId }, product));
        }
Exemple #4
0
        public EntityEntry <Product> CreateProduct(ProductViewModel productModel)
        {
            Product product = new Product()
            {
                ImageUrl = productModel.ImageUrl,
                Name     = productModel.Name,
                Price    = productModel.Price,
                Type     = productModel.Type
            };

            var result = _context.Products.Add(product);

            if (result != null)
            {
                _context.SaveChanges();

                foreach (var ingrId in productModel.Ingredients)
                {
                    ProductIngredient prodIng = new ProductIngredient();
                    prodIng.ProductId      = result.Entity.Id;
                    prodIng.IngredientId   = ingrId.IngredientId;
                    prodIng.IngredientName = _context.Ingredients.Find(ingrId.IngredientId).Name;
                    prodIng.Quantity       = ingrId.Quantity;

                    _context.ProductIngredients.Add(prodIng);
                    _context.SaveChanges();
                }

                return(result);
            }
            return(null);
        }
        public async Task <bool> SetIngredientsToProduct(Product product, List <IngredientServiceModel> ingredients)
        {
            List <string> allIngredientsNames = new List <string>();
            await Task.Run(() => allIngredientsNames = ingredients.Select(ingredient => ingredient.Name).ToList());

            List <Ingredient> ingredientsDb = await this.context.Ingredients
                                              .Where(ingredient => allIngredientsNames.Contains(ingredient.Name))
                                              .ToListAsync();

            await SetIngredientMacronutrientsToProduct(product, ingredientsDb);

            for (int i = 0; i < ingredientsDb.Count; i++)
            {
                ProductIngredient productIngredient = new ProductIngredient
                {
                    Product    = product,
                    Ingredient = ingredientsDb[i]
                };

                await this.context.ProductIngredients.AddAsync(productIngredient);
            }

            var result = await this.context.SaveChangesAsync();

            return(result > num);
        }
Exemple #6
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,ProductId,IngredientId,Quantity")] ProductIngredient productIngredient)
        {
            if (id != productIngredient.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(productIngredient);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ProductIngredientExists(productIngredient.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["IngredientId"] = new SelectList(_context.Ingredient, "Id", "Name", productIngredient.IngredientId);
            ViewData["ProductId"]    = new SelectList(_context.Product, "Id", "Name", productIngredient.ProductId);
            return(View(productIngredient));
        }
 public void Update(ProductIngredient product_ingredient)
 {
     using (IDbConnection dbConnection = Connection)
     {
         dbConnection.Open();
         dbConnection.Execute("Team2.UpdateProductIngredient", product_ingredient, commandType: CommandType.StoredProcedure);
     }
 }
 public int Insert(ProductIngredient product_ingredient)
 {
     using (IDbConnection dbConnection = Connection)
     {
         dbConnection.Open();
         return(dbConnection.Query <InsertID>("Team2.AddProductIngredient", product_ingredient, commandType: CommandType.StoredProcedure).First().ID);
     }
 }
Exemple #9
0
        public int GetHashCode(ProductIngredient obj)
        {
            if (Object.ReferenceEquals(obj, null))
            {
                return(0);
            }

            int hashProductCode = obj.IngredientId.GetHashCode();

            return(hashProductCode);
        }
Exemple #10
0
        public ActionResult LoadIngredientIngredient(string Id)
        {
            IngredientFactory IngFactory = new IngredientFactory();
            var listProductRecipe        = _factory.GetListRecipeIngredient(Id);

            RecipeProductIngredientViewModels model = new RecipeProductIngredientViewModels();
            var listIng = IngFactory.GetIngredientUnlessData(Id);
            //listIng = listIng.Where(x => x.IsActive == true).ToList();

            var m_CompanyIds = GetListCompany().Select(x => x.Value).ToList();

            if (m_CompanyIds.Count > 0)
            {
                listIng = listIng.Where(x => m_CompanyIds.Contains(x.CompanyId)).ToList();
            }

            foreach (var item in listIng)
            {
                var ProIngre = new ProductIngredient()
                {
                    BaseUOM        = item.BaseUOMName,
                    IngredientId   = item.Id,
                    IngredientName = item.Name,

                    Usage = listProductRecipe.Where(x => x.IngredientId.Equals(item.Id)).FirstOrDefault() == null
                                                    ? 0 : Math.Round(listProductRecipe.FirstOrDefault(x => x.IngredientId.Equals(item.Id)).Usage, 4),
                    BaseUsage = listProductRecipe.Where(x => x.IngredientId.Equals(item.Id)).FirstOrDefault() == null
                                                    ? 0 : Math.Round(listProductRecipe.FirstOrDefault(x => x.IngredientId.Equals(item.Id)).BaseUsage, 4),

                    BaseUOMId = listProductRecipe.Where(x => x.IngredientId.Equals(item.Id)).FirstOrDefault() == null
                                                    ? item.BaseUOMId : listProductRecipe.FirstOrDefault(x => x.IngredientId.Equals(item.Id)).UOMId,

                    IsSelect = listProductRecipe.Any(x => x.IngredientId.Equals(item.Id))
                };
                var lstItem = _UOMFactory.GetDataUOMRecipe(item.Id).ToList();
                if (lstItem != null)
                {
                    foreach (UnitOfMeasureModel uom in lstItem)
                    {
                        ProIngre.ListUOM.Add(new SelectListItem
                        {
                            Text     = uom.Name,
                            Value    = uom.Id,
                            Selected = uom.Id.Equals(ProIngre.BaseUOMId) == true ? true : false
                        });
                    }
                }

                model.ListItem.Add(ProIngre);
            }
            model.ListItem = model.ListItem.OrderByDescending(x => x.IsSelect ? 1 : 0).ThenBy(x => x.IngredientName).ToList();
            return(PartialView("_TableChooseIngredient", model));
        }
Exemple #11
0
        public async Task <IActionResult> Create([Bind("Id,ProductId,IngredientId,Quantity")] ProductIngredient productIngredient)
        {
            if (ModelState.IsValid)
            {
                _context.Add(productIngredient);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["IngredientId"] = new SelectList(_context.Ingredient, "Id", "Name", productIngredient.IngredientId);
            ViewData["ProductId"]    = new SelectList(_context.Product, "Id", "Name", productIngredient.ProductId);
            return(View(productIngredient));
        }
Exemple #12
0
        public bool Equals(ProductIngredient x, ProductIngredient y)
        {
            if (Object.ReferenceEquals(x, y))
            {
                return(true);
            }

            if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
            {
                return(false);
            }

            return(x.IngredientId == y.IngredientId);
        }
Exemple #13
0
        /// <summary>
        /// Load selected product and ingredient combination
        /// into form
        /// </summary>
        private void loadIntoForm()
        {
            if (cmbIngredients.SelectedItem != null && cmbProductName.SelectedItem != null)
            {
                ProductIngredient productIngredient = ProductIngredient.Retrieve(
                    (cmbProductName.SelectedItem as ProductItem).Id,
                    (cmbIngredients.SelectedItem as Ingredient).Id);

                if (productIngredient != null)
                {
                    txtQuantity.Text = productIngredient.Quantity.ToString();
                    selectUnitInForm(productIngredient.Unit);
                }
            }
        }
Exemple #14
0
        /// <summary>
        /// Delete ingredient row
        /// </summary>
        /// <param name="e"></param>
        private void DeleteRow(DataGridViewCellEventArgs e)
        {
            try
            {
                //Delete ingredient
                long        ingredientID = (long)dtgIngredients.Rows[e.RowIndex].Cells["IngredientID"].Value;
                ProductItem productItem  = (cmbProductName.SelectedItem as ProductItem);

                ProductIngredient productIngredient = new ProductIngredient();
                productIngredient.IngredientId  = ingredientID;
                productIngredient.ProductItemId = productItem.Id;
                productIngredient.Delete();
                dtgIngredients.Rows.Remove(dtgIngredients.Rows[e.RowIndex]);
            }
            catch
            {
                MessageBox.Show("Unable to delete ingredient");
            }
        }
Exemple #15
0
        /// <summary>
        /// Save Product Ingredients
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (validate())
            {
                try
                {
                    long ingredientId  = (cmbIngredients.SelectedItem as Ingredient).Id;
                    int  productItemID = (cmbProductName.SelectedItem as ProductItem).Id;

                    ProductIngredient subject = ProductIngredient.Retrieve(productItemID, ingredientId);



                    if (subject is null)
                    {
                        //Create new ingredient-product record
                        subject = new ProductIngredient();
                        subject.IngredientId  = ingredientId;
                        subject.ProductItemId = productItemID;
                        subject.Quantity      = double.Parse(txtQuantity.Text);
                        subject.Unit          = cmbUnits.SelectedItem as string;
                        subject.Create();
                        MessageBox.Show("Product ingredient added");
                    }
                    else
                    {
                        subject.Quantity = double.Parse(txtQuantity.Text);
                        subject.Unit     = cmbUnits.SelectedItem as string;
                        subject.Update();
                        MessageBox.Show("Product ingredient updated");
                    }
                }
                catch
                {
                    MessageBox.Show("Failed to add or update");
                }
            }
        }
Exemple #16
0
 public void RemoveIngredientFromProduct(ProductIngredient productIngredientToBeRemoved)
 {
     _context.ProductIngredients.Remove(productIngredientToBeRemoved);
     _context.SaveChanges();
 }
Exemple #17
0
 ///***Ingredient Actions***///
 public void AddIngredientToProduct(ProductIngredient productIngredient)
 {
     _context.ProductIngredients.Add(productIngredient);
     _context.SaveChanges();
 }
Exemple #18
0
 public void Update(ProductIngredient productIngredient)
 {
     _repository.Update(productIngredient);
 }
Exemple #19
0
 public void Delete(ProductIngredient productIngredient)
 {
     _repository.Delete(productIngredient);
 }
Exemple #20
0
 public int Insert(ProductIngredient productIngredient)
 {
     return(_repository.Insert(productIngredient));
 }
Exemple #21
0
 public void AddProductIngredient(ProductIngredient data)
 {
     _context.ProductIngredients.Add(data);
     _context.SaveChanges();
     UpdateProduct(data.ProductId);
 }
Exemple #22
0
        public void Create( IngredientsViewModel ingredient)
        {
            if (ModelState.IsValid)
            {

                Ingredient item = new Ingredient
                {
                    CategoryID = Convert.ToInt16(ingredient.CategoryName),
                    IngredientID = (db.Ingredient.Max(x => x.IngredientID)+1),
                    Title = ingredient.Title,
                    LongDescription = ingredient.LongDescription,
                    Cost = ingredient.Cost,
                    Weight = ingredient.Weight
                };

                db.Ingredient.Add(item);
                db.SaveChanges();

                if (ingredient.SubIngredient != null)
                    foreach (Tmodel t in ingredient.SubIngredient)
                    {
                        ProductIngredient subIngredient = new ProductIngredient();
                        subIngredient.ProductID = item.IngredientID;
                        subIngredient.IngredientID = t.id;
                        subIngredient.ProductIngredientID = (db.ProductIngredient.Max(x => x.ProductIngredientID) + 1);
                        db.ProductIngredient.Add(subIngredient);
                        db.SaveChanges();
                    }
            }
            // действия по добавлению
        }
Exemple #23
0
 /// <summary>
 /// Load ingredients of a product item into the datagrid
 /// </summary>
 /// <param name="productItem">Product item whose ingredients we want</param>
 private void loadDataGrid(ProductItem productItem)
 {
     ingredientSource.DataSource = ProductIngredient.GetIngredientsTable(productItem.Id);
 }
Exemple #24
0
        public void Edit( IngredientsViewModel ingredient)
        {
            Ingredient item = new Ingredient
            {
                CategoryID = Convert.ToInt16(ingredient.CategoryName),
                IngredientID = ingredient.IngredientID,
                Title = ingredient.Title,
                LongDescription = ingredient.LongDescription,
                Cost = ingredient.Cost,
                Weight = ingredient.Weight
            };

            if (ModelState.IsValid)
            {
                db.Entry(item).State = EntityState.Modified;
                db.SaveChanges();

                if (ingredient.SubIngredient != null)
                    foreach (Tmodel t in ingredient.SubIngredient)
                    {
                        ProductIngredient subIngredient = new ProductIngredient();
                        subIngredient.ProductID = item.IngredientID;
                        subIngredient.IngredientID = t.id;
                        subIngredient.ProductIngredientID = (db.ProductIngredient.Max(x => x.ProductIngredientID) + 1);
                        db.ProductIngredient.Add(subIngredient);
                        db.SaveChanges();

                    }

            }
        }
        public async Task PopulateDbWithProductsAsync(int productsCount)
        {
            var concurrentBag = new ConcurrentBag <ProductDto>();

            for (var j = 61; j < 63; j++)
            {
                for (var i = 1148; i < productsCount; i++)
                {
                    try
                    {
                        var product = this.GetProduct(i, j);
                        concurrentBag.Add(product);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }

                foreach (var product in concurrentBag)
                {
                    var categoryId = await this.GetOrCreateCategoryAsync(product.CategoryName);

                    var productExists = this.productsRepository
                                        .AllAsNoTracking()
                                        .Any(x => x.Name == product.ProductName);

                    if (productExists)
                    {
                        continue;
                    }

                    var newProduct = new Product()
                    {
                        Name        = product.ProductName,
                        Brand       = product.Brand,
                        ProductCode = product.ProductCode,
                        Stock       = product.Stock,
                        Description = product.ProductDescription,
                        Content     = product.Content,
                        Price       = product.Price,
                        OriginalUrl = product.OriginalUrl,
                        CategoryId  = categoryId,
                    };

                    await this.productsRepository.AddAsync(newProduct);

                    await this.productsRepository.SaveChangesAsync();

                    foreach (var item in product.Ingredients)
                    {
                        if (item.Length < 2)
                        {
                            continue;
                        }

                        var ingredientId = await this.GetOrCreateIngredientAsync(item.Trim());

                        var productIngredient = new ProductIngredient()
                        {
                            IngredientId = ingredientId,
                            ProductId    = newProduct.Id,
                        };

                        await this.productIngredientsRepository.AddAsync(productIngredient);

                        await this.productIngredientsRepository.SaveChangesAsync();
                    }

                    var image = new Image
                    {
                        RemoteImageUrl = product.ImageUrl,
                        ProductId      = newProduct.Id,
                    };

                    await this.imagesRepository.AddAsync(image);

                    await this.imagesRepository.SaveChangesAsync();
                }
            }
        }