From 0f671de43cbc7dec5bdfa2d6253c80b634f5c827 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Wed, 18 Jan 2023 03:20:57 +0200 Subject: [PATCH] Service structure --- Correo.sln | 7 +++ dependencies.props | 2 + .../CommandHandlers/SendEmailHandler.cs | 49 +++++++++++++++++++ .../Correo.Application.csproj | 7 +++ .../DependencyInjectionExtensions.cs | 14 ++++++ .../Mappings/MappingProfile.cs | 15 ++++++ .../Queries/GetSystemVersion.cs | 1 - src/Correo.Domain/Correo.Domain.csproj | 7 +++ src/Correo.Domain/Models/EmailMessage.cs | 17 +++++++ src/Correo.Domain/Models/Settings.cs | 4 ++ src/Correo.PublishedLanguage/Class1.cs | 7 --- .../Commands/SendEmail.cs | 18 +++++++ .../Events/Records.cs | 7 +++ src/Correo/Controllers/SystemController.cs | 17 +++++-- src/Correo/Correo.csproj | 1 + src/Correo/Extensions/StartupExtensions.cs | 3 ++ src/Correo/appsettings.json | 18 ++++++- 17 files changed, 181 insertions(+), 13 deletions(-) create mode 100644 src/Correo.Application/CommandHandlers/SendEmailHandler.cs create mode 100644 src/Correo.Application/DependencyInjectionExtensions.cs create mode 100644 src/Correo.Application/Mappings/MappingProfile.cs create mode 100644 src/Correo.Domain/Correo.Domain.csproj create mode 100644 src/Correo.Domain/Models/EmailMessage.cs create mode 100644 src/Correo.Domain/Models/Settings.cs delete mode 100644 src/Correo.PublishedLanguage/Class1.cs create mode 100644 src/Correo.PublishedLanguage/Commands/SendEmail.cs create mode 100644 src/Correo.PublishedLanguage/Events/Records.cs diff --git a/Correo.sln b/Correo.sln index 1c5cffa..29e20eb 100644 --- a/Correo.sln +++ b/Correo.sln @@ -22,6 +22,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Correo.Application", "src\C EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Correo.PublishedLanguage", "src\Correo.PublishedLanguage\Correo.PublishedLanguage.csproj", "{DA47BCEE-9AE7-4F20-A04F-5866966503C5}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Correo.Domain", "src\Correo.Domain\Correo.Domain.csproj", "{A2D2694C-AB68-49B0-B4B0-94BBFF48C043}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -40,6 +42,10 @@ Global {DA47BCEE-9AE7-4F20-A04F-5866966503C5}.Debug|Any CPU.Build.0 = Debug|Any CPU {DA47BCEE-9AE7-4F20-A04F-5866966503C5}.Release|Any CPU.ActiveCfg = Release|Any CPU {DA47BCEE-9AE7-4F20-A04F-5866966503C5}.Release|Any CPU.Build.0 = Release|Any CPU + {A2D2694C-AB68-49B0-B4B0-94BBFF48C043}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A2D2694C-AB68-49B0-B4B0-94BBFF48C043}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A2D2694C-AB68-49B0-B4B0-94BBFF48C043}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A2D2694C-AB68-49B0-B4B0-94BBFF48C043}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -48,6 +54,7 @@ Global {B712E585-E63F-4A6D-8E17-FEFACE04BBE2} = {245E2FBE-DFDF-40B4-94B7-5DDA216E58AD} {3AEA1AB4-F068-4C39-A173-AD65F3F8E2F2} = {245E2FBE-DFDF-40B4-94B7-5DDA216E58AD} {DA47BCEE-9AE7-4F20-A04F-5866966503C5} = {245E2FBE-DFDF-40B4-94B7-5DDA216E58AD} + {A2D2694C-AB68-49B0-B4B0-94BBFF48C043} = {245E2FBE-DFDF-40B4-94B7-5DDA216E58AD} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {86FCF989-26FC-41E9-8A23-9485606D619D} diff --git a/dependencies.props b/dependencies.props index ab137b6..ce84682 100644 --- a/dependencies.props +++ b/dependencies.props @@ -4,6 +4,8 @@ 6.1.0 5.5.0 6.2.3 + 12.0.1 + 12.0.0 9.0.0 6.0.30 diff --git a/src/Correo.Application/CommandHandlers/SendEmailHandler.cs b/src/Correo.Application/CommandHandlers/SendEmailHandler.cs new file mode 100644 index 0000000..96422c1 --- /dev/null +++ b/src/Correo.Application/CommandHandlers/SendEmailHandler.cs @@ -0,0 +1,49 @@ +using AutoMapper; +using Correo.Domain.Models; +using Correo.PublishedLanguage.Commands; +using Correo.PublishedLanguage.Events; +using MediatR; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using NBB.Messaging.Abstractions; +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace Correo.Application.CommandHandlers +{ + public class SendEmailHandler : IRequestHandler + { + private readonly IMessageBusPublisher _messageBusPublisher; + private readonly ILogger _logger; + private readonly IMapper _mapper; + private readonly IOptions _defaultSender; + + public SendEmailHandler(IMessageBusPublisher messageBusPublisher, ILogger logger, IMapper mapper, IOptions defaultSender) + { + _messageBusPublisher=messageBusPublisher; + _logger=logger; + _mapper=mapper; + _defaultSender=defaultSender; + } + + public async Task Handle(SendEmail command, CancellationToken cancellationToken) + { + try + { + var emailMessage = _mapper.Map(command); + if (emailMessage.From == null) + emailMessage.From = new EmailMessage.MailAddress(_defaultSender.Value.Address, _defaultSender.Value.Name); + + // send email + } + catch (Exception ex) + { + await _messageBusPublisher.PublishAsync(new EmailSentFailed(command.Subject, ex.Message), cancellationToken); + throw; + } + + return Unit.Value; + } + } +} diff --git a/src/Correo.Application/Correo.Application.csproj b/src/Correo.Application/Correo.Application.csproj index 9c6904d..1e842d8 100644 --- a/src/Correo.Application/Correo.Application.csproj +++ b/src/Correo.Application/Correo.Application.csproj @@ -5,6 +5,13 @@ + + + + + + + diff --git a/src/Correo.Application/DependencyInjectionExtensions.cs b/src/Correo.Application/DependencyInjectionExtensions.cs new file mode 100644 index 0000000..0fd3631 --- /dev/null +++ b/src/Correo.Application/DependencyInjectionExtensions.cs @@ -0,0 +1,14 @@ +using Correo.Domain.Models; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; + +namespace Correo.Application +{ + public static class DependencyInjectionExtensions + { + public static void AddApplicationServices(this IServiceCollection services, IConfiguration configuration) + { + services.Configure(configuration.GetSection("DefaultSender")); + } + } +} diff --git a/src/Correo.Application/Mappings/MappingProfile.cs b/src/Correo.Application/Mappings/MappingProfile.cs new file mode 100644 index 0000000..32b3305 --- /dev/null +++ b/src/Correo.Application/Mappings/MappingProfile.cs @@ -0,0 +1,15 @@ +using AutoMapper; +using Correo.Domain.Models; +using Correo.PublishedLanguage.Commands; + +namespace Correo.Application.Mappings +{ + public class MappingProfile : Profile + { + public MappingProfile() + { + CreateMap(); + CreateMap(); + } + } +} diff --git a/src/Correo.Application/Queries/GetSystemVersion.cs b/src/Correo.Application/Queries/GetSystemVersion.cs index e98f839..dc70921 100644 --- a/src/Correo.Application/Queries/GetSystemVersion.cs +++ b/src/Correo.Application/Queries/GetSystemVersion.cs @@ -9,7 +9,6 @@ namespace Correo.Application.Queries { public record Query : IRequest { - public int CountyId { get; init; } } public record Model diff --git a/src/Correo.Domain/Correo.Domain.csproj b/src/Correo.Domain/Correo.Domain.csproj new file mode 100644 index 0000000..dbc1517 --- /dev/null +++ b/src/Correo.Domain/Correo.Domain.csproj @@ -0,0 +1,7 @@ + + + + net6.0 + + + diff --git a/src/Correo.Domain/Models/EmailMessage.cs b/src/Correo.Domain/Models/EmailMessage.cs new file mode 100644 index 0000000..aaf4665 --- /dev/null +++ b/src/Correo.Domain/Models/EmailMessage.cs @@ -0,0 +1,17 @@ +using System.Collections.Generic; + +namespace Correo.Domain.Models +{ + public record EmailMessage + { + public string Subject { get; init; } + public string Body { get; init; } + public MailAddress From { get; set; } + public IEnumerable To { get; init; } + public IEnumerable Cc { get; init; } + public IEnumerable Bcc { get; init; } + public bool IsBodyHtml { get; init; } + + public record MailAddress(string Address, string DisplayName); + } +} diff --git a/src/Correo.Domain/Models/Settings.cs b/src/Correo.Domain/Models/Settings.cs new file mode 100644 index 0000000..b479757 --- /dev/null +++ b/src/Correo.Domain/Models/Settings.cs @@ -0,0 +1,4 @@ +namespace Correo.Domain.Models +{ + public record DefaultSender(string Address, string Name); +} diff --git a/src/Correo.PublishedLanguage/Class1.cs b/src/Correo.PublishedLanguage/Class1.cs deleted file mode 100644 index d476516..0000000 --- a/src/Correo.PublishedLanguage/Class1.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Correo.PublishedLanguage -{ - public class Class1 - { - - } -} \ No newline at end of file diff --git a/src/Correo.PublishedLanguage/Commands/SendEmail.cs b/src/Correo.PublishedLanguage/Commands/SendEmail.cs new file mode 100644 index 0000000..e30bd59 --- /dev/null +++ b/src/Correo.PublishedLanguage/Commands/SendEmail.cs @@ -0,0 +1,18 @@ +using MediatR; +using System.Collections.Generic; + +namespace Correo.PublishedLanguage.Commands +{ + public record SendEmail : IRequest + { + public string Subject { get; init; } + public string Body { get; init; } + public MailAddress From { get; init; } + public IEnumerable To { get; init; } + public IEnumerable Cc { get; init; } + public IEnumerable Bcc { get; init; } + public bool IsBodyHtml { get; init; } + + public record MailAddress(string Address, string DisplayName); + } +} diff --git a/src/Correo.PublishedLanguage/Events/Records.cs b/src/Correo.PublishedLanguage/Events/Records.cs new file mode 100644 index 0000000..87ee821 --- /dev/null +++ b/src/Correo.PublishedLanguage/Events/Records.cs @@ -0,0 +1,7 @@ +using MediatR; + +namespace Correo.PublishedLanguage.Events +{ + public record EmailSent(string Subject) : INotification; + public record EmailSentFailed(string Subject, string ErrorMessage) : INotification; +} diff --git a/src/Correo/Controllers/SystemController.cs b/src/Correo/Controllers/SystemController.cs index 0fe5966..84e8f23 100644 --- a/src/Correo/Controllers/SystemController.cs +++ b/src/Correo/Controllers/SystemController.cs @@ -1,6 +1,8 @@ +using Correo.Application.Queries; +using MediatR; using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Logging; using System; +using System.Threading.Tasks; namespace Correo.Controllers { @@ -8,11 +10,11 @@ namespace Correo.Controllers [Route("[controller]")] public class SystemController : ControllerBase { - private readonly ILogger _logger; + private readonly IMediator _mediator; - public SystemController(ILogger logger) + public SystemController(IMediator mediator) { - _logger = logger; + _mediator=mediator; } [HttpGet("ping")] @@ -20,5 +22,12 @@ namespace Correo.Controllers { return $"Ping success. System datetime: {DateTime.Now}"; } + + [HttpGet("version")] + public async Task GetSystemVersion([FromRoute] GetSystemVersion.Query query) + { + var result = await _mediator.Send(query); + return Ok(result); + } } } \ No newline at end of file diff --git a/src/Correo/Correo.csproj b/src/Correo/Correo.csproj index 90cdbcc..47d3913 100644 --- a/src/Correo/Correo.csproj +++ b/src/Correo/Correo.csproj @@ -5,6 +5,7 @@ + diff --git a/src/Correo/Extensions/StartupExtensions.cs b/src/Correo/Extensions/StartupExtensions.cs index 8ea74b4..7978814 100644 --- a/src/Correo/Extensions/StartupExtensions.cs +++ b/src/Correo/Extensions/StartupExtensions.cs @@ -18,6 +18,9 @@ namespace Correo.Extensions services.AddEndpointsApiExplorer(); services.AddSwaggerGen(); + // AutoMapper + services.AddAutoMapper(typeof(Application.Mappings.MappingProfile).Assembly); + // MediatR services.AddMediatR(typeof(Application.Queries.GetSystemVersion).Assembly); services.AddScoped(typeof(IPipelineBehavior<,>), typeof(RequestPreProcessorBehavior<,>)); diff --git a/src/Correo/appsettings.json b/src/Correo/appsettings.json index 308d313..7bdd60b 100644 --- a/src/Correo/appsettings.json +++ b/src/Correo/appsettings.json @@ -24,5 +24,21 @@ "qGroup": "Correo", "durableName": "durable" } + }, + "DefaultSender": { + "Address": "noreply@homelab.com", + "Name": "HomeLab" + }, + "SmtpClient": { + "Server": "smtp.gmail.com", + "Port": "587", + "Authentication": { + "UserName": "@gmail.com", + "Domain": "", + "Password": "********" + }, + "UseAuthentication": true, + "UseSsl": true, + "TrustServer": false } -} +} \ No newline at end of file