Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating Notification Handler #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -38,6 +39,7 @@ public async Task<ActionResult<IEnumerable<OrdersVm>>> GetOrdersByUserName(strin
public async Task<ActionResult<int>> CheckoutOrder([FromBody] CheckoutOrderCommand command)
{
var result = await _mediator.Send(command);
await _mediator.Publish(new CheckoutOrderAddedNotification(result));
return Ok(result);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MediatR;
using Ordering.Domain.Entities;

namespace Ordering.Application.Features.Orders.Commands.CheckoutOrder
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,14 +13,12 @@ public class CheckoutOrderCommandHandler : IRequestHandler<CheckoutOrderCommand,
{
private readonly IOrderRepository _orderRepository;
private readonly IMapper _mapper;
private readonly IEmailService _emailService;
private readonly ILogger<CheckoutOrderCommandHandler> _logger;

public CheckoutOrderCommandHandler(IOrderRepository orderRepository, IMapper mapper, IEmailService emailService, ILogger<CheckoutOrderCommandHandler> logger)
public CheckoutOrderCommandHandler(IOrderRepository orderRepository, IMapper mapper, ILogger<CheckoutOrderCommandHandler> 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));
}

Expand All @@ -32,24 +28,8 @@ public async Task<int> 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 = "[email protected]", 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}");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using MediatR;

namespace Ordering.Application.Features.Orders.Notifications.CheckoutOrder
{
public record CheckoutOrderAddedNotification(int orderId) : INotification;

}
Original file line number Diff line number Diff line change
@@ -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<CheckoutOrderAddedNotification>
{
private readonly IEmailService _emailService;
private readonly ILogger<CheckoutOrderAddedSendEmailHandler> _logger;
public CheckoutOrderAddedSendEmailHandler(IEmailService emailService, ILogger<CheckoutOrderAddedSendEmailHandler> logger)
{
_emailService = emailService;
_logger = logger;
}

public async Task Handle(CheckoutOrderAddedNotification notification, CancellationToken cancellationToken)
{
var email = new Email() { To = "[email protected]", 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}");
}
}
}
}