diff --git a/src/DevNews.Core/Repository/IArticlesRepository.cs b/src/DevNews.Core/Repository/IArticlesRepository.cs index a31ff2a..b9ff509 100644 --- a/src/DevNews.Core/Repository/IArticlesRepository.cs +++ b/src/DevNews.Core/Repository/IArticlesRepository.cs @@ -10,5 +10,7 @@ public interface IArticlesRepository { Task Exists(Article article); Task> InsertMany(IEnumerable
articles); + + IAsyncEnumerable
Get(int page, int pageSize); } } \ No newline at end of file diff --git a/src/DevNews.Infrastructure.Persistence/Model/MongoArticle.cs b/src/DevNews.Infrastructure.Persistence/Model/MongoArticle.cs index 832a908..d55db00 100644 --- a/src/DevNews.Infrastructure.Persistence/Model/MongoArticle.cs +++ b/src/DevNews.Infrastructure.Persistence/Model/MongoArticle.cs @@ -11,22 +11,33 @@ public class MongoArticle public string? Title { get; init; } [BsonElement] public string? Link { get; init; } + + [BsonElement] public string? Content { get; init; } [BsonElement, BsonDateTimeOptions(Kind = DateTimeKind.Utc)] public DateTime CrawledAt { get; init; } [BsonElement, BsonDateTimeOptions(Kind = DateTimeKind.Utc)] public DateTime? PublishedAt { get; init; } - - public MongoArticle() {} + + public MongoArticle() + { + + } public MongoArticle(Article article) { - var (title, _, link) = article; + var (title, content, link) = article; Title = title; Link = link; + Content = content; CrawledAt = DateTime.UtcNow; PublishedAt = DateTime.UtcNow; } + + public Article AsArticle() + { + return new(Title ?? "", Content, Link ?? ""); + } } } \ No newline at end of file diff --git a/src/DevNews.Infrastructure.Persistence/Repository/MongoArticlesRepository.cs b/src/DevNews.Infrastructure.Persistence/Repository/MongoArticlesRepository.cs index e40670e..b623cf2 100644 --- a/src/DevNews.Infrastructure.Persistence/Repository/MongoArticlesRepository.cs +++ b/src/DevNews.Infrastructure.Persistence/Repository/MongoArticlesRepository.cs @@ -46,6 +46,33 @@ public async Task> InsertMany(IEnumerable
artic } } + public async IAsyncEnumerable
Get(int page, int pageSize) + { + if (page <= 0) + { + throw new ArgumentOutOfRangeException(nameof(page)); + } + + if (pageSize <= 0) + { + throw new ArgumentOutOfRangeException(nameof(pageSize)); + } + + var skip = (page - 1) * pageSize; + + var result = await _articles.AsQueryable().OrderBy(x => x.CrawledAt).Skip(skip).Take(pageSize) + .ToListAsync(); + if (result is null) + { + yield break; + } + + foreach (var mongoArticle in result) + { + yield return mongoArticle.AsArticle(); + } + } + private static IMongoDatabase GetDatabase(IMongoClient client) { return client.GetDatabase("Articles");