public async Task SyncPopularNewsCommentsAsync(DateTimeOffset fromDate, int newsCount) { var popularNews = _newsArticleDataService .GetPopularNews(fromDate) .Take(newsCount) .ToArray(); foreach (var newsArticle in popularNews) { if (int.TryParse(newsArticle.ExternalId, out int id)) { var commentsIdsResponse = await _sportsRuApiService.GetCommentsIdsAsync(id, MessageClass.News, Sort.Top10).ConfigureAwait(false); if (commentsIdsResponse.IsSuccess) { var commentsByIdsResponse = await _sportsRuApiService.GetCommentsByIds(commentsIdsResponse.Content).ConfigureAwait(false); if (commentsByIdsResponse.IsSuccess) { foreach (var comment in commentsByIdsResponse.Content.Data.Comments) { var existingComment = _sportsContext.NewsArticlesComments.FirstOrDefault(x => x.ExternalId == comment.Id.ToString(CultureInfo.InvariantCulture)); if (existingComment == null) { var newComment = new NewsArticleComment() { NewsArticleId = newsArticle.NewsArticleId, NewsArticle = newsArticle, ExternalId = comment.Id.ToString(CultureInfo.InvariantCulture), }; Map(comment, newComment); _sportsContext.NewsArticlesComments.Add(newComment); } else { Map(comment, existingComment); _sportsContext.NewsArticlesComments.Update(existingComment); } } } else { _logger.LogWarning($"Can't get comments by ids.\nRequest data: {JsonSerializer.Serialize(commentsIdsResponse.Content)}.\nResponse data: {commentsByIdsResponse.ErrorMessage}"); } } } } _sportsContext.SaveChanges(); }
public async Task GetCommentsByIds() { var commentsIds = new int[] { 1084853975, 1084853842 };//comments ids from https://www.sports.ru/football/1084853230.html var commentsByIdsResponse = await _sportsRuApiService.GetCommentsByIds(commentsIds).ConfigureAwait(false); Assert.True(commentsByIdsResponse.IsSuccess, commentsByIdsResponse.ErrorMessage); Assert.NotNull(commentsByIdsResponse.Content); Assert.NotNull(commentsByIdsResponse.Content.Data); Assert.NotNull(commentsByIdsResponse.Content.Data.Comments); Assert.NotEmpty(commentsByIdsResponse.Content.Data.Comments); foreach (var comment in commentsByIdsResponse.Content.Data.Comments) { Assert.NotEqual(0, comment.Id); Assert.NotNull(comment.Text); Assert.NotNull(comment.Rating); } WritePrettyJson(commentsByIdsResponse.Content); }