public IActionResult DeleteNew(int id, string username, string password) { PasswordHasher <Admin> hasher = new PasswordHasher <Admin>(); Admin thisAdmin = _context.Admins.SingleOrDefault(a => a.username == username); if (thisAdmin == null) { return(RedirectToAction("DeleteNewHeroList")); } if (hasher.VerifyHashedPassword(thisAdmin, thisAdmin.password, password) == 0) { return(RedirectToAction("DeleteNewHeroList")); } New_Hero heroToDelete = _context.New_Heroes.Single(n => n.id == id); List <Vote> votesToDelete = _context.Votes.Where(v => v.new_hero_id == heroToDelete.id).ToList(); if (votesToDelete.Count > 0) { foreach (Vote v in votesToDelete) { _context.Remove(v); } } _context.SaveChanges(); _context.Remove(heroToDelete); _context.SaveChanges(); return(RedirectToAction("Main")); }
public IActionResult VoteDown(int id, string user) { User thisUser = _context.Users.Single(u => u.username == user); Vote existingVote = _context.Votes.SingleOrDefault(v => v.new_hero_id == id && v.user_id == thisUser.id); New_Hero thisHero = _context.New_Heroes.SingleOrDefault(n => n.id == id); if (existingVote == null) { Vote newVote = new Vote() { new_hero_id = id, user_id = thisUser.id, value = 0 }; if (thisHero.rating == null) { thisHero.rating = (decimal)0.00; } else { int numberOfVotes = _context.Votes.Where(v => v.new_hero_id == id).Count(); thisHero.rating = (thisHero.rating * numberOfVotes) / (numberOfVotes + 1); } _context.Update(thisHero); _context.Add(newVote); } else { existingVote.value = 0; int numberOfVotes = _context.Votes.Where(v => v.new_hero_id == id).Count(); thisHero.rating = (thisHero.rating * numberOfVotes - 100) / numberOfVotes; _context.Update(thisHero); _context.Update(existingVote); } _context.SaveChanges(); return(RedirectToAction("HeroPage", new { id = id })); }
public IActionResult HeroPage(int id) //display page for custom hero { New_Hero heroToDisplay = _context.New_Heroes.SingleOrDefault(h => h.id == id); if (heroToDisplay == null) { return(View("HeroNotFound")); } Hero baseHero = _context.Heroes.SingleOrDefault(h => h.id == heroToDisplay.hero_id); Spell spell1 = _context.Spells.SingleOrDefault(s => s.id == heroToDisplay.spell_1_id); Spell spell2 = _context.Spells.SingleOrDefault(s => s.id == heroToDisplay.spell_2_id); Spell spell3 = _context.Spells.SingleOrDefault(s => s.id == heroToDisplay.spell_3_id); Spell spell4 = _context.Spells.SingleOrDefault(s => s.id == heroToDisplay.spell_4_id); ViewBag.base_id = heroToDisplay.hero_id; if (heroToDisplay.img == null) //uses base hero portrait if creator did not upload their own { ViewBag.img = baseHero.img; ViewBag.user_image = false; } else { ViewBag.user_image = true; ViewBag.img = heroToDisplay.img; } if (heroToDisplay.user_id != null) { User creator = _context.Users.SingleOrDefault(u => u.id == heroToDisplay.user_id); ViewBag.username = creator.username; ViewBag.user_id = creator.id; } ViewBag.loggedUser = null; User logged = _context.Users.SingleOrDefault(u => u.username == HttpContext.Session.GetString("username")); if (logged != null) { ViewBag.loggedUser = logged.username; } decimal rating = 0; List <Vote> votes = _context.Votes.Where(v => v.new_hero_id == id).ToList(); //get hero rating ViewBag.existingVote = null; if (logged != null) { Vote existingVote = votes.SingleOrDefault(v => v.user_id == logged.id); if (existingVote != null) { if (existingVote.value == 1) { ViewBag.existingVote = "up"; } else { ViewBag.existingVote = "down"; } } } if (votes.Count == 0) { ViewBag.rating = null; } else { foreach (Vote v in votes) { rating += v.value; } ViewBag.rating = heroToDisplay.rating; ViewBag.voteCount = votes.Count; } /* may want to add this again later - should add "previous/next hero" links to each hero page, but links to a dead page if a hero is deleted * if(heroToDisplay == _context.New_Heroes.Last()) ViewBag.position = "last"; * else if(heroToDisplay == _context.New_Heroes.First()) ViewBag.position = "first"; * else ViewBag.position = "middle"; */ return(View(Converter.ConvertHero(heroToDisplay, baseHero, spell1, spell2, spell3, spell4))); //converts to version formatted for display }
public IActionResult Submit(New_Hero_Creator model) { List <DisplaySpell> allSpells = new List <DisplaySpell>(); foreach (Spell s in _context.Spells) { allSpells.Add(Converter.Convert(s)); } ViewBag.heroes = _context.Heroes.ToList(); ViewBag.regulars = allSpells.Where(s => s.ultimate == false).ToList(); ViewBag.ultimates = allSpells.Where(s => s.ultimate == true).ToList(); if (model.hero_id == 0) { ModelState.AddModelError("hero_id", "You must select a base for your hero."); } if (model.spell_1_id == 0 || model.spell_2_id == 0 || model.spell_3_id == 0) { ModelState.AddModelError("spell_1_id", "You must select three regular spells."); } else if (model.spell_1_id == model.spell_2_id || model.spell_1_id == model.spell_3_id || model.spell_2_id == model.spell_3_id) { ModelState.AddModelError("spell_1_id", "Cannot use the same spell twice."); } if (model.spell_4_id == 0) { ModelState.AddModelError("spell_4_id", "Your hero needs an ultimate."); } if (_context.New_Heroes.SingleOrDefault(n => n.name == model.name) != null) { ModelState.AddModelError("name", "This name is already in use."); } if (ModelState.IsValid) { New_Hero hero = new New_Hero() { name = model.name, hero_id = model.hero_id, spell_1_id = model.spell_1_id, spell_2_id = model.spell_2_id, spell_3_id = model.spell_3_id, spell_4_id = model.spell_4_id }; if (HttpContext.Session.GetString("username") != null) { hero.user_id = _context.Users.SingleOrDefault(u => u.username == HttpContext.Session.GetString("username")).id; } if (model.bio != null) { hero.bio = model.bio; } if (model.file != null) { TransferUtility transfer = new TransferUtility(Credentials.AccessKey, Credentials.SecretKey, Amazon.RegionEndpoint.USWest2); using (var stream = new MemoryStream()) { string key = null; while (_context.New_Heroes.FirstOrDefault(n => n.img == key) != null) { key = GenerateKey(); } model.file.CopyTo(stream); transfer.Upload(stream, "dhcimages", key); hero.img = key; } } _context.Add(hero); _context.SaveChanges(); return(RedirectToAction("HeroPage", "Home", new { id = _context.New_Heroes.SingleOrDefault(n => n.name == hero.name).id })); } ViewBag.bio = model.bio; return(View("Create")); }