public IActionResult New(ProductNewViewModel viewModel)
        {
            if (viewModel.Price < 0)
            {
                ModelState.AddModelError("Price", "Priset får inte vara negativt");
            }
            else if (viewModel.Price > 999999)
            {
                ModelState.AddModelError("Price", "Priset får max vara 999999 kr ");
            }

            if (ModelState.IsValid)
            {
                var fileName = _defaultImgTitle;

                if (viewModel.Image != null)
                {
                    var uploadsFolder = Path.Combine(_hostingEnvironment.WebRootPath + "\\img");
                    fileName = Guid.NewGuid().ToString() + "_" + viewModel.Image.FileName;
                    var filePath = Path.Combine(uploadsFolder, fileName);
                    viewModel.Image.CopyTo(new FileStream(filePath, FileMode.Create));
                }

                var newProduct = CreateProduct(viewModel, fileName);
                _productRepository.Add(newProduct);

                return(RedirectToAction("Details", new { prodId = newProduct.Id }));
            }

            viewModel.Companies     = GetCompaniesListItems();
            viewModel.SubCategories = GetSubCategoriesListItems();
            viewModel.Colors        = GetColorsListItems();

            return(View(viewModel));
        }
예제 #2
0
        public IActionResult ProductNew()
        {
            if (IsLogedIn() == true)
            {
                // Tạo slug mới
                var new_slug = new Slug();
                new_slug.Url        = DateTime.Now.ToString();
                new_slug.DateCreate = DateTime.Now;
                new_slug.DateModify = DateTime.Now;
                _context.Slugs.Add(new_slug);
                _context.SaveChanges();

                ProductNewViewModel viewmodel = new ProductNewViewModel()
                {
                    Product = new Product()
                    {
                        Slug_Id  = _context.Slugs.LastOrDefault().Id,
                        Admin_Id = (int)HttpContext.Session.GetInt32("Admin_Id")
                    },
                    ProductBrands = _context.ProductBrands.ToList(),
                    ProductTypes  = _context.ProductTypes.ToList()
                };
                return(View(viewmodel));
            }
            else
            {
                return(RedirectToAction("Login"));
            }
        }
예제 #3
0
        public IActionResult ProductNew(ProductNewViewModel viewmodel)
        {
            if (IsLogedIn() == true)
            {
                Product new_product = viewmodel.Product;
                new_product.Thumbnail = "#";
                // Tải hình ảnh sản phẩm lên thư mục wwwroot/uploads
                if (viewmodel.Thumbnail != null)
                {
                    new_product.Thumbnail = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
                    string uniqueName = new_product.Thumbnail;                                               // Tạo tên hình ảnh theo chuỗi ngày tháng lúc đăng ảnh
                    string newpath    = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "uploads"); // Trỏ đường dẫn đến thư mục wwwroot/uploads
                    newpath = Path.Combine(newpath, uniqueName);                                             // Trỏ đường dẫn đến tên hình ảnh
                    newpath = newpath + Path.GetExtension(viewmodel.Thumbnail.FileName);                     // Gắn đuôi (loại file) cho hình
                    viewmodel.Thumbnail.CopyTo(new FileStream(newpath, FileMode.Create));                    // Copy hình từ nguồn sang wwwroot/uploads
                    new_product.Thumbnail += Path.GetExtension(viewmodel.Thumbnail.FileName);
                }

                // Đổi slug theo tên sản phẩm
                Slug   edit_slug = _context.Slugs.Where(s => s.Id == new_product.Slug_Id).FirstOrDefault();
                string new_slug  = StaticClass.ToUrlFriendly(new_product.Name);
                if (IsSlugExists(new_slug) == false)
                {
                    edit_slug.Url = new_slug;
                }

                _context.Products.Add(new_product);
                _context.SaveChanges();
                return(RedirectToAction("ProductsOverview", "Admin"));
            }
            else
            {
                return(RedirectToAction("Login"));
            }
        }
예제 #4
0
        public async Task <IActionResult> Browse(string name, int?pageIndex)
        {
            Guid userGuid;

            Guid.TryParse(this.User.FindFirstValue(ClaimTypes.NameIdentifier), out userGuid);

            var products = await _productService.BrowseAsync1(name, pageIndex);

            var viewModels = products.Select(p =>
                                             new ProductViewModel
            {
                Id          = p.Id,
                UserId      = p.UserId,
                Name        = p.Name,
                Category    = p.Category,
                Price       = p.Price,
                Description = p.Description,
                Files       = p.Files
            });

            if (userGuid != Guid.Empty && name != "all")
            {
                viewModels = viewModels.Where(c => c.UserId == userGuid);
            }

            ProductNewViewModel newModel = new ProductNewViewModel();

            newModel.Products        = viewModels.ToList();
            newModel.HasNextPage     = products.HasNextPage;
            newModel.HasPreviousPage = products.HasPreviousPage;
            newModel.PageIndex       = products.PageIndex;
            newModel.TotalPages      = products.TotalPages;

            return(View(newModel));
        }
예제 #5
0
        public IActionResult New(ProductNewViewModel viewModel)
        {
            string uniqueFileName = UploadedFileNew(viewModel);

            var productExists = _dbContext.Products.Any(p => p.Name.ToLower() == viewModel.Name.ToLower());

            if (productExists)
            {
                ModelState.AddModelError("Name", "Produkt finns redan");
            }

            if (ModelState.IsValid)
            {
                var dbProduct = new Product();

                dbProduct.Name        = viewModel.Name;
                dbProduct.Category    = _dbContext.Categories.First(r => r.Id == viewModel.SelectedCategoryId);
                dbProduct.Description = viewModel.Description;
                dbProduct.Price       = viewModel.Price;
                dbProduct.Image       = uniqueFileName;

                _dbContext.Products.Add(dbProduct);

                _dbContext.SaveChanges();

                return(RedirectToAction("Index"));
            }

            viewModel.AllCategories = GetCategoriesListItems();
            return(View(viewModel));
        }
