public async Task <IEnumerable <Game> > GetGamesAsync() { var lastDate = _context.Games.Select(x => x.PlayedOn).Distinct().OrderByDescending(x => x).FirstOrDefault() ?? new DateTime(2015, 04, 1, 08, 00, 00); lastDate = lastDate.AddMinutes(5); using (var client = new HttpClient()) { // Epoch representation of the date we want to query. // More details: https://developer.riotgames.com/api/methods#!/980/3340 var matchIdsUrl = BaseGameIdsUrl + "?api_key=" + ApiKey + "&beginDate=" + lastDate.ToUnixTime(); var matchIds = JsonConvert.DeserializeObject <IEnumerable <int> >(await(await client.GetAsync(matchIdsUrl)).Content.ReadAsStringAsync()); var games = new List <Game>(); foreach (var id in matchIds.Where(x => !_context.Games.Any(y => x == y.MatchId))) // Verify game isn't inserted yet { var gameRequest = await client.GetAsync(BaseMatchUrl + id + "?api_key=" + ApiKey); var game = JsonConvert.DeserializeObject <Game>(await gameRequest.Content.ReadAsStringAsync()); game.PlayedOn = lastDate; if (!string.IsNullOrWhiteSpace(game.Region)) { _context.Games.Add(game); games.Add(game); } } await _context.SaveChangesAsync(); return(games); } }
public async Task <bool> TryAddRefreshTokenAsync(RefreshToken refreshToken) { var existingToken = _context.RefreshTokens.SingleOrDefault(x => x.Subject == refreshToken.Subject && x.ClientApplicationId == refreshToken.ClientApplicationId); if (existingToken != null) { await TryRemoveRefreshTokenAsync(existingToken); } _context.RefreshTokens.Add(refreshToken); return(await _context.SaveChangesAsync() > 0); }