//private static List<Product> products = new List<Product>
 //{
 //    new Product{Id = 0, Name = "Coffee", Price = 10},
 //    new Product{Id = 1, Name = "Tea", Price = 15}
 //};
 public Product AddProduct(Product product)
 {
     //products.Add(product);
     _dbContext.Products.Add(product);
     _dbContext.SaveChanges();
     return(product);
 }
Exemple #2
0
 public void Post([FromBody] Product product)
 {
     if (ModelState.IsValid)
     {
         context.Products.Add(product);
         context.SaveChanges();
     }
 }
Exemple #3
0
 public void AddUserCompany(UserCompany userCompany)
 {
     if (userCompany != null)
     {
         context.UserCompany.Add(userCompany);
     }
     context.SaveChanges();
 }
 public void AddWorkStock(WorkStock workStock)
 {
     if (workStock != null)
     {
         context.WorkStock.Add(workStock);
     }
     context.SaveChanges();
 }
 public void AddCompStock(CompanyStock company)
 {
     if (company != null)
     {
         productsDb.CompanyStock.Add(company);
     }
     productsDb.SaveChanges();
 }
Exemple #6
0
 public Catalogue AddCatalogue(Catalogue catalogue)
 {
     //using (ProductsDbContext context = new ProductsDbContext())
     {
         Catalogue addedCatalogue = _context.Catalogues.Add(catalogue).Entity;
         _context.SaveChanges();
         return(addedCatalogue);
     }
 }
Exemple #7
0
        public void Post([FromBody] Product product)
        {
            //if (!ModelState.IsValid)
            //{

            //}
            context.Products.Add(product);
            context.SaveChanges();
        }
Exemple #8
0
        private Customer AddCustomerToDb(string customerId)
        {
            Customer customer = new Customer {
                Id = customerId, Balance = 100
            };

            db.Customers.Add(customer);
            db.SaveChanges();
            return(customer);
        }
