From fb424749c035ea5dbdfe6b345bdcd2e0d1b92e9c Mon Sep 17 00:00:00 2001 From: hello Date: Thu, 12 Sep 2024 20:43:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=AC=E5=9C=B0=E4=BA=8B=E4=BB=B6=E7=9A=84?= =?UTF-8?q?=E8=AE=BE=E8=AE=A1=E4=B8=8E=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Orders/CreateOrderCommandHandler.cs | 5 ++++- .../Extensions/Extensions.cs | 2 ++ .../EmailWhenOrderStartedLocalEventHandler.cs | 18 +++++++++++++++ .../LocalEvents/OrderStartedLocalEvent.cs | 10 +++++++++ .../SmsWhenOrderStartedLocalEventHandler.cs | 18 +++++++++++++++ .../Services/IEmailSender.cs | 10 +++++++++ .../Services/ISmsSender.cs | 10 +++++++++ .../Services/MessageService.cs | 22 +++++++++++++++++++ 8 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 src/HelloShop.OrderingService/LocalEvents/EmailWhenOrderStartedLocalEventHandler.cs create mode 100644 src/HelloShop.OrderingService/LocalEvents/OrderStartedLocalEvent.cs create mode 100644 src/HelloShop.OrderingService/LocalEvents/SmsWhenOrderStartedLocalEventHandler.cs create mode 100644 src/HelloShop.OrderingService/Services/IEmailSender.cs create mode 100644 src/HelloShop.OrderingService/Services/ISmsSender.cs create mode 100644 src/HelloShop.OrderingService/Services/MessageService.cs diff --git a/src/HelloShop.OrderingService/Commands/Orders/CreateOrderCommandHandler.cs b/src/HelloShop.OrderingService/Commands/Orders/CreateOrderCommandHandler.cs index dd213fc..2ab8958 100644 --- a/src/HelloShop.OrderingService/Commands/Orders/CreateOrderCommandHandler.cs +++ b/src/HelloShop.OrderingService/Commands/Orders/CreateOrderCommandHandler.cs @@ -5,13 +5,14 @@ using AutoMapper; using HelloShop.OrderingService.Entities.Buyers; using HelloShop.OrderingService.Entities.Orders; using HelloShop.OrderingService.Infrastructure; +using HelloShop.OrderingService.LocalEvents; using HelloShop.OrderingService.Services; using MediatR; using Microsoft.EntityFrameworkCore; namespace HelloShop.OrderingService.Commands.Orders { - public class CreateOrderCommandHandler(OrderingServiceDbContext dbContext, IMapper mapper) : IRequestHandler + public class CreateOrderCommandHandler(IMediator mediator, OrderingServiceDbContext dbContext, IMapper mapper) : IRequestHandler { public async Task Handle(CreateOrderCommand request, CancellationToken cancellationToken) { @@ -42,6 +43,8 @@ namespace HelloShop.OrderingService.Commands.Orders await dbContext.AddAsync(order, cancellationToken); await dbContext.SaveChangesAsync(cancellationToken); + await mediator.Publish(new OrderStartedLocalEvent(order), cancellationToken); + return await Task.FromResult(true); } } diff --git a/src/HelloShop.OrderingService/Extensions/Extensions.cs b/src/HelloShop.OrderingService/Extensions/Extensions.cs index 2492695..3c8097b 100644 --- a/src/HelloShop.OrderingService/Extensions/Extensions.cs +++ b/src/HelloShop.OrderingService/Extensions/Extensions.cs @@ -33,6 +33,8 @@ namespace HelloShop.OrderingService.Extensions }); builder.Services.AddModelMapper().AddModelValidator(); + + builder.Services.AddTransient().AddTransient(); } public static WebApplication MapApplicationEndpoints(this WebApplication app) diff --git a/src/HelloShop.OrderingService/LocalEvents/EmailWhenOrderStartedLocalEventHandler.cs b/src/HelloShop.OrderingService/LocalEvents/EmailWhenOrderStartedLocalEventHandler.cs new file mode 100644 index 0000000..e455375 --- /dev/null +++ b/src/HelloShop.OrderingService/LocalEvents/EmailWhenOrderStartedLocalEventHandler.cs @@ -0,0 +1,18 @@ +// Copyright (c) HelloShop Corporation. All rights reserved. +// See the license file in the project root for more information. + +using HelloShop.OrderingService.Services; +using MediatR; + +namespace HelloShop.OrderingService.LocalEvents +{ + public class EmailWhenOrderStartedLocalEventHandler(IEmailSender emailSender) : INotificationHandler + { + public Task Handle(OrderStartedLocalEvent notification, CancellationToken cancellationToken) + { + emailSender.SendEmailAsync(notification.Order.BuyerId.ToString(), "Order started", $"Your order {notification.Order.Id} has started", cancellationToken); + + return Task.CompletedTask; + } + } +} \ No newline at end of file diff --git a/src/HelloShop.OrderingService/LocalEvents/OrderStartedLocalEvent.cs b/src/HelloShop.OrderingService/LocalEvents/OrderStartedLocalEvent.cs new file mode 100644 index 0000000..dca5fe4 --- /dev/null +++ b/src/HelloShop.OrderingService/LocalEvents/OrderStartedLocalEvent.cs @@ -0,0 +1,10 @@ +// Copyright (c) HelloShop Corporation. All rights reserved. +// See the license file in the project root for more information. + +using HelloShop.OrderingService.Entities.Orders; +using MediatR; + +namespace HelloShop.OrderingService.LocalEvents +{ + public record class OrderStartedLocalEvent(Order Order) : INotification; +} diff --git a/src/HelloShop.OrderingService/LocalEvents/SmsWhenOrderStartedLocalEventHandler.cs b/src/HelloShop.OrderingService/LocalEvents/SmsWhenOrderStartedLocalEventHandler.cs new file mode 100644 index 0000000..1bfdb4a --- /dev/null +++ b/src/HelloShop.OrderingService/LocalEvents/SmsWhenOrderStartedLocalEventHandler.cs @@ -0,0 +1,18 @@ +// Copyright (c) HelloShop Corporation. All rights reserved. +// See the license file in the project root for more information. + +using HelloShop.OrderingService.Services; +using MediatR; + +namespace HelloShop.OrderingService.LocalEvents +{ + public class SmsWhenOrderStartedLocalEventHandler(ISmsSender smsSender) : INotificationHandler + { + public Task Handle(OrderStartedLocalEvent notification, CancellationToken cancellationToken) + { + smsSender.SendSmsAsync(notification.Order.BuyerId.ToString(), $"Your order {notification.Order.Id} has started", cancellationToken); + + return Task.CompletedTask; + } + } +} diff --git a/src/HelloShop.OrderingService/Services/IEmailSender.cs b/src/HelloShop.OrderingService/Services/IEmailSender.cs new file mode 100644 index 0000000..1a6c54d --- /dev/null +++ b/src/HelloShop.OrderingService/Services/IEmailSender.cs @@ -0,0 +1,10 @@ +// Copyright (c) HelloShop Corporation. All rights reserved. +// See the license file in the project root for more information. + +namespace HelloShop.OrderingService.Services +{ + public interface IEmailSender + { + Task SendEmailAsync(string email, string subject, string message, CancellationToken cancellationToken = default); + } +} diff --git a/src/HelloShop.OrderingService/Services/ISmsSender.cs b/src/HelloShop.OrderingService/Services/ISmsSender.cs new file mode 100644 index 0000000..377cc55 --- /dev/null +++ b/src/HelloShop.OrderingService/Services/ISmsSender.cs @@ -0,0 +1,10 @@ +// Copyright (c) HelloShop Corporation. All rights reserved. +// See the license file in the project root for more information. + +namespace HelloShop.OrderingService.Services +{ + public interface ISmsSender + { + Task SendSmsAsync(string number, string message, CancellationToken cancellationToken = default); + } +} diff --git a/src/HelloShop.OrderingService/Services/MessageService.cs b/src/HelloShop.OrderingService/Services/MessageService.cs new file mode 100644 index 0000000..e8e43fa --- /dev/null +++ b/src/HelloShop.OrderingService/Services/MessageService.cs @@ -0,0 +1,22 @@ +// Copyright (c) HelloShop Corporation. All rights reserved. +// See the license file in the project root for more information. + +using Microsoft.Extensions.Logging; + +namespace HelloShop.OrderingService.Services +{ + public class MessageService(ILogger logger) : IEmailSender, ISmsSender + { + public Task SendEmailAsync(string email, string subject, string message, CancellationToken cancellationToken = default) + { + logger.LogInformation("Sending email to {Email} with subject {Subject} and message {Message}", email, subject, message); + return Task.CompletedTask; + } + + public Task SendSmsAsync(string number, string message, CancellationToken cancellationToken = default) + { + logger.LogInformation("Sending SMS to {Number} with message {Message}", number, message); + return Task.CompletedTask; + } + } +}