Example #1
0
        public async Task <PagedList <HeroDto> > GetHeroesToManagement(PageParams pageParams, HeroesManagementFilter filter)
        {
            List <HeroDto> lstHeroesListed = new List <HeroDto>();
            List <Heroes>  lstHeroes       = new List <Heroes>();

            lstHeroes = await _context.Heroes.ToListAsync();

            if (filter.HeroNameLike != null)
            {
                lstHeroes = lstHeroes.Where(x => x.HeroName.ToLower().Contains(filter.HeroNameLike.ToLower())).ToList();
            }


            foreach (Heroes heroes in lstHeroes)
            {
                var gameName = await _context.Games.FirstOrDefaultAsync(x => x.Id == heroes.GameId);


                HeroDto heroDto = new HeroDto()
                {
                    Id       = heroes.Id,
                    HeroName = heroes.HeroName,
                    GameName = gameName.GameName
                };
                lstHeroesListed.Add(heroDto);
            }

            return(await PagedList <HeroDto> .Create(lstHeroesListed, pageParams.PageNumber, pageParams.PageSize));
        }
Example #2
0
 public static void CheckIfHeroExist(HeroDto hero)
 {
     if (Field.Hero == null)
     {
         Console.ForegroundColor = ConsoleColor.Red;
         throw new InputException("You must add hero first.");
     }
 }
        public PowerDto FindPowerOfHero(HeroDto hero, string powerName)
        {
            if (hero.Powers != null)
            {
                return(hero.Powers.FirstOrDefault(power => power.Name == powerName));
            }

            return(null);
        }
Example #4
0
        public async Task <IActionResult> Hero([FromBody] HeroDto heroDto)
        {
            if (string.IsNullOrEmpty(heroDto.Name) || string.IsNullOrEmpty(heroDto.Power))
            {
                return(BadRequest("Invalid hero"));
            }

            return(Ok(await heroService.InsertHero(new Hero(heroDto.Name, heroDto.Power))));
        }
 private User ConvertHeroToUser(HeroDto hero)
 {
     return(new User()
     {
         Email = hero.Email,
         FullName = hero.Name,
         Username = hero.Email,
         Age = 18,
     });
 }
        [HttpPost] // POST: heroes
        public async Task <IActionResult> Post([FromBody] HeroDto newHero)
        {
            var query = new CreateHeroCommand()
            {
                NewHero = newHero
            };
            var result = await _mediator.Send(query);

            return(Accepted(result));
        }
Example #7
0
        public async Task <IActionResult> Hero([FromBody] HeroDto heroDto)
        {
            if (string.IsNullOrEmpty(heroDto.Name) || string.IsNullOrEmpty(heroDto.Power))
            {
                return(BadRequest("Invalid Hero"));
            }

            var hero = _mapper.Map <Hero>(heroDto);

            return(Ok(await _heroService.InsertHero(hero)));
        }
        public async Task <ActionResult <Hero> > AddHero(HeroDto heroDto)
        {
            var hero = new Hero
            {
                HeroName = heroDto.HeroName.ToLower(),
            };

            _context.Heroes.Add(hero);
            await _context.SaveChangesAsync();

            return(hero);
        }
