public IActionResult Index() { if (User.Identity.IsAuthenticated) { List <Category> categories = db .Categories .OrderBy(c => c.FullName) .ToList(); SellItemViewModel sellItemViewModel = new SellItemViewModel(categories); return(View(sellItemViewModel)); } return(Redirect("/Account/Login")); }
public ActionResult SellItem(SellItemViewModel model) { try { HttpCookie conString = Request.Cookies.Get("rwxgqlb"); foreach (var item in model.ListSellItems) { try { InventoryItem i = new InventoryItem(item.Id, Cryptography.Decrypt(conString.Value)); i.ItemSell(model.Id, item.Quantity); } catch (Exception ex) { return(Content(ex.Message)); } } return(View()); } catch (Exception ex) { return(Content(ex.Message)); } }
public async Task <IActionResult> Index(SellItemViewModel sellItemViewModel) { if (ModelState.IsValid) { string userId = User.FindFirst(ClaimTypes.NameIdentifier).Value; ApplicationUser currentUser = await _userManager.FindByIdAsync(userId); Condition condition = (Condition)Enum.Parse(typeof(Condition), sellItemViewModel.Condition); Category category = db.Categories.Single(c => c.ID == sellItemViewModel.CategoryID); List <Brand> existingBrands = db.Brands.Where(b => b.Name == sellItemViewModel.Brand).ToList(); Brand brand = new Brand(); if (existingBrands.Count == 0) { brand = new Brand() { Name = sellItemViewModel.Brand }; db.Brands.Add(brand); db.SaveChanges(); } else { brand = existingBrands[0]; } List <Model> existingModels = db .Models .Where(m => m.Name == sellItemViewModel.Model && m.Brand == brand) .ToList(); Model model = new Model(); if (existingModels.Count == 0) { model = new Model() { Name = sellItemViewModel.Model, Brand = brand }; db.Models.Add(model); db.SaveChanges(); } else { model = existingModels[0]; } Item newItem = new Item() { Owner = currentUser, OwnerId = currentUser.Id, Title = sellItemViewModel.Title, Price = sellItemViewModel.Price, Description = sellItemViewModel.Description, Condition = condition, Category = category, Brand = brand, Model = model, Year = sellItemViewModel.Year }; db.Items.Add(newItem); db.SaveChanges(); UploadImage(sellItemViewModel.Files, newItem); return(Redirect("/Items/ViewItem/" + newItem.ID)); } return(View(sellItemViewModel)); }