2021-12-14 09:06:56 +01:00

53 lines
1.1 KiB
C#

using HackerNet.Application;
using HackerNet.Domain;
namespace HackerNet.Infrastructure.Repositories.Memory;
public class MemoryLinkRepository : ILinkRepository, IReadStore
{
private List<Link> _links;
public MemoryLinkRepository(params Link[] links)
{
_links = links.ToList();
}
public void Add(Link link)
{
_links.Add(link);
}
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<LinkHomePage> GetLinks(Guid? id = null)
{
return _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 = 0,
});
}
}