Skip to content

Commit

Permalink
Add Notification to User Created
Browse files Browse the repository at this point in the history
  • Loading branch information
vanderlan committed Oct 30, 2023
1 parent 6d3ff86 commit 68c5147
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
using MediatR;
using Orion.Application.Core.Notifications.UserCreated;
using Orion.Domain.Core.Services.Interfaces;

namespace Orion.Application.Core.Commands.UserCreate;

public class UserCreateRequestHandler : IRequestHandler<UserCreateRequest, UserCreateResponse>
{
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<UserCreateResponse> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using MediatR;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace Orion.Application.Core.Notifications.UserCreated
{
public class UserCreatedNotificationHandler : INotificationHandler<UserCreatedNotification>
{
private readonly ILogger<UserCreatedNotificationHandler> _logger;

public UserCreatedNotificationHandler(ILogger<UserCreatedNotificationHandler> 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;
}
}
}
3 changes: 3 additions & 0 deletions src/Orion.Application.Core/Orion.Application.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

<ItemGroup>
<PackageReference Include="FluentValidation" Version="11.5.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>

</Project>

0 comments on commit 68c5147

Please sign in to comment.