Skip to content

Commit

Permalink
(#125) frined serice external event udpate
Browse files Browse the repository at this point in the history
  • Loading branch information
SaintAngeLs committed May 17, 2024
1 parent daba825 commit 745b2fc
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,19 @@ public async Task HandleAsync(InviteFriend command, CancellationToken cancellati
// Publish FriendInvited Event
var friendInvitedEvent = new FriendInvited(command.InviterId, command.InviteeId);
string friendInvitedJson = JsonSerializer.Serialize(friendInvitedEvent);
Console.WriteLine($"Publishing FriendInvited event: {friendInvitedJson}");
// Console.WriteLine($"Publishing FriendInvited event: {friendInvitedJson}");
await _messageBroker.PublishAsync(friendInvitedEvent);

// Publish FriendRequestCreated Event
var friendRequestCreatedEvent = new FriendRequestCreated(command.InviterId, command.InviteeId);
string friendRequestCreatedJson = JsonSerializer.Serialize(friendRequestCreatedEvent);
Console.WriteLine($"Publishing FriendRequestCreated event: {friendRequestCreatedJson}");
await _messageBroker.PublishAsync(friendRequestCreatedEvent);
// // Publish FriendRequestCreated Event
// var friendRequestCreatedEvent = new FriendRequestCreated(command.InviterId, command.InviteeId);
// string friendRequestCreatedJson = JsonSerializer.Serialize(friendRequestCreatedEvent);
// // Console.WriteLine($"Publishing FriendRequestCreated event: {friendRequestCreatedJson}");
// await _messageBroker.PublishAsync(friendRequestCreatedEvent);

// Publish FriendRequestSent Event
var friendRequestSentEvent = new FriendRequestSent(command.InviterId, command.InviteeId);
string friendRequestSentJson = JsonSerializer.Serialize(friendRequestSentEvent);
Console.WriteLine($"Publishing FriendRequestSent event: {friendRequestSentJson}");
// Console.WriteLine($"Publishing FriendRequestSent event: {friendRequestSentJson}");
await _messageBroker.PublishAsync(friendRequestSentEvent);


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using Convey.CQRS.Events;
using MiniSpace.Services.Friends.Application.Events.External;
using MiniSpace.Services.Friends.Application.Exceptions;
using MiniSpace.Services.Friends.Application.Services;
using MiniSpace.Services.Friends.Core.Repositories;
using MiniSpace.Services.Friends.Core.Entities;

namespace MiniSpace.Services.Friends.Application.Commands.Handlers
{
public class FriendRequestSentHandler : IEventHandler<FriendRequestSent>
{
private readonly IFriendRepository _friendRepository;
private readonly IEventMapper _eventMapper;
private readonly IMessageBroker _messageBroker;
private readonly IAppContext _appContext;

public FriendRequestSentHandler(IFriendRepository friendRepository, IEventMapper eventMapper, IMessageBroker messageBroker, IAppContext appContext)
{
_friendRepository = friendRepository;
_eventMapper = eventMapper;
_messageBroker = messageBroker;
_appContext = appContext;
}

// public async Task HandleAsync(FriendRequestSent @event, CancellationToken cancellationToken)
// {
// var now = DateTime.UtcNow;
// var request = new FriendRequest(
// inviterId: @event.InviterId,
// inviteeId: @event.InviteeId,
// requestedAt: now,
// state: FriendState.Requested
// );
// await _friendRepository.AddRequestAsync(request);

// var events = _eventMapper.MapAll(request.Events);
// await _messageBroker.PublishAsync(events.ToArray());
// // Console.WriteLine($"FriendInvited event published: InviterId={@event.InviterId}, InviteeId={@event.InviteeId}");
// }

public async Task HandleAsync(FriendRequestSent @event, CancellationToken cancellationToken)
{
try
{
var request = new FriendRequest(
inviterId: @event.InviterId,
inviteeId: @event.InviteeId,
requestedAt: DateTime.UtcNow,
state: FriendState.Requested
);

// await _friendRepository.AddRequestAsync(request);
var events = _eventMapper.MapAll(request.Events);
await _messageBroker.PublishAsync(events.ToArray());
}
catch (Exception ex)
{
// Console.WriteLine($"An error occurred while handling FriendRequestSent: {ex.Message}");
throw;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ public static IApplicationBuilder UseInfrastructure(this IApplicationBuilder app
.SubscribeCommand<InviteFriend>()
.SubscribeCommand<PendingFriendAccept>()
.SubscribeCommand<PendingFriendDecline>()
// .SubscribeEvent<FriendRequestCreated>()
// .SubscribeEvent<FriendRequestSent>()
.SubscribeEvent<FriendRequestCreated>()
.SubscribeEvent<FriendRequestSent>()
.SubscribeEvent<Application.Events.FriendAdded>()
.SubscribeEvent<Application.Events.FriendRemoved>()
.SubscribeEvent<PendingFriendAccepted>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ internal sealed class MessageToLogTemplateMapper : IMessageToLogTemplateMapper
{
After = "Friend request sent from: {InviterId} to: {InviteeId}."
}
},
{
typeof(FriendInvited), new HandlerLogTemplate
{
After = "Friend invited by: {InviterId} to {InviteeId}. Invitation created at: {CreatedAt}."
}
},
{
typeof(FriendRequestCreated), new HandlerLogTemplate
{
After = "Friend request created between requester: {RequesterId} and friend: {FriendId}."
}
}
};

Expand Down

0 comments on commit 745b2fc

Please sign in to comment.