2021-12-15 16:05:50 +01:00

56 lines
1.5 KiB
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 (from link in _context.Links
join user in _context.Users on link.CreatedBy equals user.Id
orderby link.CreatedAt descending
select new LinkHomePage
{
Id = link.Id,
Url = link.Url,
Description = link.Description,
CommentsCount = _context.Comments.Count(c => c.LinkId == link.Id),
Author = user.UserName,
});
// return _context.Links
// .Where(l => !id.HasValue || l.Id == id)
// .OrderByDescending(l => l.CreatedAt)
// .Join()
// .Select(l => new LinkHomePage
// {
// Id = l.Id,
// Url = l.Url,
// Description = l.Description,
// CommentsCount = _context.Comments
// .Count(c => c.LinkId == l.Id),
// });
}
}