diff --git a/src/Orion.Application.Core/Commands/UserCreate/UserCreateRequestHandler.cs b/src/Orion.Application.Core/Commands/UserCreate/UserCreateRequestHandler.cs index 1f76b31..5c2b7df 100644 --- a/src/Orion.Application.Core/Commands/UserCreate/UserCreateRequestHandler.cs +++ b/src/Orion.Application.Core/Commands/UserCreate/UserCreateRequestHandler.cs @@ -1,4 +1,5 @@ using MediatR; +using Orion.Application.Core.Notifications.UserCreated; using Orion.Domain.Core.Services.Interfaces; namespace Orion.Application.Core.Commands.UserCreate; @@ -6,16 +7,20 @@ namespace Orion.Application.Core.Commands.UserCreate; public class UserCreateRequestHandler : IRequestHandler { private readonly IUserService _userService; - - public UserCreateRequestHandler(IUserService userService) + private readonly IMediator _mediator; + + public UserCreateRequestHandler(IUserService userService, IMediator mediator) { _userService = userService; + _mediator = mediator; } - + public async Task Handle(UserCreateRequest request, CancellationToken cancellationToken) { - var customerCreated = await _userService.AddAsync(request); + var userCreated = await _userService.AddAsync(request); + + await _mediator.Publish(new UserCreatedNotification(userCreated), cancellationToken); - return (UserCreateResponse)customerCreated; + return (UserCreateResponse)userCreated; } } \ No newline at end of file diff --git a/src/Orion.Application.Core/Notifications/UserCreated/UserCreatedNotification.cs b/src/Orion.Application.Core/Notifications/UserCreated/UserCreatedNotification.cs new file mode 100644 index 0000000..48891db --- /dev/null +++ b/src/Orion.Application.Core/Notifications/UserCreated/UserCreatedNotification.cs @@ -0,0 +1,15 @@ +using MediatR; +using Orion.Domain.Core.Entities; + +namespace Orion.Application.Core.Notifications.UserCreated +{ + public class UserCreatedNotification : INotification + { + public UserCreatedNotification(User user) + { + Entity = user; + } + + public User Entity { get; set; } + } +} diff --git a/src/Orion.Application.Core/Notifications/UserCreated/UserCreatedNotificationHandler.cs b/src/Orion.Application.Core/Notifications/UserCreated/UserCreatedNotificationHandler.cs new file mode 100644 index 0000000..50e1f88 --- /dev/null +++ b/src/Orion.Application.Core/Notifications/UserCreated/UserCreatedNotificationHandler.cs @@ -0,0 +1,25 @@ +using MediatR; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; + +namespace Orion.Application.Core.Notifications.UserCreated +{ + public class UserCreatedNotificationHandler : INotificationHandler + { + private readonly ILogger _logger; + + public UserCreatedNotificationHandler(ILogger logger) + { + _logger = logger; + } + + public async Task Handle(UserCreatedNotification notification, CancellationToken cancellationToken) + { + _logger.LogInformation("A notification {notificationType} has been received. Notification details: {notification}", + nameof(UserCreatedNotification), + JsonConvert.SerializeObject(notification, Formatting.Indented)); + + await Task.CompletedTask; + } + } +} diff --git a/src/Orion.Application.Core/Orion.Application.Core.csproj b/src/Orion.Application.Core/Orion.Application.Core.csproj index 5b15b4e..1cadd51 100644 --- a/src/Orion.Application.Core/Orion.Application.Core.csproj +++ b/src/Orion.Application.Core/Orion.Application.Core.csproj @@ -11,6 +11,9 @@ + + +