Ejemplo n.º 1
0
        public ActionResult Add(ItemsAddViewModel model)
        {
            using (ApplicationDbContext context = new ApplicationDbContext())
            {
                context.Items.Add(new ItemsAddViewModel()
                {
                    Author = model.Author, Title = model.Title, Description = model.Description
                });
                context.SaveChanges();
            }



            return(View());
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> ItemsRequest(ItemsAddViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = await _userManager.GetUserAsync(User);

                if (user == null)
                {
                    throw new ApplicationException(
                              $"Không thể lấy thông tin người dùng '{_userManager.GetUserId(User)}'.");
                }

                var   seller   = _sellerRepository.GetByEmail(user.Email);
                Items newItems = new Items(model.Name, model.Price, model.Description, 0, @"temp",
                                           _categoryRepository.GetByName(model.Category), model.Street, model.ApartmentNumber, model.Postcode,
                                           model.City, _sellerRepository.GetBySellerId(seller.SellerId), Offer.No, false);
                _itemsRepository.Add(newItems);
                _itemsRepository.SaveChanges();

                newItems.Image = @"images\items\" + newItems.ItemsId + @"\";
                _itemsRepository.SaveChanges();

                var filePath = @"wwwroot/images/items/" + newItems.ItemsId + "/thumb.jpg";
                Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                var fileStream = new FileStream(filePath, FileMode.Create);
                await model.Thumbnail.CopyToAsync(fileStream);

                fileStream.Close();

                for (int i = 0; i < model.Image.Count; i++)
                {
                    filePath = @"wwwroot/images/items/" + newItems.ItemsId + "/Image/" + (i + 1) + ".jpg";
                    Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                    fileStream = new FileStream(filePath, FileMode.Create);
                    await model.Image[i].CopyToAsync(fileStream);
                    fileStream.Close();
                }

                ViewData["AllCategories"] = _categoryRepository.GetAll().ToList();
                ViewData["category"]      = new SelectList(_categoryRepository.GetAll().Select(c => c.Name));
                return(RedirectToAction(nameof(HomeController.Index), "Home"));
            }

            ViewData["AllCategories"] = _categoryRepository.GetAll().ToList();
            ViewData["category"]      = new SelectList(_categoryRepository.GetAll().Select(c => c.Name));
            return(View(nameof(ItemsRequest), model));
        }