-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6d4ec81
commit 0b4f34b
Showing
5 changed files
with
95 additions
and
6 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
HR.LeaveManagement.Infrastructure/EmailService/EmailSender.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using HR.LeaveManagement.Application.Contracts.Email; | ||
using HR.LeaveManagement.Application.Models.Email; | ||
|
||
using Microsoft.Extensions.Options; | ||
|
||
using SendGrid; | ||
using SendGrid.Helpers.Mail; | ||
|
||
namespace HR.LeaveManagement.Infrastructure.EmailService; | ||
public class EmailSender : IEmailSender | ||
{ | ||
public EmailSettings _emailSettings { get; } | ||
public EmailSender(IOptions<EmailSettings> emailSettings) | ||
=> _emailSettings = emailSettings.Value; | ||
|
||
public async Task<bool> SendEmail(EmailMessage email) | ||
{ | ||
var client = new SendGridClient(_emailSettings.ApiKey); | ||
var to = new EmailAddress(email.To); | ||
var from = new EmailAddress | ||
{ | ||
Email = _emailSettings.FromAddress, | ||
Name = _emailSettings.FromName | ||
}; | ||
|
||
var message = MailHelper.CreateSingleEmail(from, to, email.Subject, email.Body, email.Body); | ||
var response = await client.SendEmailAsync(message); | ||
|
||
return response.IsSuccessStatusCode; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
HR.LeaveManagement.Infrastructure/HR.LeaveManagement.Infrastructure.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="7.0.0" /> | ||
<PackageReference Include="SendGrid" Version="9.28.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\HR.LeaveManagement.Application\HR.LeaveManagement.Application.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
20 changes: 20 additions & 0 deletions
20
HR.LeaveManagement.Infrastructure/InfrastructureServicesRegistration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using HR.LeaveManagement.Application.Contracts.Email; | ||
using HR.LeaveManagement.Application.Contracts.Logging; | ||
using HR.LeaveManagement.Application.Models.Email; | ||
using HR.LeaveManagement.Infrastructure.EmailService; | ||
using HR.LeaveManagement.Infrastructure.Logging; | ||
|
||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace HR.LeaveManagement.Infrastructure; | ||
public static class InfrastructureServicesRegistration | ||
{ | ||
public static IServiceCollection AddInfrastructureServices(this IServiceCollection services, IConfiguration configuration) | ||
{ | ||
services.Configure<EmailSettings>(configuration.GetSection("EmailSettings")); | ||
services.AddTransient<IEmailSender, EmailSender>(); | ||
services.AddScoped(typeof(IAppLogger<>), typeof(LoggerAdapter<>)); | ||
return services; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
HR.LeaveManagement.Infrastructure/Logging/LoggerAdapter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using HR.LeaveManagement.Application.Contracts.Logging; | ||
|
||
using Microsoft.Extensions.Logging; | ||
|
||
namespace HR.LeaveManagement.Infrastructure.Logging; | ||
public class LoggerAdapter<T> : IAppLogger<T> | ||
{ | ||
private readonly ILogger<T> _logger; | ||
public LoggerAdapter(ILoggerFactory loggerFactory) | ||
=> _logger = loggerFactory.CreateLogger<T>(); | ||
|
||
public void LogInformation(string message, params object[] args) | ||
=> _logger.LogInformation(message, args); | ||
|
||
public void LogWarning(string message, params object[] args) | ||
=> _logger.LogWarning(message, args); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters