From df635d79c04e0c870f1a1742d95e7d703b7d8be8 Mon Sep 17 00:00:00 2001 From: Souvik Mazumder Date: Fri, 4 Feb 2022 05:13:48 -0500 Subject: [PATCH] Implemented feedback feature --- .../Controllers/EmailController.cs | 48 +++++++++++++++++ Library.Encyclopedia.API/appsettings.json | 7 ++- .../Email/EmailAdapter.cs | 52 +++++++++++++++++++ .../Library.Encyclopedia.DataAccess.csproj | 2 + .../Library.Encyclopedia.Entity.csproj | 1 + .../Models/EmailPropertiesV1.cs | 17 ++++++ 6 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 Library.Encyclopedia.API/Controllers/EmailController.cs create mode 100644 Library.Encyclopedia.DataAccess/Email/EmailAdapter.cs create mode 100644 Library.Encyclopedia.Entity/Models/EmailPropertiesV1.cs diff --git a/Library.Encyclopedia.API/Controllers/EmailController.cs b/Library.Encyclopedia.API/Controllers/EmailController.cs new file mode 100644 index 0000000..f4fd07b --- /dev/null +++ b/Library.Encyclopedia.API/Controllers/EmailController.cs @@ -0,0 +1,48 @@ +using Library.Encyclopedia.DataAccess.Email; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.DirectoryServices.AccountManagement; +using System.Linq; +using System.Threading.Tasks; + +namespace Library.Encyclopedia.API.Controllers +{ + [ApiController] + [Route("[controller]")] + public class EmailController : ControllerBase + { + public readonly EmailAdapter adapter; + private readonly ILogger logger; + + public EmailController(ILogger logger, IConfiguration configuration) + { + adapter = new EmailAdapter(configuration); + this.logger = logger; + } + + [HttpPost] + [Authorize] + public async Task SendEmail([FromBody]string content) + { + try + { + using (var context = new PrincipalContext(ContextType.Domain | ContextType.Machine)) + { + var usr = UserPrincipal.FindByIdentity(context, this.HttpContext.User.Identity.Name); + await adapter.SendEmail(usr.DisplayName, usr.EmailAddress, content); + + return Ok(); + } + } + catch (Exception ex) + { + logger.LogError(ex, $"an error has occured {ex.Message}"); + throw; + } + } + } +} diff --git a/Library.Encyclopedia.API/appsettings.json b/Library.Encyclopedia.API/appsettings.json index b0058e8..d8b1515 100644 --- a/Library.Encyclopedia.API/appsettings.json +++ b/Library.Encyclopedia.API/appsettings.json @@ -28,5 +28,10 @@ // For Windows "filePath": "C:\\inetpub\\wwwroot\\PFW Encyclopedia Files\\" - } + }, + "SendGrid-APIKEY": "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", + "Template-Id": "d-af8da9debd13445898a1d82e2674e329", + "Admin-Email": "mazus01@pfw.edu", + "Admin-Username": "Souvik Mazumder", + "PFW-EmailList-Group": "Test" } \ No newline at end of file diff --git a/Library.Encyclopedia.DataAccess/Email/EmailAdapter.cs b/Library.Encyclopedia.DataAccess/Email/EmailAdapter.cs new file mode 100644 index 0000000..8378950 --- /dev/null +++ b/Library.Encyclopedia.DataAccess/Email/EmailAdapter.cs @@ -0,0 +1,52 @@ +using Library.Encyclopedia.Entity.Models; +using Microsoft.Extensions.Configuration; +using SendGrid; +using SendGrid.Helpers.Mail; +using System; +using System.Collections.Generic; +using System.DirectoryServices.AccountManagement; +using System.Text; +using System.Threading.Tasks; + +namespace Library.Encyclopedia.DataAccess.Email +{ + public class EmailAdapter + { + private readonly IConfiguration configuration; + + public EmailAdapter(IConfiguration configuration) + { + this.configuration = configuration; + } + + public async Task SendEmail(string userName, string email, string content) + { + var apiKey = configuration.GetSection("SendGrid-APIKEY").Value; + var client = new SendGridClient(apiKey); + var from = new EmailAddress(configuration.GetSection("Admin-Email").Value, configuration.GetSection("Admin-Username").Value); + + var to = new List(); + + using (var context = new PrincipalContext(ContextType.Machine)) + { + using (var group = GroupPrincipal.FindByIdentity(context, configuration.GetSection("PFW-EmailList-Group").Value)) + { + if (group != null) + { + var members = group.GetMembers(true); + foreach (UserPrincipal user in members) + { + to.Add(new EmailAddress { Email = user.EmailAddress, Name = user.DisplayName }); + } + } + } + } + + var msg = MailHelper.CreateSingleTemplateEmailToMultipleRecipients(from, to, configuration.GetSection("Template-Id").Value, new EmailPropertiesV1 { Content = content, Name = userName, Email = email }); + var response = await client.SendEmailAsync(msg); + + if (!response.IsSuccessStatusCode) + throw new Exception("Email not sent!"); + } + } +} diff --git a/Library.Encyclopedia.DataAccess/Library.Encyclopedia.DataAccess.csproj b/Library.Encyclopedia.DataAccess/Library.Encyclopedia.DataAccess.csproj index c75a2ba..1b6b377 100644 --- a/Library.Encyclopedia.DataAccess/Library.Encyclopedia.DataAccess.csproj +++ b/Library.Encyclopedia.DataAccess/Library.Encyclopedia.DataAccess.csproj @@ -19,7 +19,9 @@ runtime; build; native; contentfiles; analyzers; buildtransitive + + diff --git a/Library.Encyclopedia.Entity/Library.Encyclopedia.Entity.csproj b/Library.Encyclopedia.Entity/Library.Encyclopedia.Entity.csproj index f6f8e6a..66d87ed 100644 --- a/Library.Encyclopedia.Entity/Library.Encyclopedia.Entity.csproj +++ b/Library.Encyclopedia.Entity/Library.Encyclopedia.Entity.csproj @@ -7,6 +7,7 @@ + diff --git a/Library.Encyclopedia.Entity/Models/EmailPropertiesV1.cs b/Library.Encyclopedia.Entity/Models/EmailPropertiesV1.cs new file mode 100644 index 0000000..c50a039 --- /dev/null +++ b/Library.Encyclopedia.Entity/Models/EmailPropertiesV1.cs @@ -0,0 +1,17 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Library.Encyclopedia.Entity.Models +{ + public class EmailPropertiesV1 + { + [JsonProperty("content")] + public string Content { get; set; } + [JsonProperty("name")] + public string Name { get; set; } + [JsonProperty("email")] + public string Email { get; set; } + } +}