using HackerNet.Application; namespace HackerNet.Infrastructure.Repositories.Memory; public class MemoryReadStore : IReadStore { private readonly MemoryLinkRepository _linkRepository; private readonly MemoryCommentRepository _commentRepository; public MemoryReadStore(MemoryLinkRepository linkRepository, MemoryCommentRepository commentRepository) { _linkRepository = linkRepository; _commentRepository = commentRepository; } public LinkComment[] GetLinkComments(Guid linkId) => _commentRepository.Comments .OrderByDescending(c => c.CreatedAt) .Select(c => new LinkComment { Content = c.Content }).ToArray(); public LinkHomePage GetLinkDetail(Guid id) => GetLinks(id).Single(); public LinkHomePage[] GetPublishedLinks() { // return (from l in _links // orderby l.CreatedAt descending // select new LinkHomePage // { // Id = l.Id, // Url = l.Url, // Description = l.Description, // CommentsCount = 0, // }).ToArray(); return GetLinks().ToArray(); } private IEnumerable GetLinks(Guid? id = null) { return _linkRepository.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 = _commentRepository.Comments .Count(c => c.LinkId == l.Id), }); } }