Service structure

master
Tudor Stanciu 2023-01-18 03:20:57 +02:00
parent 907a4453cc
commit 0f671de43c
17 changed files with 181 additions and 13 deletions

View File

@ -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}

View File

@ -4,6 +4,8 @@
<SerilogPackageVersion>6.1.0</SerilogPackageVersion>
<SerilogSinksSQLitePackageVersion>5.5.0</SerilogSinksSQLitePackageVersion>
<SwashbucklePackageVersion>6.2.3</SwashbucklePackageVersion>
<AutoMapperPackageVersion>12.0.1</AutoMapperPackageVersion>
<AutoMapperExtensionsPackageVersion>12.0.0</AutoMapperExtensionsPackageVersion>
<MediatRPackageVersion>9.0.0</MediatRPackageVersion>
<NBBPackageVersion>6.0.30</NBBPackageVersion>
</PropertyGroup>

View File

@ -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<SendEmail>
{
private readonly IMessageBusPublisher _messageBusPublisher;
private readonly ILogger<SendEmailHandler> _logger;
private readonly IMapper _mapper;
private readonly IOptions<DefaultSender> _defaultSender;
public SendEmailHandler(IMessageBusPublisher messageBusPublisher, ILogger<SendEmailHandler> logger, IMapper mapper, IOptions<DefaultSender> defaultSender)
{
_messageBusPublisher=messageBusPublisher;
_logger=logger;
_mapper=mapper;
_defaultSender=defaultSender;
}
public async Task<Unit> Handle(SendEmail command, CancellationToken cancellationToken)
{
try
{
var emailMessage = _mapper.Map<EmailMessage>(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;
}
}
}

View File

@ -5,6 +5,13 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="$(AutoMapperPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="$(MicrosoftExtensionsPackageVersion)" />
<PackageReference Include="NBB.Messaging.Abstractions" Version="$(NBBPackageVersion)" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Correo.Domain\Correo.Domain.csproj" />
<ProjectReference Include="..\Correo.PublishedLanguage\Correo.PublishedLanguage.csproj" />
</ItemGroup>

View File

@ -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<DefaultSender>(configuration.GetSection("DefaultSender"));
}
}
}

View File

@ -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<SendEmail.MailAddress, EmailMessage.MailAddress>();
CreateMap<SendEmail, EmailMessage>();
}
}
}

View File

@ -9,7 +9,6 @@ namespace Correo.Application.Queries
{
public record Query : IRequest<Model>
{
public int CountyId { get; init; }
}
public record Model

View File

@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
</Project>

View File

@ -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<MailAddress> To { get; init; }
public IEnumerable<MailAddress> Cc { get; init; }
public IEnumerable<MailAddress> Bcc { get; init; }
public bool IsBodyHtml { get; init; }
public record MailAddress(string Address, string DisplayName);
}
}

View File

@ -0,0 +1,4 @@
namespace Correo.Domain.Models
{
public record DefaultSender(string Address, string Name);
}

View File

@ -1,7 +0,0 @@
namespace Correo.PublishedLanguage
{
public class Class1
{
}
}

View File

@ -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<MailAddress> To { get; init; }
public IEnumerable<MailAddress> Cc { get; init; }
public IEnumerable<MailAddress> Bcc { get; init; }
public bool IsBodyHtml { get; init; }
public record MailAddress(string Address, string DisplayName);
}
}

View File

@ -0,0 +1,7 @@
using MediatR;
namespace Correo.PublishedLanguage.Events
{
public record EmailSent(string Subject) : INotification;
public record EmailSentFailed(string Subject, string ErrorMessage) : INotification;
}

View File

@ -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<SystemController> _logger;
private readonly IMediator _mediator;
public SystemController(ILogger<SystemController> 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<IActionResult> GetSystemVersion([FromRoute] GetSystemVersion.Query query)
{
var result = await _mediator.Send(query);
return Ok(result);
}
}
}

View File

@ -5,6 +5,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="$(AutoMapperExtensionsPackageVersion)" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="$(MediatRPackageVersion)" />
<PackageReference Include="NBB.Messaging.Host" Version="$(NBBPackageVersion)" />
<PackageReference Include="NBB.Messaging.Nats" Version="$(NBBPackageVersion)" />

View File

@ -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<,>));

View File

@ -24,5 +24,21 @@
"qGroup": "Correo",
"durableName": "durable"
}
},
"DefaultSender": {
"Address": "noreply@homelab.com",
"Name": "HomeLab"
},
"SmtpClient": {
"Server": "smtp.gmail.com",
"Port": "587",
"Authentication": {
"UserName": "<account>@gmail.com",
"Domain": "",
"Password": "********"
},
"UseAuthentication": true,
"UseSsl": true,
"TrustServer": false
}
}