add exception filter when user not connected default to needing authentication and apply anonymous to some actions add user in get requests add user relation in link, comment and vote signup and in are ok now!
39 lines
1.1 KiB
C#
39 lines
1.1 KiB
C#
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using HN.Domain;
|
|
using MediatR;
|
|
|
|
namespace HN.Application
|
|
{
|
|
public sealed class VoteForCommentCommandHandler : IRequestHandler<VoteForCommentCommand>
|
|
{
|
|
private readonly ICommentRepository _commentRepository;
|
|
private readonly IExecutingUserProvider _executingUserProvider;
|
|
|
|
public VoteForCommentCommandHandler(ICommentRepository commentRepository, IExecutingUserProvider executingUserProvider)
|
|
{
|
|
_commentRepository = commentRepository;
|
|
_executingUserProvider = executingUserProvider;
|
|
}
|
|
|
|
public async Task<Unit> Handle(VoteForCommentCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var comment = await _commentRepository.GetByIdAsync(request.CommentId);
|
|
var userId = _executingUserProvider.GetCurrentUserId();
|
|
|
|
switch (request.Type)
|
|
{
|
|
case VoteType.Up:
|
|
comment.Upvote(userId);
|
|
break;
|
|
case VoteType.Down:
|
|
comment.Downvote(userId);
|
|
break;
|
|
}
|
|
|
|
await _commentRepository.UpdateAsync(comment);
|
|
|
|
return Unit.Value;
|
|
}
|
|
}
|
|
} |