Example #9
0
        public async Task <IActionResult> UpdateHero([FromBody] HeroDto heroDto)
        {
            if (string.IsNullOrEmpty(heroDto.HeroId.ToString()))
            {
                return(BadRequest("Hero Id is invalid."));
            }

            if (string.IsNullOrEmpty(heroDto.Name) || string.IsNullOrEmpty(heroDto.Power))
            {
                return(BadRequest("Hero Name or Hero Power are invalid."));
            }

            return(Ok(await _heroService.UpdateHero(new Hero(Guid.Parse(heroDto.HeroId), heroDto.Name, heroDto.Power))));
        }
        private async Task <ICollection <HeroAlly> > CreateHeroAllies(HeroDto heroDto, Hero hero)
        {
            var heroAllies = new List <HeroAlly>();

            foreach (var ally in heroDto.Allies)
            {
                heroAllies.Add(new HeroAlly
                {
                    AllyFrom = hero,
                    AllyTo   = await FindById(ally.Id)
                });
            }

            return(heroAllies);
        }
        [HttpPut("{id}")] // PUT: heroes/123
        public async Task <IActionResult> Put([FromRoute] string id, [FromBody] HeroDto updateHero)
        {
            if (id != updateHero.Id)
            {
                return(BadRequest());
            }

            var query = new UpdateHeroCommand()
            {
                HeroDto = updateHero
            };
            await _mediator.Send(query);

            return(NoContent());
        }
        public async Task ChangeEntityDetails(HeroDto entity)
        {
            var entityForUpdate = await _context.Heroes
                                  .FirstOrDefaultAsync(h => h.Id == entity.Id);

            if (entityForUpdate == null)
            {
                throw new EntityNotFoundException($"There is no entity with id: {entity.Id}");
            }

            entityForUpdate.Name     = entity.Name;
            entityForUpdate.Birthday = DateTime.Parse(entity.Birthday);

            _context.Heroes.Update(entityForUpdate);
            await _context.SaveChangesAsync();
        }
 public ActionResult <Hero> Register([FromBody] HeroDto heroDto)
 {
     try
     {
         Hero hero = _service.Create(new Hero()
         {
             Name = heroDto.Name
         });
         HeroDto responseHeroDto = new HeroDto(hero);
         return(Ok($"{heroDto.Name} is created as a hero with a weapon {hero.Weapon}"));
     }
     catch (Exception ex)
     {
         return(BadRequest(ex.Message));
     }
 }
        private async Task <ICollection <HeroPower> > CreateHeroPowers(HeroDto heroDto, Hero hero)
        {
            var heroPowers = new List <HeroPower>();

            foreach (var power in heroDto.Powers)
            {
                heroPowers.Add(new HeroPower
                {
                    Hero     = hero,
                    Power    = await _powerService.FindById(power.Id),
                    Strength = (await _powerService.FindById(power.Id)).Strength
                });
            }

            return(heroPowers);
        }
        public async Task UpdateHero(HeroDto heroDto)
        {
            await CheckHeroFields(heroDto);

            var heroForUpdate = await _context.Heroes
                                .FirstOrDefaultAsync(h => h.Id == heroDto.Id);

            if (heroForUpdate == null)
            {
                throw new EntityNotFoundException($"There is no hero with id: {heroDto.Id}");
            }

            await DeleteAllHeroPowers(heroForUpdate);
            await DeleteAllHeroAllies(heroForUpdate);

            heroForUpdate.Powers = null;

            heroForUpdate.Name   = heroDto.Name;
            heroForUpdate.TypeId = heroDto.TypeId;
            heroForUpdate.Powers = await CreateHeroPowers(heroDto, heroForUpdate);

            heroForUpdate.Allies = await CreateHeroAllies(heroDto, heroForUpdate);

            heroForUpdate.OverallStrength = heroForUpdate.Powers.Aggregate(0, (acc, power) => acc += power.Power.Strength);

            Power mainPower = await _context.Powers.FirstOrDefaultAsync(power => power.Id == heroDto.MainPower.Id);

            heroForUpdate.MainPower = mainPower != null ? new HeroPower
            {
                Hero     = heroForUpdate,
                Power    = mainPower,
                Strength = mainPower.Strength
            } : null;
            heroForUpdate.OverallStrength += heroForUpdate.MainPower == null ? 0 : heroForUpdate.MainPower.Power.Strength;


            heroForUpdate.Latitude  = heroDto.Latitude;
            heroForUpdate.Longitude = heroDto.Longitude;


            _context.Heroes.Update(heroForUpdate);
            await _context.SaveChangesAsync();
        }
        public IHttpActionResult UpdateHero(int id, HeroDto heroDto)
        {
            var heroToUpdate = _context.Heroes.SingleOrDefault(h => h.Id == id);

            if (heroToUpdate == null)
            {
                return(NotFound());
            }

            // Ignore ImageURI Property
            Mapper.CreateMap <HeroDto, Hero>()
            .ForMember(src => src.ImageURI, opt => opt.Ignore());

            Mapper.Map(heroDto, heroToUpdate);

            _context.SaveChanges();

            return(Ok());
        }
        public async Task CreateHero(HeroDto heroDto)
        {
            await CheckHeroFields(heroDto);

            Hero newHero = new Hero
            {
                Name = heroDto.Name,
            };

            newHero.TypeId = heroDto.TypeId;
            newHero.Powers = await CreateHeroPowers(heroDto, newHero);

            newHero.Allies = await CreateHeroAllies(heroDto, newHero);

            newHero.AvatarPath      = Constants.DEFAULT_IMAGE_HERO;
            newHero.OverallStrength = newHero.Powers.Aggregate(0, (acc, power) => acc += power.Power.Strength);
            Power mainPower = await _context.Powers.FirstOrDefaultAsync(power => power.Id == heroDto.MainPower.Id);

            newHero.MainPower = mainPower == null ? null : new HeroPower
            {
                Hero     = newHero,
                Power    = mainPower,
                Strength = mainPower.Strength
            };
            newHero.OverallStrength += newHero.MainPower == null ? 0 : newHero.MainPower.Power.Strength;

            newHero.Latitude  = heroDto.Latitude;
            newHero.Longitude = heroDto.Longitude;

            if (!string.IsNullOrEmpty(heroDto.Email))
            {
                var user = ConvertHeroToUser(heroDto);

                await _userService.CreateHeroUser(user);

                newHero.User = user;
            }

            await _context.AddAsync(newHero);

            await _context.SaveChangesAsync();
        }
        private async Task CheckHeroFields(HeroDto heroDto)
        {
            await _heroTypeService.FindById(heroDto.TypeId);

            if (string.IsNullOrEmpty(heroDto.Name) || string.IsNullOrEmpty(heroDto.TypeId.ToString()))
            {
                throw new InvalidParameterException();
            }

            try
            {
                var heroFromDb = await FindByName(heroDto.Name);

                if (heroFromDb != null && heroFromDb.Id != heroDto.Id)
                {
                    throw new DuplicateException($"There is a hero with the same name: {heroDto.Name}");
                }
            }
            catch { }
        }