예제 #6
0
        public IActionResult ProductNew()
        {
            var viewModel = new ProductNewViewModel {
                AllCategories = GetCategoriesListItems()
            };

            return(View(viewModel));
        }
예제 #7
0
        public IActionResult New()
        {
            var viewModel = new ProductNewViewModel();

            viewModel.AllCategory = GetCategorySelectListItems();

            return(View(viewModel));
        }
예제 #8
0
        public ActionResult Create()
        {
            ProductNewViewModel model = new ProductNewViewModel();

            model.AvailableCategories = CategoriesService.Instance.GetCategory();

            return(PartialView(model));
        }
예제 #9
0
        public IActionResult New()
        {
            var viewModel = new ProductNewViewModel();

            viewModel.Categories = GetAllCategories();

            return(View(viewModel));
        }
        public IActionResult New()
        {
            var viewModel = new ProductNewViewModel();

            viewModel.Companies     = GetCompaniesListItems();
            viewModel.SubCategories = GetSubCategoriesListItems();
            viewModel.Colors        = GetColorsListItems();

            return(View(viewModel));
        }
예제 #11
0
        public ActionResult Create(ProductNewViewModel model)
        {
            var newPorduct = new Product();

            newPorduct.Name        = model.Name;
            newPorduct.Description = model.Description;
            newPorduct.Price       = model.Price;
            newPorduct.categoryID  = model.CategoryID;
            newPorduct.ImageURL    = model.ImageURL;
            productService.Instance.SaveProduct(newPorduct);
            return(RedirectToAction("ProductTable"));
        }
 private Product CreateProduct(ProductNewViewModel viewModel, string fileName)
 {
     return(new Product
     {
         Title = viewModel.Title,
         Color = _colorRepository.GetById(viewModel.SelectedColorId),
         Company = _companyRepository.GetById(viewModel.SelectedCompanyId),
         Price = viewModel.Price,
         InStock = viewModel.InStock,
         ShortDescription = viewModel.ShortDescription,
         LongDescription = viewModel.LongDescription,
         SubCategory = _subCategoryRepository.GetById(viewModel.SelectedSubCategoryId),
         Warranty = viewModel.WarrantyYears,
         imgTitle = fileName
     });
 }
예제 #13
0
        private string UploadedFileNew(ProductNewViewModel viewModel)
        {
            string uniqueFileName = null;

            if (viewModel.Image != null)
            {
                string uploadsFolder = Path.Combine(_webHostEnvironment.WebRootPath, "img");
                uniqueFileName = Guid.NewGuid().ToString() + "_" + viewModel.Image.FileName;
                string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    viewModel.Image.CopyTo(fileStream);
                }
            }
            return(uniqueFileName);
        }
예제 #14
0
 public IActionResult ProductNew(ProductNewViewModel viewModel)
 {
     if (ModelState.IsValid)
     {
         var dbProduct = new Product();
         _dbContext.Products.Add(dbProduct);
         dbProduct.Category    = _dbContext.ProductCategories.First(r => r.Id == viewModel.SelectedCategoryId);
         dbProduct.Name        = viewModel.Name;
         dbProduct.Price       = viewModel.Price;
         dbProduct.Image       = viewModel.Image;
         dbProduct.Description = viewModel.Description;
         _dbContext.SaveChanges();
         return(RedirectToAction("Index", "Home"));
     }
     viewModel.AllCategories = GetAllCategoriesAsListItems();
     return(View(viewModel));
 }
예제 #15
0
        public IActionResult New(ProductNewViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                var dbProduct = new Product();
                dbProduct.ProductCategory = _dbContext.ProductCategories.First(r => r.Id == viewModel.SelectedCategorieId);
                dbProduct.Name            = viewModel.Namn;
                dbProduct.Price           = viewModel.Price;
                dbProduct.Description     = viewModel.Comment;
                _dbContext.Produkter.Add(dbProduct);
                _dbContext.SaveChanges();
                return(RedirectToAction("Index"));
            }

            viewModel.AllCategory = GetCategorySelectListItems();
            return(View(viewModel));
        }
예제 #16
0
        public IActionResult New(ProductNewViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                var dbProduct = new Product();
                _dbContext.Products.Add(dbProduct);
                dbProduct.Id          = viewModel.Id;
                dbProduct.Title       = viewModel.Title;
                dbProduct.Description = viewModel.Description;
                dbProduct.Price       = viewModel.Price;
                dbProduct.Category    = _dbContext.Categories.First(r => r.Id == viewModel.Category);
                _dbContext.SaveChanges();

                return(RedirectToAction("Index"));
            }

            viewModel.Categories = GetAllCategories();
            return(View(viewModel));
        }
예제 #17
0
        public IActionResult ProductNew(ProductNewViewModel viewModel)
        {
            if (DbContext.Products.Any(c => c.ProductName == viewModel.Name))
            {
                ModelState.AddModelError("Name", "Product already exist");
            }
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Index", "Shop"));
            }

            var dbProd = new Product();

            DbContext.Products.Add(dbProd);
            dbProd.Category = DbContext.Categories
                              .First(c => c.Id == viewModel.SelectCategoryId);
            dbProd.ProductName = viewModel.Name;
            dbProd.Description = viewModel.Description;
            dbProd.Price       = viewModel.Price;
            DbContext.SaveChanges();
            return(View(viewModel));
        }