Exemple #9
0
 public IActionResult Post([FromBody] Product product)
 {
     if (!ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     productsDbContext.Products.Add(product);
     productsDbContext.SaveChanges(true);
     return(StatusCode(StatusCodes.Status201Created));
 }
Exemple #10
0
        public IHttpActionResult PostProduct(Product product)
        {
            string sqlInsert = "EXEC InsertProcedure " + product.Id
                               + "," + product.Name
                               + "," + product.Category
                               + "," + product.Price;

            db.Database.ExecuteSqlCommand(sqlInsert);
            db.SaveChanges();
            return(Ok(product));
        }
        public IActionResult PostProduct(Product product)
        {
            if (ModelState.IsValid)
            {
                _context.Products.Add(product);
                _context.SaveChanges();
                return(Ok(product));
            }

            return(BadRequest(ModelState));
        }
Exemple #12
0
        public ActionResult Create([Bind(Include = "Id,Title")] Product product)
        {
            if (ModelState.IsValid)
            {
                db.Products.Add(product);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(product));
        }
Exemple #13
0
        public IActionResult Create(Product product)
        {
            if (!ModelState.IsValid)
            {
                return(View(product));
            }

            _productsDb.Products.Add(product);
            _productsDb.SaveChanges();
            return(RedirectToAction(nameof(Index)));
        }
Exemple #14
0
 public void AddProduct(Product product)
 {
     try
     {
         productDbContext.Add(product);
         productDbContext.SaveChanges(true);
     }
     catch (Exception ex)
     {
         throw new Exception("Cannot update data. " + ex.Message);
     }
 }
Exemple #15
0
        public void Add(Product product)
        {
            Validator.ValidateObject(product, new ValidationContext(product));

            var data = _database.Products.Create();

            data.Name           = product.Name;
            data.IsDiscontinued = product.IsDiscontinued;
            data.UnitPrice      = product.UnitPrice;

            data = _database.Products.Add(data);

            _database.SaveChanges();
        }
        private void SeedData()
        {
            if (!_dbContext.Products.Any())
            {
                _dbContext.Products.Add(new Db.Product()
                {
                    Id = 1, Name = "Keyboard", Price = 10, Inventory = 100
                });
                _dbContext.Products.Add(new Db.Product()
                {
                    Id = 2, Name = "Monitor", Price = 350, Inventory = 150
                });
                _dbContext.Products.Add(new Db.Product()
                {
                    Id = 3, Name = "CPU", Price = 500, Inventory = 140
                });
                _dbContext.Products.Add(new Db.Product()
                {
                    Id = 4, Name = "Mouse", Price = 30, Inventory = 100
                });
                _dbContext.Products.Add(new Db.Product()
                {
                    Id = 5, Name = "SSD", Price = 100, Inventory = 200
                });

                _dbContext.SaveChanges();
            }
        }
Exemple #17
0
        public void Handle(ProductsDbContext context, ProductDto dto)
        {
            var product = context.Products.Find(dto.Id);

            if (product == null)
            {
                return;
            }

            if (dto.Qty > product.Qty)
            {
                _stockShortagePublisher.Publish(new StockShortageEvent
                {
                    OrderId   = dto.OrderId,
                    ProductId = product.Id
                });

                return;
            }

            product.Qty -= dto.Qty;

            context.SaveChanges();

            _stockConfirmedPublisher.Publish(new StockConfirmedEvent
            {
                OrderId   = product.Id,
                ProductId = dto.OrderId
            });
        }
 private void SeedData()
 {
     if (!dbContext.Products.Any())
     {
         dbContext.Products.Add(new Db.Product()
         {
             Id = 1, Name = "KeyBoard", Price = 20, Inventory = 100
         });
         dbContext.Products.Add(new Db.Product()
         {
             Id = 2, Name = "Mouse", Price = 20, Inventory = 200
         });
         dbContext.Products.Add(new Db.Product()
         {
             Id = 3, Name = "Moniter", Price = 200, Inventory = 300
         });
         dbContext.Products.Add(new Db.Product()
         {
             Id = 4, Name = "CPU", Price = 120, Inventory = 400
         });
         dbContext.Products.Add(new Db.Product()
         {
             Id = 5, Name = "HeadSet", Price = 320, Inventory = 500
         });
         dbContext.SaveChanges();
     }
 }
Exemple #19
0
        public static void Seed(this ProductsDbContext dbContext)
        {
            if (dbContext.Items.Any() ||
                dbContext.Products.Any() ||
                dbContext.Stores.Any())
            {
                return;
            }

            dbContext.Stores.AddRange(new List <Store> {
                new Store {
                    Name = "Los Angeles - Pasadena", StoreId = new Guid("8048e9ec-80fe-4bad-bc2a-e4f4a75c834e")
                },
                new Store {
                    Name = "Los Angeles - Beverly Hills", StoreId = new Guid("8d618778-85d7-411e-878b-846a8eef30c0")
                }
            });

            var productsTxt = File.ReadAllText("products.json");
            var products    = JsonConvert.DeserializeObject <List <Product> >(productsTxt);

            dbContext.Products.AddRange(products);

            dbContext.SaveChanges();
        }
 private void SeedData()
 {
     if (!dbContext.Products.Any())
     {
         dbContext.Products.Add(new Product()
         {
             Id = 1, Name = "Keyboard", Price = 20, Inventory = 100
         });
         dbContext.Products.Add(new Product()
         {
             Id = 2, Name = "Mouse", Price = 18, Inventory = 300
         });
         dbContext.Products.Add(new Product()
         {
             Id = 3, Name = "Mother Board", Price = 150, Inventory = 50
         });
         dbContext.Products.Add(new Product()
         {
             Id = 4, Name = "Monitor", Price = 90, Inventory = 300
         });
         dbContext.Products.Add(new Product()
         {
             Id = 5, Name = "Pendrive", Price = 10, Inventory = 600
         });
         dbContext.SaveChanges();
     }
 }
        public IActionResult ImportProducts()
        {
            string         contentRootPath = "wwwroot/json/Product.json";
            var            jsondata        = System.IO.File.ReadAllText(contentRootPath);
            List <Product> products        = JsonConvert.DeserializeObject <List <Product> >(jsondata);

            products.ForEach(p =>
            {
                Product product = new Product()
                {
                    ProductId         = p.ProductId,
                    Name              = p.Name,
                    Description       = p.Description,
                    CategoryProductId = p.CategoryProductId,
                    CategoryProduct   = p.CategoryProduct,
                    Manufacturer      = p.Manufacturer,
                    Supplier          = p.Supplier,
                    Price             = p.Price
                };
                var ourProducts = _context.Products.SingleOrDefault(x => x.ProductId.Equals(p.ProductId));
                if (ourProducts == null)
                {
                    _context.Products.Add(product);
                    _context.SaveChanges();
                    ViewBag.Success = "File uploaded Successfully..Please click on the List Of Products";
                }
            });
            return(View("Index"));
        }
        private void Seed()
        {
            _products = new List <Product>()
            {
                new Product()
                {
                    ProductName = "Mouse", Price = 10.00M
                },
                new Product()
                {
                    ProductName = "Keyboard", Price = 15.00M
                },
                new Product()
                {
                    ProductName = "Gamepad", Price = 25.00M
                },
            };

            using (var context = new ProductsDbContext(_options))
            {
                context.Database.EnsureDeleted();
                context.Products.AddRange(_products);
                context.SaveChanges();
            }
        }
Exemple #23
0
 private void SeedData()
 {
     if (!dbContext.Products.Any())
     {
         dbContext.Products.Add(new Db.Product()
         {
             Id = 1, Name = "Keyboard", Price = 20, Inventory = 100
         });
         dbContext.Products.Add(new Db.Product()
         {
             Id = 2, Name = "Mouse", Price = 15, Inventory = 250
         });
         dbContext.Products.Add(new Db.Product()
         {
             Id = 3, Name = "Monitor", Price = 500, Inventory = 180
         });
         dbContext.Products.Add(new Db.Product()
         {
             Id = 4, Name = "USB", Price = 5, Inventory = 100
         });
         dbContext.Products.Add(new Db.Product()
         {
             Id = 5, Name = "CPU", Price = 4000, Inventory = 30
         });
         dbContext.SaveChanges();
     }
 }
Exemple #24
0
        private void SeedData()
        {
            if (!_dbContext.Products.Any())
            {
                _dbContext.Products.Add(new ProductEntity()
                {
                    Id = Guid.Parse("f02db3f7-d55d-4ef1-bcef-7ea7a5ab7669"), Name = "Teclado", Price = 20, Inventory = 100
                });
                _dbContext.Products.Add(new ProductEntity()
                {
                    Id = Guid.Parse("59f34477-a14a-471a-ae36-e7f5f19ee508"), Name = "Mouse", Price = 15, Inventory = 200
                });
                _dbContext.Products.Add(new ProductEntity()
                {
                    Id = Guid.Parse("48c71845-a01c-40b6-80b5-633a781f6c68"), Name = "Monitor", Price = 35, Inventory = 100
                });
                _dbContext.Products.Add(new ProductEntity()
                {
                    Id = Guid.Parse("d26cab14-934e-498d-b238-df700f866d3e"), Name = "Cooler", Price = 5, Inventory = 100
                });
                _dbContext.Products.Add(new ProductEntity()
                {
                    Id = Guid.Parse("213c62a0-9b1d-491f-a771-320da745d766"), Name = "HD 500gb", Price = 30, Inventory = 200
                });
                _dbContext.Products.Add(new ProductEntity()
                {
                    Id = Guid.Parse("9c1b575b-9844-442e-aa5a-c9fd6f853533"), Name = "Gabinete", Price = 50, Inventory = 100
                });

                _dbContext.SaveChanges();
            }
        }
        private void SeedData()
        {
            if (!dbContext.Products.Any())
            {
                List <Db.Product> products = new List <Db.Product>()
                {
                    new Db.Product()
                    {
                        Id = 1, Name = "Keyboard", Price = 75, Inventory = 25
                    },
                    new Db.Product()
                    {
                        Id = 2, Name = "Mouse", Price = 40, Inventory = 60
                    },
                    new Db.Product()
                    {
                        Id = 3, Name = "Monitor", Price = 150, Inventory = 10
                    },
                    new Db.Product()
                    {
                        Id = 4, Name = "Mousepad", Price = 20, Inventory = 30
                    },
                    new Db.Product()
                    {
                        Id = 5, Name = "Router", Price = 60, Inventory = 15
                    }
                };

                dbContext.Products.AddRange(products);
                dbContext.SaveChanges();

                logger?.LogInformation("Products added to DB");
            }
        }
Exemple #26
0
 private void SeedData()
 {
     if (!dbContext.Products.Any())
     {
         dbContext.Products.Add(new Db.Product()
         {
             Id = 1, Name = "Keyboard", Price = 10, Inventory = 20
         });
         dbContext.Products.Add(new Db.Product()
         {
             Id = 2, Name = "Mouse", Price = 8, Inventory = 30
         });
         dbContext.Products.Add(new Db.Product()
         {
             Id = 3, Name = "CPU", Price = 100, Inventory = 25
         });
         dbContext.Products.Add(new Db.Product()
         {
             Id = 4, Name = "Monitor", Price = 30, Inventory = 25
         });
         dbContext.Products.Add(new Db.Product()
         {
             Id = 5, Name = "Headphone", Price = 10, Inventory = 20
         });
         dbContext.SaveChanges();
     }
 }
        public void Seed()
        {
            if (Seeded)
            {
                return;
            }

            var builder = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile("appsettings.json");

            var configuration = builder.Build();

            var dbContext = new DbContextOptionsBuilder <ProductsDbContext> ()
                            .UseSqlite(configuration.GetConnectionString("DefaultConnection"))
                            .Options;

            using (var context = new ProductsDbContext(dbContext)) {
                //
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();

                // Add a new product
                using (StreamReader reader = new StreamReader("data.json")) {
                    var json     = reader.ReadToEnd();
                    var products = JsonConvert.DeserializeObject <List <Product> > (json);
                    context.Products.AddRange(products);
                    context.SaveChanges();
                    context.Database.CloseConnection();
                }
            }

            Seeded = true;
        }
 private void SeedData()
 {
     if (!dbContext.Products.Any())
     {
         dbContext.Products.Add(new AppDbContext.Product()
         {
             Id = 1, Name = "Keyboard", Price = 20, Inventory = 100
         });
         dbContext.Products.Add(new AppDbContext.Product()
         {
             Id = 2, Name = "Mouse", Price = 10, Inventory = 80
         });
         dbContext.Products.Add(new AppDbContext.Product()
         {
             Id = 3, Name = "Monitor", Price = 100, Inventory = 85
         });
         dbContext.Products.Add(new AppDbContext.Product()
         {
             Id = 4, Name = "CPU", Price = 250, Inventory = 90
         });
         dbContext.Products.Add(new AppDbContext.Product()
         {
             Id = 5, Name = "Processor", Price = 300, Inventory = 95
         });
         dbContext.SaveChanges();
     }
 }
 private void SeedData()
 {
     if (!dbContext.Products.Any())
     {
         dbContext.Products.Add(new Db.Product()
         {
             Id = 1, Name = "Keyboard", Price = 48, Inventory = 100
         });
         dbContext.Products.Add(new Db.Product()
         {
             Id = 2, Name = "Mouse", Price = 35, Inventory = 100
         });
         dbContext.Products.Add(new Db.Product()
         {
             Id = 3, Name = "Monitor", Price = 320, Inventory = 100
         });
         dbContext.Products.Add(new Db.Product()
         {
             Id = 4, Name = "CPU", Price = 1010, Inventory = 100
         });
         dbContext.Products.Add(new Db.Product()
         {
             Id = 5, Name = "GPU", Price = 799, Inventory = 100
         });
         dbContext.Products.Add(new Db.Product()
         {
             Id = 6, Name = "Mouse Pad", Price = 30, Inventory = 100
         });
         dbContext.SaveChanges();
     }
 }
Exemple #30
0
        public static void Initialize(ProductsDbContext context)
        {
            context.Database.EnsureCreated();

            // Look for any flowers.
            if (context.Products.Any())
            {
                return;   // DB has been seeded
            }

            context.Products.AddRange(
                new Product
            {
                Name        = "telefon",
                Description = "White, Yellow",
                Category    = "a",
                Price       = 5
            },
                new Product
            {
                Name        = "telefon1",
                Description = "White, Yellow",
                Category    = "a",
                Price       = 5
            }
                );
            context.SaveChanges();
        }
        public void CanDefineDateTimeAndTimestampWithIdentity()
        {
            ReInitDb();
              if (Version < new Version(5, 6)) return;

              using (var db = new ProductsDbContext())
              {
            //db.Database.CreateIfNotExists();
            db.Database.Initialize(true);
            Product product = new Product
            {
              //Omitting Identity Columns
              DateTimeWithPrecision = DateTime.Now,
              TimeStampWithPrecision = DateTime.Now
            };

            db.Products.Add(product);
            db.SaveChanges();

            var updateProduct = db.Products.First();
            updateProduct.DateTimeWithPrecision = new DateTime(2012, 3, 18, 23, 9, 7, 6);
            db.SaveChanges();

            Assert.AreNotEqual(null, db.Products.First().Timestamp);
            Assert.AreNotEqual(null, db.Products.First().DateCreated);
            Assert.AreEqual(new DateTime(2012, 3, 18, 23, 9, 7, 6), db.Products.First().DateTimeWithPrecision);
            Assert.AreEqual(1, db.Products.Count());

            db.Database.Delete();
              }
        }