Example #19
0
        public async Task <IEnumerable <HeroDto> > GetAllHeroes()
        {
            var lstHeroes = await _context.Heroes.ToListAsync();

            var lstItemsFromApi = new List <HeroDto>();

            foreach (Heroes hero in lstHeroes)
            {
                var heroToCreate = new HeroDto();
                heroToCreate.Id       = hero.Id;
                heroToCreate.HeroName = hero.HeroName;

                var gameName = await _context.Games.FirstOrDefaultAsync(x => x.Id == hero.GameId);

                heroToCreate.GameName = gameName.GameName;

                lstItemsFromApi.Add(heroToCreate);
            }

            return(lstItemsFromApi);
        }
        public HeroDto AddBonusStrength(HeroDto hero)
        {
            if (hero.Powers != null)
            {
                int tenPercentIncrease  = hero.OverallStrength + Convert.ToInt32(_appSettings.TEN_PERCENT_BONUS * Convert.ToDouble(hero.OverallStrength));
                int fivePercentIncrease = hero.OverallStrength + Convert.ToInt32(_appSettings.FIVE_PERCENT_BONUS * Convert.ToDouble(hero.OverallStrength));

                foreach (var power in hero.Powers)
                {
                    if (power.Name == Constants.STRENGTH_POWER)
                    {
                        hero.OverallStrength = tenPercentIncrease;
                    }
                    else if (power.Name == Constants.SPEED_POWER || power.Name == Constants.INVISIBILITY_POWER)
                    {
                        hero.OverallStrength = fivePercentIncrease;
                    }
                }
            }
            return(hero);
        }
        public async Task <IActionResult> Authenticate([FromBody] HeroDto heroDto)

        {
            Hero hero;

            try
            {
                hero = await Task.Run(() => _service.Authenticate(heroDto.Name));
            }
            catch (HeroServiceException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }

            HeroDto signInUser = new HeroDto(hero,
                                             _service.GetHeroToken(hero, _appSettings.Secret));

            return(Ok(signInUser));
        }
