public IActionResult VoteUp(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 = 1 }; if (thisHero.rating == null) { thisHero.rating = (decimal)100.00; } else { int numberOfVotes = _context.Votes.Where(v => v.new_hero_id == id).Count(); thisHero.rating = (thisHero.rating * numberOfVotes + 100) / (numberOfVotes + 1); } _context.Update(thisHero); _context.Add(newVote); } else { existingVote.value = 1; 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 RegisterSubmit(UserValidator model) { if (!ModelState.IsValid) { return(View("Register")); } if (_context.Users.SingleOrDefault(u => u.username == model.username) != null) { ModelState.AddModelError("username", "This username is already taken."); return(View("Register")); } PasswordHasher <User> hasher = new PasswordHasher <User>(); User newUser = new User() { username = model.username }; newUser.password = hasher.HashPassword(newUser, model.password); _context.Users.Add(newUser); _context.SaveChanges(); HttpContext.Session.SetString("username", _context.Users.Single(u => u.username == newUser.username).username); return(RedirectToAction("Index", "Home")); }
public IActionResult CreateHero(Hero newHero) { Hero heroToAdd = new Hero() { name = newHero.name, attribute = newHero.attribute, intelligence = newHero.intelligence, agility = newHero.agility, strength = newHero.strength, attack = newHero.attack, speed = newHero.speed, armor = newHero.armor, bio = newHero.bio, attack_range = newHero.attack_range, attack_type = newHero.attack_type, img = newHero.img }; _context.Add(heroToAdd); _context.SaveChanges(); return(RedirectToAction("AddHero")); }
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")); }
public void Persist(DotaEntity dotaEntity) { string errMessage; if (dotaEntity != null) { try { GameEntity gameEntity = dotaEntity.Game; this.Add <GameEntity>(gameEntity); } catch (Exception e) { errMessage = "Could not persist GameEntity"; Console.WriteLine(errMessage); throw new Exception(e.Message, e.InnerException); } try { List <HeroEntity> heroes = dotaEntity.Heroes; heroes.ForEach((HeroEntity HeroEntity) => { Add <HeroEntity>(HeroEntity); }); } catch (Exception e) { errMessage = "Could not persist Hero:"; Console.WriteLine(errMessage); throw new Exception(e.Message, e.InnerException); } try { List <ItemEntity> items = dotaEntity.Items; items.ForEach((ItemEntity itemEntity) => { Add <ItemEntity>(itemEntity); }); } catch (Exception e) { errMessage = "Could not persist Items:"; Console.WriteLine(errMessage); throw new Exception(e.Message, e.InnerException); } try { List <BuildingEntity> items = dotaEntity.Buildings; items.ForEach((BuildingEntity buildingEntity) => { Add <BuildingEntity>(buildingEntity); }); } catch (Exception e) { errMessage = "Could not persist Building:"; Console.WriteLine(errMessage); throw new Exception(e.Message, e.InnerException); } try{ _dbContext.SaveChanges(); } catch (Exception e) { errMessage = "Could not persist The Objects"; Console.WriteLine(errMessage); throw new Exception(e.Message, e.InnerException); } } }