From 8095c7958090f3b78729ece2f1967b9487adb138 Mon Sep 17 00:00:00 2001 From: Andrii Voznesenskyi Date: Sun, 8 Sep 2024 00:10:02 +0200 Subject: [PATCH] (#411) comments: uopdate add like events handler --- .../Commands/Handlers/AddLikeHandler.cs | 11 +++++++--- .../Events/LikeAdded.cs | 21 +++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 MiniSpace.Services.Comments/src/MiniSpace.Services.Comments.Application/Events/LikeAdded.cs diff --git a/MiniSpace.Services.Comments/src/MiniSpace.Services.Comments.Application/Commands/Handlers/AddLikeHandler.cs b/MiniSpace.Services.Comments/src/MiniSpace.Services.Comments.Application/Commands/Handlers/AddLikeHandler.cs index 75ceb747f..dc5659e13 100644 --- a/MiniSpace.Services.Comments/src/MiniSpace.Services.Comments.Application/Commands/Handlers/AddLikeHandler.cs +++ b/MiniSpace.Services.Comments/src/MiniSpace.Services.Comments.Application/Commands/Handlers/AddLikeHandler.cs @@ -7,7 +7,6 @@ using MiniSpace.Services.Comments.Application.Services.Clients; using MiniSpace.Services.Comments.Core.Entities; using MiniSpace.Services.Comments.Core.Repositories; -using MiniSpace.Services.Comments.Core.Exceptions; using MiniSpace.Services.Comments.Application.Services; using System.Text.Json; @@ -43,8 +42,9 @@ public AddLikeHandler( public async Task HandleAsync(AddLike command, CancellationToken cancellationToken = default) { - var commandJson = JsonSerializer.Serialize(command, new JsonSerializerOptions { WriteIndented = true }); + var commandJson = JsonSerializer.Serialize(command, new JsonSerializerOptions { WriteIndented = true }); Console.WriteLine($"Received AddLike command: {commandJson}"); + var identity = _appContext.Identity; if (!identity.IsAuthenticated || identity.Id != command.UserId) @@ -72,7 +72,12 @@ public async Task HandleAsync(AddLike command, CancellationToken cancellationTok comment.Like(command.UserId); await UpdateCommentAsync(comment, commentContext); - await _messageBroker.PublishAsync(new CommentUpdated(command.CommentId)); + await _messageBroker.PublishAsync(new LikeAdded( + commentId: command.CommentId, + userId: command.UserId, + commentContext: command.CommentContext, + likedAt: DateTime.UtcNow + )); } private async Task GetCommentAsync(Guid commentId, CommentContext context) diff --git a/MiniSpace.Services.Comments/src/MiniSpace.Services.Comments.Application/Events/LikeAdded.cs b/MiniSpace.Services.Comments/src/MiniSpace.Services.Comments.Application/Events/LikeAdded.cs new file mode 100644 index 000000000..06521163e --- /dev/null +++ b/MiniSpace.Services.Comments/src/MiniSpace.Services.Comments.Application/Events/LikeAdded.cs @@ -0,0 +1,21 @@ +using Convey.CQRS.Events; +using System; + +namespace MiniSpace.Services.Comments.Application.Events +{ + public class LikeAdded : IEvent + { + public Guid CommentId { get; } + public Guid UserId { get; } + public string CommentContext { get; } + public DateTime LikedAt { get; } + + public LikeAdded(Guid commentId, Guid userId, string commentContext, DateTime likedAt) + { + CommentId = commentId; + UserId = userId; + CommentContext = commentContext; + LikedAt = likedAt; + } + } +}