Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added layer of abstraction to email service #212

Merged
merged 2 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions UT4MasterServer.Services/Interfaces/IEmailService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace UT4MasterServer.Services.Interfaces;

public interface IEmailService
{
Task SendTextEmailAsync(string fromAddress, List<string> toAddresses, string subject, string body);
Task SendHTMLEmailAsync(string fromAddress, List<string> toAddresses, string subject, string body);
}
11 changes: 6 additions & 5 deletions UT4MasterServer.Services/Scoped/AccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@
using UT4MasterServer.Models.Database;
using UT4MasterServer.Models.DTO.Responses;
using UT4MasterServer.Models.Settings;
using UT4MasterServer.Services.Interfaces;

namespace UT4MasterServer.Services.Scoped;

public sealed class AccountService
{
private readonly IMongoCollection<Account> accountCollection;
private readonly ApplicationSettings applicationSettings;
private readonly AwsSesClient awsSesClient;
private readonly IEmailService emailService;

public AccountService(
DatabaseContext dbContext,
IOptions<ApplicationSettings> applicationSettings,
AwsSesClient awsSesClient)
IEmailService emailService)
{
this.applicationSettings = applicationSettings.Value;
accountCollection = dbContext.Database.GetCollection<Account>("accounts");
this.awsSesClient = awsSesClient;
this.emailService = emailService;
}

public async Task CreateIndexesAsync()
Expand Down Expand Up @@ -296,7 +297,7 @@ private async Task SendVerificationLinkAsync(string email, EpicID accountID, str
<p>Click <a href='{uriBuilder.Uri}' target='_blank'>here</a> to verify your email.</p>
";

await awsSesClient.SendHTMLEmailAsync(applicationSettings.NoReplyEmail, new List<string>() { email }, "Email Verification", html);
await emailService.SendHTMLEmailAsync(applicationSettings.NoReplyEmail, new List<string>() { email }, "Email Verification", html);
}

private async Task SendResetPasswordLinkAsync(string email, EpicID accountID, string guid)
Expand All @@ -315,6 +316,6 @@ private async Task SendResetPasswordLinkAsync(string email, EpicID accountID, st
<p>If you didn't initiate password reset, ignore this message.</p>
";

await awsSesClient.SendHTMLEmailAsync(applicationSettings.NoReplyEmail, new List<string>() { email }, "Reset Password", html);
await emailService.SendHTMLEmailAsync(applicationSettings.NoReplyEmail, new List<string>() { email }, "Reset Password", html);
}
}
3 changes: 2 additions & 1 deletion UT4MasterServer.Services/Scoped/AwsSesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
using System.Text.Json;
using UT4MasterServer.Common.Exceptions;
using UT4MasterServer.Models.Settings;
using UT4MasterServer.Services.Interfaces;

namespace UT4MasterServer.Services.Scoped;

public sealed class AwsSesClient
public sealed class AwsSesClient : IEmailService
{
private readonly ILogger<AwsSesClient> _logger;
private readonly IAmazonSimpleEmailService _amazonSimpleEmailService;
Expand Down
11 changes: 6 additions & 5 deletions UT4MasterServer/Controllers/AdminPanelController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using UT4MasterServer.Models.DTO.Requests;
using UT4MasterServer.Models.DTO.Responses;
using UT4MasterServer.Models.Responses;
using UT4MasterServer.Services.Interfaces;
using UT4MasterServer.Services.Scoped;

namespace UT4MasterServer.Controllers;
Expand All @@ -27,7 +28,7 @@ public sealed class AdminPanelController : ControllerBase
private readonly TrustedGameServerService trustedGameServerService;
private readonly MatchmakingService matchmakingService;
private readonly CleanupService cleanupService;
private readonly AwsSesClient awsSesClient;
private readonly IEmailService emailService;

public AdminPanelController(
ILogger<AdminPanelController> logger,
Expand All @@ -38,7 +39,7 @@ public AdminPanelController(
TrustedGameServerService trustedGameServerService,
MatchmakingService matchmakingService,
CleanupService cleanupService,
AwsSesClient awsSesClient)
IEmailService emailService)
{
this.logger = logger;
this.accountService = accountService;
Expand All @@ -48,7 +49,7 @@ public AdminPanelController(
this.trustedGameServerService = trustedGameServerService;
this.matchmakingService = matchmakingService;
this.cleanupService = cleanupService;
this.awsSesClient = awsSesClient;
this.emailService = emailService;
}

#region Accounts
Expand Down Expand Up @@ -571,14 +572,14 @@ public async Task<IActionResult> DeleteMCPFile(string filename)
[HttpPost("send-text-email")]
public async Task<IActionResult> SendTextEmail([FromBody] SendEmailRequest sendEmailRequest)
{
await awsSesClient.SendTextEmailAsync(sendEmailRequest.From, sendEmailRequest.To, sendEmailRequest.Subject, sendEmailRequest.Body);
await emailService.SendTextEmailAsync(sendEmailRequest.From, sendEmailRequest.To, sendEmailRequest.Subject, sendEmailRequest.Body);
return Ok();
}

[HttpPost("send-html-email")]
public async Task<IActionResult> SendHtmlEmail([FromBody] SendEmailRequest sendEmailRequest)
{
await awsSesClient.SendHTMLEmailAsync(sendEmailRequest.From, sendEmailRequest.To, sendEmailRequest.Subject, sendEmailRequest.Body);
await emailService.SendHTMLEmailAsync(sendEmailRequest.From, sendEmailRequest.To, sendEmailRequest.Subject, sendEmailRequest.Body);
return Ok();
}

Expand Down
3 changes: 2 additions & 1 deletion UT4MasterServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using UT4MasterServer.Services.Scoped;
using UT4MasterServer.Services.Singleton;
using UT4MasterServer.Services.Hosted;
using UT4MasterServer.Services.Interfaces;

namespace UT4MasterServer;

Expand Down Expand Up @@ -115,7 +116,7 @@ public static void Main(string[] args)
.AddScoped<MatchmakingService>()
.AddScoped<StatisticsService>()
.AddScoped<RatingsService>()
.AddScoped<AwsSesClient>()
.AddScoped<IEmailService, AwsSesClient>()
.AddScoped<CleanupService>();

// services whose instance is created once and are persistent
Expand Down