Exemple #1
0
        public IActionResult AddGoodsFromXml(IFormFile xmlFile)
        {
            string path = "/files/" + xmlFile.Name;

            using (FileStream fileStream = new FileStream(_hostingEnvironment.WebRootPath + path, FileMode.Create))
            {
                xmlFile.CopyTo(fileStream);
            }
            XmlDocument xmlDocument = new XmlDocument();

            xmlDocument.Load(_hostingEnvironment.WebRootPath + path);

            XmlElement goodsRoot         = xmlDocument.DocumentElement;
            string     goodsName         = null;
            int        goodsPrice        = 0;
            string     goodsCategory     = null;
            string     goodsManufacturer = null;

            foreach (XmlNode node in goodsRoot)
            {
                foreach (XmlNode childNode in node.ChildNodes)
                {
                    if (childNode.Name == "name")
                    {
                        goodsName = childNode.InnerText;
                    }

                    if (childNode.Name == "price")
                    {
                        goodsPrice = Int32.Parse(childNode.InnerText);
                    }

                    if (childNode.Name == "category")
                    {
                        goodsCategory = childNode.InnerText;
                    }

                    if (childNode.Name == "manufacturer")
                    {
                        goodsManufacturer = childNode.InnerText;
                    }
                }
                _databaseContext.Goods.Add(new Goods()
                {
                    Name         = goodsName,
                    Price        = goodsPrice,
                    Category     = _databaseContext.Categories.FirstOrDefault(category => category.Name == goodsCategory),
                    Manufacturer = _databaseContext.Manufacturers.FirstOrDefault(manufacturer => manufacturer.Name == goodsManufacturer)
                });
                _databaseContext.SaveChanges();
            }

            return(RedirectToAction("GetAllGoods"));
        }
Exemple #2
0
        public GoodsController(ChestDatabaseContext context,
                               IHostingEnvironment environment,
                               GoodsCounter goodsCounter,
                               EmailService emailService)
        {
            _databaseContext    = context;
            _hostingEnvironment = environment;
            _goodsCounter       = goodsCounter;
            _emailService       = emailService;

            if (_databaseContext.Categories.Any() == false & _databaseContext.Manufacturers.Any() == false & _databaseContext.Goods.Any() == false)
            {
                Category smartphone = new Category {
                    Name = "Smartphone"
                };
                Category notebook = new Category {
                    Name = "Notebook"
                };
                _databaseContext.Categories.AddRange(smartphone, notebook);

                Manufacturer apple = new Manufacturer {
                    Name = "Apple"
                };
                Manufacturer xiaomi = new Manufacturer {
                    Name = "Xiaomi"
                };

                _databaseContext.Manufacturers.AddRange(apple, xiaomi);

                _databaseContext.Goods.AddRange(
                    new Goods {
                    Name = "Mi A2 Lite", Price = 5000, Category = smartphone, Manufacturer = xiaomi
                },
                    new Goods {
                    Name = "Mi Notebook Pro", Price = 25000, Category = notebook, Manufacturer = xiaomi
                },
                    new Goods {
                    Name = "IPhone X", Price = 20000, Category = smartphone, Manufacturer = apple
                },
                    new Goods {
                    Name = "MacBook Pro", Price = 25000, Category = notebook, Manufacturer = apple
                });
                _databaseContext.SaveChanges();
            }
        }