From 68c5147743189f5c82b032dc5b0f32b91e38f847 Mon Sep 17 00:00:00 2001 From: 83585-Vanderlan Silva Date: Mon, 30 Oct 2023 16:47:24 -0300 Subject: [PATCH] Add Notification to User Created --- .../UserCreate/UserCreateRequestHandler.cs | 15 +++++++---- .../UserCreated/UserCreatedNotification.cs | 15 +++++++++++ .../UserCreatedNotificationHandler.cs | 25 +++++++++++++++++++ .../Orion.Application.Core.csproj | 3 +++ 4 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 src/Orion.Application.Core/Notifications/UserCreated/UserCreatedNotification.cs create mode 100644 src/Orion.Application.Core/Notifications/UserCreated/UserCreatedNotificationHandler.cs 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 @@ + + +