Skip to content

Commit

Permalink
Infrastructure layer added
Browse files Browse the repository at this point in the history
  • Loading branch information
rahiyansafin committed May 4, 2023
1 parent 1573658 commit 6d4ec81
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 2 deletions.
31 changes: 31 additions & 0 deletions HR.LeaveManagement.Persistence/EmailService/EmailSender.cs
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.Persistence.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;
}
}
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>
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.Persistence.EmailService;
using HR.LeaveManagement.Persistence.Logging;

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace HR.LeaveManagement.Persistence;
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 HR.LeaveManagement.Persistence/Logging/LoggerAdapter.cs
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.Persistence.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);
}
11 changes: 9 additions & 2 deletions HR.LeaveManagement.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "API", "API", "{C3F8BCD4-2E5
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Infrastructure", "Infrastructure", "{806C1EC6-F10D-40B0-A859-1E6D915C2CFB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HR.LeaveManagement.Domain", "HR.LeaveManagement.Domain\HR.LeaveManagement.Domain.csproj", "{77E4397B-55C9-484F-B8FD-9D5C2917B561}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HR.LeaveManagement.Domain", "HR.LeaveManagement.Domain\HR.LeaveManagement.Domain.csproj", "{77E4397B-55C9-484F-B8FD-9D5C2917B561}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HR.LeaveManagement.Application", "HR.LeaveManagement.Application\HR.LeaveManagement.Application.csproj", "{D63F4CEC-1751-4508-89BB-004EF5F2D655}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HR.LeaveManagement.Application", "HR.LeaveManagement.Application\HR.LeaveManagement.Application.csproj", "{D63F4CEC-1751-4508-89BB-004EF5F2D655}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HR.LeaveManagement.Persistence", "HR.LeaveManagement.Persistence\HR.LeaveManagement.Persistence.csproj", "{5CC8F511-EDD3-47A8-9EBF-5B949696991B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -33,6 +35,10 @@ Global
{D63F4CEC-1751-4508-89BB-004EF5F2D655}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D63F4CEC-1751-4508-89BB-004EF5F2D655}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D63F4CEC-1751-4508-89BB-004EF5F2D655}.Release|Any CPU.Build.0 = Release|Any CPU
{5CC8F511-EDD3-47A8-9EBF-5B949696991B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5CC8F511-EDD3-47A8-9EBF-5B949696991B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5CC8F511-EDD3-47A8-9EBF-5B949696991B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5CC8F511-EDD3-47A8-9EBF-5B949696991B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -44,6 +50,7 @@ Global
{806C1EC6-F10D-40B0-A859-1E6D915C2CFB} = {5DA04958-626B-491F-9598-ADD8B89F0205}
{77E4397B-55C9-484F-B8FD-9D5C2917B561} = {79013FD3-7C1C-4AF0-AA5B-8F356DC00404}
{D63F4CEC-1751-4508-89BB-004EF5F2D655} = {79013FD3-7C1C-4AF0-AA5B-8F356DC00404}
{5CC8F511-EDD3-47A8-9EBF-5B949696991B} = {806C1EC6-F10D-40B0-A859-1E6D915C2CFB}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {20D6677C-2E5C-4FCC-9FFB-D6FE8214DA70}
Expand Down

0 comments on commit 6d4ec81

Please sign in to comment.