Пример #1
0
 public ActionResult Add(AddProductViewModel product)
 {
     if (!ModelState.IsValid)
     {
         var viewModel = new AddProductViewModel
         {
             ProductName   = product.ProductName,
             CaloricValue  = product.CaloricValue,
             Fat           = product.Fat,
             Proteins      = product.Proteins,
             Carbohydrates = product.Carbohydrates
         };
         return(View("Add", viewModel));
     }
     _productProvider.AddProduct(product);
     return(RedirectToAction("Index"));
 }
Пример #2
0
        // POST api/values
        public HttpResponseMessage Post([FromBody] Product product)
        {
            HttpResponseMessage response;

            if (product == null || string.IsNullOrWhiteSpace(product.ProductName))
            {
                response = Request.CreateResponse(HttpStatusCode.BadRequest);
                response.ReasonPhrase = "Null product or empty product name";
            }
            else
            {
                product  = _productProvider.AddProduct(product);
                response = Request.CreateResponse(HttpStatusCode.Created, product);
            }

            return(response);
        }
Пример #3
0
 public ActionResult Create(ProductsItemsAddViewModel item)
 {
     if (ModelState.IsValid)
     {
         _context.AddProduct(item);
         return(RedirectToAction("Index"));
     }
     else
     {
         item.CategoryList = _context2.Get_All()
                             .Select(r => new SelectItemViewModel
         {
             Id   = r.Id,
             Name = r.Name
         }).ToList();
     }
     return(View(item));
 }
Пример #4
0
        private void ButtonAdd_Click(object sender, RoutedEventArgs e)
        {
            ProductAddViewModel product = new ProductAddViewModel()
            {
                Name       = TxtBoxName.Text,
                CategoryId = (ComboCategory.SelectedItem as Category).Id,
                Price      = float.Parse(TxtBoxPrice.Text),
                Quantity   = int.Parse(TxtBoxQty.Text),
                Images     = Photos
            };
            var prod = _productProvider.AddProduct(product);

            if (prod != null)
            {
                MessageBox.Show($"Продукт успешно сохранен. ID = {prod.Id.ToString()}");
            }

            this.Close();
        }
Пример #5
0
        public async Task <IActionResult> AddProduct([FromBody] AddProductModel model)
        {
            var product = await _productProvider.AddProduct(model.Name, model.Description);

            return(Created($"product/{product.Id}", product));
        }
Пример #6
0
 public void AddCustomProduct(Product product)
 {
     _productProvider.AddProduct(product);
 }
Пример #7
0
        private void ButtonAdd_Click(object sender, RoutedEventArgs e)
        {
            ProductAddViewModel product = new ProductAddViewModel()
            {
                Name       = TxtBoxName.Text,
                CategoryId = (ComboCategory.SelectedItem as Category).Id,
                Price      = float.Parse(TxtBoxPrice.Text),
                Quantity   = int.Parse(TxtBoxQty.Text),
                Images     = Photos
            };
            var prod = _productProvider.AddProduct(product);

            if (prod != null)
            {
                MessageBox.Show($"Продукт успешно сохранен. ID = {prod.Id.ToString()}");
            }
            Photos.Clear();
            //Photos.Add(new Photo() { });
            this.Close();
            #region OldAddCode
            //foreach (var p in Photos)
            //{
            //    MessageBox.Show(p.SourceOriginal);
            //}

            //using (EfContext context = new EfContext())
            //{
            //Product product = new Product
            //{
            //    Name = TxtBoxName.Text,
            //    CategoryId = (ComboCategory.SelectedItem as Category).Id,
            //    Price = float.Parse(TxtBoxPrice.Text),
            //    Quantity = int.Parse(TxtBoxQty.Text),
            //    DateCreate = DateTime.Now,
            //};
            //context.Products.Add(product);
            //context.SaveChanges();
            //MessageBox.Show($"Id = {product.Id.ToString()}");
            //if (Photos.Count > 0)
            //{

            //    List<string> imgTemp = new List<string>();
            //    foreach (var p in Photos)
            //    {
            //        //MessageBox.Show(p.SourceOriginal);
            //        string fOriginalName = Path.GetFileName(p.SourceOriginal);
            //        string fOriginalPath = Path.GetDirectoryName(p.SourceOriginal);
            //        string fSmallName = Path.GetFileName(p.Source);
            //        string fSmallPath = Environment.CurrentDirectory + ConfigurationManager.AppSettings["ImagePath"].ToString();
            //        string fImages = Environment.CurrentDirectory + ConfigurationManager.AppSettings["ImageStore"].ToString();
            //        // Will not overwrite if the destination file already exists.
            //        try
            //        {
            //            File.Copy(Path.Combine(fOriginalPath, fOriginalName), Path.Combine(fImages, "o_" + fSmallName));
            //            imgTemp.Add("o_" + fSmallName);
            //            File.Copy(Path.Combine(fSmallPath, fSmallName), Path.Combine(fImages, "s_" + fSmallName));
            //            imgTemp.Add("s_" + fSmallName);

            //            ProductImage image = new ProductImage // сохраняем в таблицу ProductImages
            //            {
            //                Name = fSmallName,
            //                ProductId = product.Id
            //            };
            //            context.ProductImages.Add(image);

            //            // Catch exception if the file was already copied.
            //        }
            //        catch (IOException copyError)
            //        {
            //            MessageBox.Show(copyError.Message);
            //            foreach (var item in imgTemp)
            //            {
            //                File.Delete(Path.Combine(fImages, item));
            //            }
            //            return;
            //        }

            //    }
            //    context.SaveChanges();
            //}
            //}
            #endregion
            //this.DialogResult = true;
        }