hn-20-2/Application/CommentService.cs

43 lines
1.1 KiB
C#

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 void PublishComment(PublishCommentCommand cmd)
{
var link = _linkRepository.GetById(cmd.LinkId);
var comment = link.AddComment(cmd.Content);
_commentRepository.Add(comment);
}
public CommentDTO[] GetAllLinkComments(Guid linkId)
{
return _data.Comments
.OrderByDescending(comment => comment.CreatedAt)
.Where(comment => comment.LinkId == linkId)
.Select(comment => new CommentDTO
{
Id = comment.Id,
Content = comment.Content,
UpvotesCount = 0,
DownvotesCount = 0,
})
.ToArray();
}
}
}