Skip to content

Commit

Permalink
add Get Articles method
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikus1993 committed Apr 8, 2021
1 parent 48c7d47 commit 59f10f7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/DevNews.Core/Repository/IArticlesRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ public interface IArticlesRepository
{
Task<bool> Exists(Article article);
Task<Either<Exception, Unit>> InsertMany(IEnumerable<Article> articles);

IAsyncEnumerable<Article> Get(int page, int pageSize);
}
}
17 changes: 14 additions & 3 deletions src/DevNews.Infrastructure.Persistence/Model/MongoArticle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? "");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,33 @@ public async Task<Either<Exception, Unit>> InsertMany(IEnumerable<Article> artic
}
}

public async IAsyncEnumerable<Article> 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");
Expand Down

0 comments on commit 59f10f7

Please sign in to comment.