示例#1
0
        public StoreController(IGreenhouse greenhouse, IStore store)
        {
            _greenhouse = greenhouse;
            _store      = store;

            long id1 = _store.AddProduct(new Product {
                Name = "Audi", Price = 3208.7
            });
            long id2 = _store.AddProduct(new Product {
                Name = "Renault", Price = 3605.2
            });
            long id3 = _store.AddProduct(new Product {
                Name = "Saab", Price = 1200.1
            });
        }
        public IActionResult AddProduct(ProductViewModel viewModel)
        {
            string username = User.Identity.Name;

            Store store = _user.GetUserStore(username);

            if (ModelState.IsValid)
            {
                if (viewModel.Img != null)
                {
                    if (Path.GetExtension(viewModel.Img.FileName) != ".jpg")
                    {
                        ModelState.AddModelError("Img", "فایل با پسوند jpg بارگزاری شود");
                    }
                    else
                    {
                        string filePath = "";
                        viewModel.ImgName = CodeGenerators.FileCode() + Path.GetExtension(viewModel.Img.FileName);
                        filePath          = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/images/products/", viewModel.ImgName);

                        using (var stream = new FileStream(filePath, FileMode.Create))
                        {
                            viewModel.Img.CopyTo(stream);
                        }

                        Product product = new Product()
                        {
                            Img        = viewModel.ImgName,
                            BrandId    = viewModel.BrandId,
                            CategoryId = viewModel.CategoryId,
                            Date       = pc.GetYear(DateTime.Now).ToString("0000") + "/" + pc.GetMonth(DateTime.Now).ToString("00") +
                                         "/" + pc.GetDayOfMonth(DateTime.Now).ToString("00"),
                            Time        = pc.GetHour(DateTime.Now).ToString("00") + ":" + pc.GetMinute(DateTime.Now).ToString("00"),
                            DeletePrice = viewModel.DeletePrice,
                            Exist       = viewModel.Exist,
                            Des         = viewModel.Des,
                            Name        = viewModel.Name,
                            NotShow     = viewModel.NotShow,
                            Price       = viewModel.Price,
                            StoreId     = store.UserId
                        };

                        _store.AddProduct(product);

                        return(RedirectToAction(nameof(ShowProducts)));
                    }
                }
            }

            int id = _store.GetStoreCategoriesByStoreId(store.UserId).FirstOrDefault().Id;

            StoreCategory storeCategory = _store.GetStoreCategory(id);

            ViewBag.CategoryId = new SelectList(_store.GetCategories(storeCategory.CategoryId), "Id", "Name", viewModel.CategoryId);
            ViewBag.BrandId    = new SelectList(_store.AllBrands(), "Id", "Name", viewModel.BrandId);

            return(View(viewModel));
        }
示例#3
0
 private void PopulateProducts()
 {
     store.AddProduct(Factory.CreateProduct("A", 50));
     store.AddProduct(Factory.CreateProduct("B", 30));
     store.AddProduct(Factory.CreateProduct("C", 20));
     store.AddProduct(Factory.CreateProduct("D", 15));
     store.AddProduct(Factory.CreateProduct("E", 62));
     store.AddProduct(Factory.CreateProduct("F", 55));
 }
示例#4
0
        public void AddProduct(string name, double standardPrice)
        {
            Product prod = factory.CreateProduct(name, standardPrice);

            store.AddProduct(prod);
        }