2021-12-15 10:22:55 +01:00

42 lines
1017 B
C#

using HackerNet.Application;
namespace HackerNet.Infrastructure.Repositories.EntityFramework;
public class EFReadStore : IReadStore
{
private readonly HackerContext _context;
public EFReadStore(HackerContext context)
{
_context = context;
}
public LinkComment[] GetLinkComments(Guid linkId)
=> _context.Comments
.OrderByDescending(c => c.CreatedAt)
.Select(c => new LinkComment
{
Content = c.Content
}).ToArray();
public LinkHomePage GetLinkDetail(Guid id)
=> GetLinks(id).Single();
public LinkHomePage[] GetPublishedLinks()
=> GetLinks().ToArray();
private IQueryable<LinkHomePage> GetLinks(Guid? id = null)
{
return _context.Links
.Where(l => !id.HasValue || l.Id == id)
.OrderByDescending(l => l.CreatedAt)
.Select(l => new LinkHomePage
{
Id = l.Id,
Url = l.Url,
Description = l.Description,
CommentsCount = _context.Comments
.Count(c => c.LinkId == l.Id),
});
}
}