Example #22
0
        public HeroDto GetById(int id)
        {
            var hero = this.context.Heroes
                       .Include(h => h.Roles)
                       .Include(h => h.Abilities)
                       .FirstOrDefault(h => h.Id == id);

            if (hero == null)
            {
                throw new DotaException(Constants.InvalidOperation);
            }

            var heroDto = new HeroDto
            {
                Id               = hero.Id,
                AgilityGain      = hero.AgilityGain,
                AttackRange      = hero.AttackRange,
                AttackRate       = hero.AttackRate,
                AttackType       = hero.AttackType,
                BaseAgility      = hero.BaseAgility,
                BaseArmor        = hero.BaseArmor,
                BaseAttackMax    = hero.BaseAttackMax,
                BaseAttackMin    = hero.BaseAttackMin,
                BaseHealth       = hero.BaseHealth,
                BaseHealthRegen  = hero.BaseHealthRegen,
                BaseIntellect    = hero.BaseIntellect,
                BaseMana         = hero.BaseMana,
                BaseManaRegen    = hero.BaseManaRegen,
                BaseMr           = hero.BaseMr,
                BaseStrength     = hero.BaseStrength,
                Icon             = hero.Icon,
                Image            = hero.Image,
                IntellectGain    = hero.IntellectGain,
                Legs             = hero.Legs,
                MoveSpeed        = hero.MoveSpeed,
                Name             = hero.Name,
                PrimaryAttribute = hero.PrimaryAttribute,
                ProjectileSpeed  = hero.ProjectileSpeed,
                StrengthGain     = hero.StrengthGain,
                TurnRate         = hero.TurnRate,
                Roles            = hero.Roles
                                   .Select(r => new RoleDto {
                    Id = r.RoleId, Name = r.Role.Name
                })
                                   .ToList(),
                Abilities = hero.Abilities
                            .Where(a => !string.IsNullOrEmpty(a.Description))
                            .Select(a => new AbilityDto
                {
                    AbilityName       = a.AbilityName,
                    Behavior          = a.Behavior,
                    Cooldown          = a.Cooldown,
                    DamageType        = a.DamageType,
                    Description       = a.Description,
                    Image             = a.Image,
                    ManaCost          = a.ManaCost,
                    Pierce            = a.Pierce,
                    AbilityAttributes = a.AbilityAttributes
                                        .Select(aa => new AbilityAttributeDto
                    {
                        Generated = aa.Generated,
                        Header    = aa.Header,
                        Key       = aa.Key,
                        Value     = aa.Value
                    })
                                        .ToList()
                })
                            .ToList()
            };

            return(heroDto);
        }
Example #23
0
        public async Task <IActionResult> UpdateEntityDetails(HeroDto entity)
        {
            await _heroService.ChangeEntityDetails(entity);

            return(Ok(entity));
        }
Example #24
0
        public async Task <IActionResult> UpdateHero([FromBody] HeroDto heroDto)
        {
            await _heroService.UpdateHero(heroDto);

            return(Created(Constants.HTTP_UPDATED, heroDto));
        }
 public async Task CreateHeroAsync([FromBody] HeroDto hero)
 {
     await _heroService.CreateHeroAsync(hero);
 }
        public async Task CreateHeroAsync(HeroDto hero)
        {
            await _context.AddAsync(_mapper.Map <Hero>(hero));

            await _context.SaveChangesAsync();
        }