70 lines
2.0 KiB
C#
70 lines
2.0 KiB
C#
using System;
|
|
using System.Linq;
|
|
using Domain;
|
|
|
|
namespace Application
|
|
{
|
|
public class CommentService
|
|
{
|
|
private readonly ILinkRepository _linkRepository;
|
|
private readonly ICommentRepository _commentRepository;
|
|
private readonly ICurrentUserProvider _userProvider;
|
|
private readonly IData _data;
|
|
|
|
public CommentService(ILinkRepository linkRepository, ICommentRepository commentRepository, ICurrentUserProvider userProvider, IData data)
|
|
{
|
|
_linkRepository = linkRepository;
|
|
_commentRepository = commentRepository;
|
|
_userProvider = userProvider;
|
|
_data = data;
|
|
}
|
|
|
|
public Guid PublishComment(PublishCommentCommand cmd)
|
|
{
|
|
var link = _linkRepository.GetById(cmd.LinkId);
|
|
var comment = link.AddComment(_userProvider.GetCurrentUserId(), 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<CommentDTO> CommentDTOs(Guid? linkId = null)
|
|
{
|
|
var comments = linkId.HasValue ?
|
|
_data.Comments.Where(comment => comment.LinkId == linkId)
|
|
: _data.Comments;
|
|
|
|
return from comment in comments
|
|
join user in _data.Users on comment.CreatedBy equals user.Id
|
|
select new CommentDTO
|
|
{
|
|
Id = comment.Id,
|
|
Content = comment.Content,
|
|
UpvotesCount = 0,
|
|
DownvotesCount = 0,
|
|
CreatedByUsername = user.UserName
|
|
};
|
|
|
|
// return comments
|
|
// .OrderByDescending(comment => comment.CreatedAt)
|
|
// .Select(comment => new CommentDTO
|
|
// {
|
|
// Id = comment.Id,
|
|
// Content = comment.Content,
|
|
// UpvotesCount = 0,
|
|
// DownvotesCount = 0,
|
|
// });
|
|
}
|
|
}
|
|
} |