From 3b58e9bc59e6132f7770a3bd58b894423fa2ef0d Mon Sep 17 00:00:00 2001 From: Mohamed Walaa Date: Wed, 5 Oct 2022 11:52:39 +0200 Subject: [PATCH] Creating Notification Handler for sending email instead of do it inside command handler --- .../Controllers/OrderController.cs | 2 ++ .../CheckoutOrder/CheckoutOrderCommand.cs | 1 + .../CheckoutOrderCommandHandler.cs | 22 +----------- .../CheckoutOrderAddedNotification.cs | 7 ++++ .../CheckoutOrderAddedSendEmailHandler.cs | 35 +++++++++++++++++++ 5 files changed, 46 insertions(+), 21 deletions(-) create mode 100644 src/Services/Ordering/Ordering.Application/Features/Orders/Notifications/CheckoutOrder/CheckoutOrderAddedNotification.cs create mode 100644 src/Services/Ordering/Ordering.Application/Features/Orders/Notifications/CheckoutOrder/CheckoutOrderAddedSendEmailHandler.cs diff --git a/src/Services/Ordering/Ordering.API/Controllers/OrderController.cs b/src/Services/Ordering/Ordering.API/Controllers/OrderController.cs index a6cef40..c0a00aa 100644 --- a/src/Services/Ordering/Ordering.API/Controllers/OrderController.cs +++ b/src/Services/Ordering/Ordering.API/Controllers/OrderController.cs @@ -9,6 +9,7 @@ using System.Collections.Generic; using System.Net; using System.Threading.Tasks; +using Ordering.Application.Features.Orders.Notifications.CheckoutOrder; namespace Ordering.API.Controllers { @@ -38,6 +39,7 @@ public async Task>> GetOrdersByUserName(strin public async Task> CheckoutOrder([FromBody] CheckoutOrderCommand command) { var result = await _mediator.Send(command); + await _mediator.Publish(new CheckoutOrderAddedNotification(result)); return Ok(result); } diff --git a/src/Services/Ordering/Ordering.Application/Features/Orders/Commands/CheckoutOrder/CheckoutOrderCommand.cs b/src/Services/Ordering/Ordering.Application/Features/Orders/Commands/CheckoutOrder/CheckoutOrderCommand.cs index 8cbe243..f18d147 100644 --- a/src/Services/Ordering/Ordering.Application/Features/Orders/Commands/CheckoutOrder/CheckoutOrderCommand.cs +++ b/src/Services/Ordering/Ordering.Application/Features/Orders/Commands/CheckoutOrder/CheckoutOrderCommand.cs @@ -1,4 +1,5 @@ using MediatR; +using Ordering.Domain.Entities; namespace Ordering.Application.Features.Orders.Commands.CheckoutOrder { diff --git a/src/Services/Ordering/Ordering.Application/Features/Orders/Commands/CheckoutOrder/CheckoutOrderCommandHandler.cs b/src/Services/Ordering/Ordering.Application/Features/Orders/Commands/CheckoutOrder/CheckoutOrderCommandHandler.cs index d298d59..d7285f1 100644 --- a/src/Services/Ordering/Ordering.Application/Features/Orders/Commands/CheckoutOrder/CheckoutOrderCommandHandler.cs +++ b/src/Services/Ordering/Ordering.Application/Features/Orders/Commands/CheckoutOrder/CheckoutOrderCommandHandler.cs @@ -1,9 +1,7 @@ using AutoMapper; using MediatR; using Microsoft.Extensions.Logging; -using Ordering.Application.Contracts.Infrastructure; using Ordering.Application.Contracts.Persistence; -using Ordering.Application.Models; using Ordering.Domain.Entities; using System; using System.Threading; @@ -15,14 +13,12 @@ public class CheckoutOrderCommandHandler : IRequestHandler _logger; - public CheckoutOrderCommandHandler(IOrderRepository orderRepository, IMapper mapper, IEmailService emailService, ILogger logger) + public CheckoutOrderCommandHandler(IOrderRepository orderRepository, IMapper mapper, ILogger logger) { _orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository)); _mapper = mapper ?? throw new ArgumentNullException(nameof(mapper)); - _emailService = emailService ?? throw new ArgumentNullException(nameof(emailService)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } @@ -32,24 +28,8 @@ public async Task Handle(CheckoutOrderCommand request, CancellationToken ca var newOrder = await _orderRepository.AddAsync(orderEntity); _logger.LogInformation($"Order {newOrder.Id} is successfully created."); - - await SendMail(newOrder); return newOrder.Id; } - - private async Task SendMail(Order order) - { - var email = new Email() { To = "ezozkme@gmail.com", Body = $"Order was created.", Subject = "Order was created" }; - - try - { - await _emailService.SendEmail(email); - } - catch (Exception ex) - { - _logger.LogError($"Order {order.Id} failed due to an error with the mail service: {ex.Message}"); - } - } } } diff --git a/src/Services/Ordering/Ordering.Application/Features/Orders/Notifications/CheckoutOrder/CheckoutOrderAddedNotification.cs b/src/Services/Ordering/Ordering.Application/Features/Orders/Notifications/CheckoutOrder/CheckoutOrderAddedNotification.cs new file mode 100644 index 0000000..9ec50ec --- /dev/null +++ b/src/Services/Ordering/Ordering.Application/Features/Orders/Notifications/CheckoutOrder/CheckoutOrderAddedNotification.cs @@ -0,0 +1,7 @@ +using MediatR; + +namespace Ordering.Application.Features.Orders.Notifications.CheckoutOrder +{ + public record CheckoutOrderAddedNotification(int orderId) : INotification; + +} diff --git a/src/Services/Ordering/Ordering.Application/Features/Orders/Notifications/CheckoutOrder/CheckoutOrderAddedSendEmailHandler.cs b/src/Services/Ordering/Ordering.Application/Features/Orders/Notifications/CheckoutOrder/CheckoutOrderAddedSendEmailHandler.cs new file mode 100644 index 0000000..25e2972 --- /dev/null +++ b/src/Services/Ordering/Ordering.Application/Features/Orders/Notifications/CheckoutOrder/CheckoutOrderAddedSendEmailHandler.cs @@ -0,0 +1,35 @@ +using MediatR; +using Microsoft.Extensions.Logging; +using Ordering.Application.Contracts.Infrastructure; +using Ordering.Application.Models; +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace Ordering.Application.Features.Orders.Notifications.CheckoutOrder +{ + public class CheckoutOrderAddedSendEmailHandler : INotificationHandler + { + private readonly IEmailService _emailService; + private readonly ILogger _logger; + public CheckoutOrderAddedSendEmailHandler(IEmailService emailService, ILogger logger) + { + _emailService = emailService; + _logger = logger; + } + + public async Task Handle(CheckoutOrderAddedNotification notification, CancellationToken cancellationToken) + { + var email = new Email() { To = "ezozkme@gmail.com", Body = $"Order was created.", Subject = "Order was created" }; + + try + { + await _emailService.SendEmail(email); + } + catch (Exception ex) + { + _logger.LogError($"Order {notification.orderId} failed due to an error with the mail service: {ex.Message}"); + } + } + } +}