示例#1
0
        public IActionResult SubmitHeroChanges(Hero model)
        {
            Hero thisHero = _context.Heroes.SingleOrDefault(h => h.id == model.id);

            thisHero.name         = model.name;
            thisHero.attribute    = model.attribute;
            thisHero.intelligence = model.intelligence;
            thisHero.agility      = model.agility;
            thisHero.strength     = model.strength;
            thisHero.attack       = model.attack;
            thisHero.speed        = model.speed;
            thisHero.armor        = model.armor;
            thisHero.bio          = model.bio;
            thisHero.attack_type  = model.attack_type;
            thisHero.attack_range = model.attack_range;
            thisHero.img          = model.img;
            _context.Update(thisHero);
            _context.SaveChanges();
            return(RedirectToAction("UpdateHero", new { id = model.id }));
        }
        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 }));
        }