using System; using System.Linq; using Domain; namespace Application { public class CommentService { private readonly ILinkRepository _linkRepository; private readonly ICommentRepository _commentRepository; private readonly IData _data; public CommentService(ILinkRepository linkRepository, ICommentRepository commentRepository, IData data) { _linkRepository = linkRepository; _commentRepository = commentRepository; _data = data; } public Guid PublishComment(PublishCommentCommand cmd) { var link = _linkRepository.GetById(cmd.LinkId); var comment = link.AddComment(cmd.Content); _commentRepository.Add(comment); return comment.Id; } public CommentDTO[] GetAllLinkComments(Guid linkId) { return CommentDTOs(linkId).ToArray(); } public CommentDTO GetCommentById(Guid id) { return CommentDTOs().Single(comment => comment.Id == id); } private IQueryable CommentDTOs(Guid? linkId = null) { var comments = linkId.HasValue ? _data.Comments.Where(comment => comment.LinkId == linkId) : _data.Comments; return comments .OrderByDescending(comment => comment.CreatedAt) .Select(comment => new CommentDTO { Id = comment.Id, Content = comment.Content, UpvotesCount = 0, DownvotesCount = 0, }); } } }