public async Task <Product> Create(Product product)
        {
            _context.Add(product);
            await _context.SaveChangesAsync();

            return(product);
        }
        public async Task <IActionResult> Create(Product product)
        {
            if (product is null)
            {
                return(BadRequest());
            }

            //model validation

            var newProduct = new Model.Product
            {
                Name        = product.Name,
                Description = product.Description,
                ImageUrl    = product.ImageUrl,
                Price       = product.Price,
                Supplier    = product.Supplier
            };

            _dbContext.Add(newProduct);
            await _dbContext.SaveChangesAsync();

            product.Id = newProduct.Id;

            return(CreatedAtAction(nameof(Get), new { id = newProduct.Id }, product));
        }
Esempio n. 3
0
        public async Task <IActionResult> Create([Bind("CategoryId,Name")] Category category)
        {
            if (ModelState.IsValid)
            {
                _context.Add(category);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(category));
        }
Esempio n. 4
0
        public async Task <IActionResult> Create([Bind("Id,Name,Price,Quantity,ShippingPrice")] Products products)
        {
            if (ModelState.IsValid)
            {
                _context.Add(products);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(products));
        }
Esempio n. 5
0
        public async Task <IActionResult> Create([Bind("SowRatesId,SowWeight,TraysPerPack,CostPerTray, ProductsId,DateIn,DateOut")] SowRatesL sowRatesL)
        {
            if (ModelState.IsValid)
            {
                _context.Add(sowRatesL);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(sowRatesL));
        }
Esempio n. 6
0
 public void AddProduct(Product product)
 {
     try
     {
         productDbContext.Add(product);
         productDbContext.SaveChanges(true);
     }
     catch (Exception ex)
     {
         throw new Exception("Cannot update data. " + ex.Message);
     }
 }
Esempio n. 7
0
        public async Task <IActionResult> Create([Bind("YieldId,Yield,CostPerTray,ProductsId,DateIn,DateOut")] Yields yields)
        {
            if (ModelState.IsValid)
            {
                _context.Add(yields);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ProductsId"] = new SelectList(_context.Products, "Id", "Id", yields.ProductsId);
            return(View(yields));
        }
Esempio n. 8
0
 private void SeedData()
 {
     if (!dbContext.Products.Any())
     {
         dbContext.Add(new Db.Product()
         {
             Id = 1, Name = "Keyword", Price = 20, Inventory = 100
         });
         dbContext.Add(new Db.Product()
         {
             Id = 2, Name = "Mouse", Price = 5, Inventory = 200
         });
         dbContext.Add(new Db.Product()
         {
             Id = 3, Name = "Monitor", Price = 150, Inventory = 1000
         });
         dbContext.Add(new Db.Product()
         {
             Id = 4, Name = "CPU", Price = 200, Inventory = 2000
         });
         dbContext.SaveChanges();
     }
 }
 private void CreateProducts(ProductsDbContext dbContext)
 {
     for (int i = 1; i <= 10; i++)
     {
         dbContext.Add(new Product()
         {
             Id        = i,
             Name      = Guid.NewGuid().ToString(),
             Inventory = i + 10,
             Price     = (decimal)(i * 3.145)
         });
     }
     dbContext.SaveChanges();
 }
Esempio n. 10
0
 public async void SaveList(List <T> list)
 {
     try
     {
         foreach (var record in list)
         {
             _context.Add(record);
         }
         await _context.SaveChangesAsync();
     }
     catch (Exception)
     {
         throw;
     }
 }
 private void SeedData(ProductsDbContext dbContext)
 {
     if (!dbContext.Products.Any())
     {
         for (var i = 1000; i < 1025; i++)
         {
             dbContext.Add(new Db.Product()
             {
                 Id        = i,
                 Name      = Guid.NewGuid().ToString(),
                 Inventory = i * 5,
                 Price     = (double)i * 45
             });
         }
         dbContext.SaveChanges();
     }
 }
        public async Task <IActionResult> Create([Bind("ProductId,Name,Description,CategoryProductId,Manufacturer,Supplier,Price")] Product product)
        {
            var max = _context.Products.FirstOrDefault();

            if (max == null)
            {
                product.ProductId = 1;
            }
            else
            {
                product.ProductId = _context.Products.Max(item => item.ProductId) + 1;
            }
            if (ModelState.IsValid)
            {
                _context.Add(product);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(product));
        }
Esempio n. 13
0
        public async Task <IActionResult> Create([Bind("CategoryProductId,CategoryName")] CategoryProduct categoryProduct)
        {
            var max = _context.CategoryProducts.FirstOrDefault();

            if (max == null)
            {
                categoryProduct.CategoryProductId = 1;
            }
            else
            {
                categoryProduct.CategoryProductId = _context.CategoryProducts.Max(item => item.CategoryProductId) + 1;
            }
            if (ModelState.IsValid)
            {
                _context.Add(categoryProduct);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(categoryProduct));
        }
Esempio n. 14
0
 public void AddProductAsync(Product product)
 {
     _context.Add(product);
     SaveAndUpdateContext();
 }