Skip to content

Commit

Permalink
(#125) events serialisations for the friends request sent
Browse files Browse the repository at this point in the history
  • Loading branch information
SaintAngeLs committed May 17, 2024
1 parent 745b2fc commit e462874
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ public async Task HandleAsync(InviteFriend command, CancellationToken cancellati
// 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public static IApplicationBuilder UseInfrastructure(this IApplicationBuilder app
.SubscribeCommand<InviteFriend>()
.SubscribeCommand<PendingFriendAccept>()
.SubscribeCommand<PendingFriendDecline>()
.SubscribeEvent<FriendRequestCreated>()
// .SubscribeEvent<FriendRequestCreated>()
.SubscribeEvent<FriendRequestSent>()
.SubscribeEvent<Application.Events.FriendAdded>()
.SubscribeEvent<Application.Events.FriendRemoved>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace MiniSpace.Services.Notifications.Application.Events.External
{
[Message("friends")]
[Contract]
public class FriendInvited : IEvent, IDomainEvent
{
public Guid InviterId { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace MiniSpace.Services.Notifications.Application.Events.External
{
[Message("friends")]
[Contract]
public class FriendRequestCreated : IEvent
{
public Guid RequesterId { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace MiniSpace.Services.Notifications.Application.Events.External
{
[Message("friends")]
[Contract]
public class FriendRequestSent : IEvent
{
public Guid InviterId { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

namespace MiniSpace.Services.Notifications.Application.Events.External.Handlers
{
public class FriendInvitedHandler : IEventHandler<NotificationCreated>,
public class FriendInvitedHandler :
// IEventHandler<NotificationCreated>,
IEventHandler<FriendInvited>
{
private readonly INotificationRepository _notificationRepository;
Expand All @@ -21,18 +22,18 @@ public FriendInvitedHandler(INotificationRepository notificationRepository, IEve
_messageBroker = messageBroker;
}

public async Task HandleAsync(NotificationCreated @event, CancellationToken cancellationToken)
{
var notification = await _notificationRepository.GetAsync(@event.NotificationId);
if (notification == null)
{
throw new NotificationNotFoundException(@event.NotificationId);
}
// public async Task HandleAsync(NotificationCreated @event, CancellationToken cancellationToken)
// {
// var notification = await _notificationRepository.GetAsync(@event.NotificationId);
// if (notification == null)
// {
// throw new NotificationNotFoundException(@event.NotificationId);
// }

await _notificationRepository.AddAsync(notification);
var events = _eventMapper.MapAll(notification.Events);
await _messageBroker.PublishAsync(events.ToArray());
}
// await _notificationRepository.AddAsync(notification);
// var events = _eventMapper.MapAll(notification.Events);
// await _messageBroker.PublishAsync(events.ToArray());
// }

public async Task HandleAsync(FriendInvited @event, CancellationToken cancellationToken)
{
Expand All @@ -49,6 +50,18 @@ public async Task HandleAsync(FriendInvited @event, CancellationToken cancellati
// Save the notification to the repository
await _notificationRepository.AddAsync(notification);

// Create a new event to indicate that a notification has been created
var notificationCreatedEvent = new NotificationCreated(
notificationId: notification.NotificationId,
userId: notification.UserId,
message: notification.Message,
createdAt: notification.CreatedAt
// status: notification.Status
);

// Publish the NotificationCreated event
await _messageBroker.PublishAsync(notificationCreatedEvent);

// Optionally, if there are any other domain events resulting from this, publish them
// Here we can use the _messageBroker to publish any further events if required by the domain logic
// For instance:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text.Json;
using Convey.CQRS.Events;
using Microsoft.Extensions.Logging;
using MiniSpace.Services.Notifications.Application.Services;
Expand Down Expand Up @@ -27,6 +28,13 @@ public async Task HandleAsync(FriendRequestCreated friendEvent, CancellationToke
{
_logger.LogInformation($"Received FriendRequestCreated event: RequesterId={friendEvent.RequesterId}, FriendId={friendEvent.FriendId}");
Console.WriteLine("**************************************************************************************************************");

string eventJson = JsonSerializer.Serialize(friendEvent, new JsonSerializerOptions { WriteIndented = true });
Console.WriteLine("**************************************************************************************************************");
Console.WriteLine("Received FriendRequestCreated Event JSON:");
Console.WriteLine(eventJson);
Console.WriteLine("**************************************************************************************************************");

var newFriendEvent = new FriendEvent(
id: Guid.NewGuid(),
eventId: Guid.NewGuid(),
Expand All @@ -50,7 +58,7 @@ public async Task HandleAsync(FriendRequestCreated friendEvent, CancellationToke

await _messageBroker.PublishAsync(friendEvent);

await _notificationRepository.AddAsync(notification);
// await _notificationRepository.AddAsync(notification);
_logger.LogInformation($"Stored new friend event and notification for UserId={friendEvent.RequesterId}");
var notificationCreated = new NotificationCreated(
notificationId: notification.NotificationId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace MiniSpace.Services.Notifications.Application.Events.External
{
[Message("friends")]
[Contract]
public class NotificationCreated : IEvent
{
public Guid NotificationId { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace MiniSpace.Services.Notifications.Application.Events.External
{
[Message("friends")]
[Contract]
public class NotificationDeleted : IEvent
{
public Guid NotificationId { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace MiniSpace.Services.Notifications.Application.Events.External
{
[Message("friends")]
[Contract]
public class NotificationUpdated : IEvent
{
public Guid NotificationId { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ public static IApplicationBuilder UseInfrastructure(this IApplicationBuilder app
.SubscribeCommand<CreateNotification>()
.SubscribeCommand<DeleteNotification>()
.SubscribeCommand<UpdateNotificationStatus>()
.SubscribeEvent<FriendRequestCreated>()
// .SubscribeEvent<FriendRequestCreated>()
// .SubscribeEvent<FriendRequestSent>()
// .SubscribeEvent<FriendRequestCreated>()
.SubscribeEvent<FriendRequestSent>()
.SubscribeEvent<FriendInvited>()
.SubscribeEvent<FriendAdded>()
.SubscribeEvent<PendingFriendAccepted>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,24 @@ internal sealed class MessageToLogTemplateMapper : IMessageToLogTemplateMapper
After = "Updated the status of notification with id: {NotificationId} to: {NewStatus}."
}
},
{
typeof(FriendRequestCreated), new HandlerLogTemplate
{typeof(FriendRequestCreated), new HandlerLogTemplate
{
After = "New Friend request created: {NotificationId} "
After = "Processed creation of friend request from {RequesterId} to {FriendId}."
}
},
{typeof(FriendRequestSent), new HandlerLogTemplate
{
After = "Processed friend request sent from {InviterId} to {InviteeId}."
}
},
{typeof(FriendInvited), new HandlerLogTemplate
{
After = "Handled invitation sent by {InviterId} to {InviteeId}."
}
},
{typeof(NotificationCreated), new HandlerLogTemplate
{
After = "Notification created with ID: {NotificationId} for user: {UserId}, message: '{Message}' at {CreatedAt}."
}
},
};
Expand Down

0 comments on commit e462874

Please sign in to comment.