コード例 #1
0
        public async Task <IActionResult> AddProduct(IFormFile file, ProductViewModel vm)
        {
            if (ModelState.IsValid)
            {
                var Seller = _context.Sellers.Where(s => s.Email.Contains(User.Identity.Name));
                if (Seller.SingleOrDefault() == null)
                {
                    return(RedirectToAction("RequestForSell", "Products"));
                }


                ImgUploader img     = new ImgUploader(environment);
                var         imgPath = img.ImageUrl(file); //function working here

                var products = new Product
                {
                    Id    = NumberUtilities.GetUniqueNumber(),
                    Title = vm.Product.Title,

                    Description     = vm.Product.Description,
                    Price           = vm.Product.Price,
                    SellerId        = 1, //manually value. Next time it will be from Session User Value
                    CategoryId      = vm.Product.CategoryId,
                    DistrictId      = vm.Product.DistrictId,
                    IsPublished     = vm.Product.IsPublished, //manually valuess
                    Unit            = vm.Product.Unit,
                    ItemInStock     = vm.Product.ItemInStock,
                    UpdatedAt       = DateTime.Now, //manually valuess
                    OfferExpireDate = DateTime.Now, //manually valuess
                    ImagePath       = imgPath,
                    OfferPrice      = vm.Product.OfferPrice,
                };

                await _context.AddAsync(products);

                await _context.SaveChangesAsync();

                long id = products.Id;
            }
            else
            {
                var viewmodel = _productService.GetProductViewModelAsync();

                ViewBag.error = "You Cannot ignore required fields";
                return(View("AddProduct", viewmodel));
            }


            return(RedirectToAction("AddProduct", "Products"));
        }
コード例 #2
0
        public ActionResult SellProduct(int id, IFormFile file, SellerVM vm)
        {
            var data = _context.Sellers.Where(c => c.Email == User.Identity.Name);

            if (id == null || data.SingleOrDefault() == null)
            {
                NotFound();
            }

            if (vm.Product.Title == null ||
                vm.Product.CategoryId == null ||
                vm.Product.DistrictId == null ||
                vm.Product.Price == null ||
                vm.Product.ItemInStock == null ||
                vm.Product.Unit == null)

            {
                ViewBag.entryerr = "Please don't ignore required fields";

                return(View(vm));
            }


            var Seller = _context.Sellers.Where(s => s.Email.Contains(User.Identity.Name));

            if (Seller.SingleOrDefault() == null)
            {
                return(RedirectToAction("RequestForSell", "Products"));
            }


            ImgUploader img = new ImgUploader(_environment);

            var imgPath = img.ImageUrl(file);   //function working here


            var products = new Product
            {
                Title           = vm.Product.Title,
                Description     = vm.Product.Description,
                Price           = vm.Product.Price,
                SellerId        = id,
                CategoryId      = vm.Product.CategoryId,
                DistrictId      = vm.Product.DistrictId,
                IsPublished     = vm.Product.IsPublished, //manually values
                Unit            = vm.Product.Unit,
                ItemInStock     = vm.Product.ItemInStock,
                CreatedAt       = DateTime.Now, //manually valuess
                UpdatedAt       = DateTime.Now, //manually valuess
                OfferExpireDate = DateTime.Now, //manually valuess
                ImagePath       = imgPath,
                OfferPrice      = 1             //need to change
            };

            _context.Products.Add(products);
            _context.SaveChanges();

            var vms = new SellerVM
            {
                Products   = _context.Products.Where(c => c.SellerId == id).ToList(),
                Districts  = _context.Districts.ToList(),
                Categories = _context.Categories.ToList(),
                Sellers    = _context.Sellers.ToList(),
                Error      = "Your Product has been added"
            };

            return(View(vms));
        }