public IHttpActionResult PutPlayerLoadoutsEntity(int id, PlayerLoadoutsEntity playerLoadoutsEntity)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != playerLoadoutsEntity.DbId)
            {
                return(BadRequest());
            }

            _dbContext.Entry(playerLoadoutsEntity).State = EntityState.Modified;

            try
            {
                _dbContext.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PlayerLoadoutsEntityExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public IHttpActionResult PostPlayerLoadoutsEntity(PlayerLoadoutsEntity playerLoadoutsEntity)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _dbContext.PlayerLoadoutsEntities.Add(playerLoadoutsEntity);
            _dbContext.SaveChanges();

            return(CreatedAtRoute("DefaultApi", new { id = playerLoadoutsEntity.DbId }, playerLoadoutsEntity));
        }
        public async Task <IHttpActionResult> RetrievePlayerFromApi(string id)
        {
            var player = _dbContext.PlayerEntities.FirstOrDefault(p => p.Name.Equals(id))
                         ?? new PlayerEntity {
                lastUpdated = DateTime.UtcNow.AddHours(-1)
            };

            if (!(DateTime.UtcNow.Subtract(player.lastUpdated).TotalMinutes > 30))
            {
                /* Return 417 in order the client understand that the player did not updated */
                return(StatusCode(HttpStatusCode.ExpectationFailed));
            }

            var isPlayerInDb = player.Name != null;

            try
            {
                #region Update Player

                var playerFromApi = await _paladinsApi.GetPlayer(id);

                var newplayer = new PlayerEntity(playerFromApi)
                {
                    DbId = player.DbId,
                    Champions_Last_Updated = DateTime.Now,
                    History_Last_Updated   = DateTime.Now,
                    lastUpdated            = DateTime.Now
                };
                Mapper.Initialize(cfg =>
                {
                    cfg.CreateMap <PlayerEntity, PlayerEntity>();
                    cfg.CreateMap <PlayerAchievementsEntity, PlayerAchievementsEntity>();
                    cfg.CreateMap <PlayerChampionRankEntity, PlayerChampionRankEntity>();
                    cfg.CreateMap <PlayerStatusEntity, PlayerStatusEntity>();
                    cfg.CreateMap <PlayerLoadoutsEntity, PlayerLoadoutsEntity>();
                });
                Mapper.Map(newplayer, player);

                if (isPlayerInDb)
                {
                    PutPlayerEntity(player.DbId, player);
                }
                else
                {
                    PostPlayerEntity(player);
                }

                #endregion

                #region Update Achievements

                if (isPlayerInDb)
                {
                    var achievements = _dbContext.PlayerAchievementsEntities
                                       .FirstOrDefault(a => a.Id == newplayer.Id);

                    if (achievements == null)
                    {
                        return(BadRequest());
                    }

                    var achievementsFromApi = await _paladinsApi.GetPlayerAchievements(newplayer.Id);

                    var newAchievements = new PlayerAchievementsEntity(achievementsFromApi)
                    {
                        DbId = achievements.DbId
                    };
                    Mapper.Map(newAchievements, achievements);
                    _achievementsController.PutPlayerAchievementsEntity(achievements.DbId, achievements);
                }
                else
                {
                    var achievementsFromApi = await _paladinsApi.GetPlayerAchievements(newplayer.Id);

                    _achievementsController.PostPlayerAchievementsEntity(new PlayerAchievementsEntity(achievementsFromApi));
                }

                #endregion

                #region Update ChampionRanks

                var ranks = _dbContext.PlayerChampionRankEntities
                            .Where(r => r.player_id.Equals(newplayer.Id.ToString()));

                var ranksFromApi = await _paladinsApi.GetChampionRanks(newplayer.Id);

                if (ranks.Any())
                {
                    foreach (var rank in ranks)
                    {
                        var foundRank = ranksFromApi
                                        .Find(r => r.champion_id.Equals(rank.champion_id));
                        var selectedRank = new PlayerChampionRankEntity(foundRank)
                        {
                            DbId = rank.DbId
                        };
                        Mapper.Map(selectedRank, rank);
                        _championRanksController.PutPlayerChampionRankEntity(rank.DbId, rank);
                        ranksFromApi.Remove(foundRank);
                    }
                }

                foreach (var rankEntity in ranksFromApi)
                {
                    var selectedRank = new PlayerChampionRankEntity(rankEntity);
                    _championRanksController.PostPlayerChampionRankEntity(selectedRank);
                }

                #endregion

                #region Update Player Status

                if (isPlayerInDb)
                {
                    var status = _dbContext.PlayerStatusEntities
                                 .FirstOrDefault(s => s.playerId == newplayer.Id);

                    if (status == null)
                    {
                        return(BadRequest());
                    }

                    var statusFromApi = await _paladinsApi.GetPlayerStatus(newplayer.Name);

                    var newStatus = new PlayerStatusEntity(statusFromApi)
                    {
                        DbId = status.DbId
                    };

                    /* When user is offline, api returns 0 in playerId */
                    if (newStatus.playerId == 0)
                    {
                        newStatus.playerId = newplayer.Id;
                    }

                    Mapper.Map(newStatus, status);
                    _statusController.PutPlayerStatusEntity(status.DbId, status);
                }
                else
                {
                    var statusFromApi = await _paladinsApi.GetPlayerStatus(newplayer.Name);

                    _statusController.PostPlayerStatusEntity(new PlayerStatusEntity(statusFromApi));
                }

                #endregion

                #region Update PlayerLoadouts

                var loadouts = _dbContext.PlayerLoadoutsEntities
                               .Where(l => l.PlayerId == newplayer.Id)
                               .ToList();

                var loadoutsFromApi = await _paladinsApi.GetPlayerLoadouts(newplayer.Id);

                if (loadoutsFromApi.Any())
                {
                    foreach (var loadout in loadouts)
                    {
                        var foundLoadout = loadoutsFromApi
                                           .Find(l => l.DeckId == loadout.DeckId);
                        var selectedLoadout = new PlayerLoadoutsEntity(foundLoadout)
                        {
                            DbId = loadout.DbId
                        };
                        Mapper.Map(selectedLoadout, loadout);
                        _playerLoadoutsController.PutPlayerLoadoutsEntity(loadout.DbId, loadout);
                        loadoutsFromApi.Remove(foundLoadout);
                    }
                }

                foreach (var loadout in loadoutsFromApi)
                {
                    var selectedLoadout = new PlayerLoadoutsEntity(loadout);
                    _playerLoadoutsController.PostPlayerLoadoutsEntity(selectedLoadout);
                }

                #endregion

                return(StatusCode(HttpStatusCode.NoContent));
            }
            catch (NotFoundException)
            {
                return(StatusCode(HttpStatusCode.NotFound));
            }
        }