private static void Map(CommentInfo from, NewsArticleComment to) { to.Rating = from.Rating.Plus + from.Rating.Minus; to.Text = from.Text .Replace("<br />", "\n", StringComparison.OrdinalIgnoreCase) .Replace(""", "\"", StringComparison.OrdinalIgnoreCase); }
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(); }