Skip to content

Commit

Permalink
Implemented feedback feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mazumdes committed Feb 4, 2022
1 parent 6670b1a commit df635d7
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 1 deletion.
48 changes: 48 additions & 0 deletions Library.Encyclopedia.API/Controllers/EmailController.cs
Original file line number Diff line number Diff line change
@@ -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<EmailController> logger;

public EmailController(ILogger<EmailController> logger, IConfiguration configuration)
{
adapter = new EmailAdapter(configuration);
this.logger = logger;
}

[HttpPost]
[Authorize]
public async Task<IActionResult> 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;
}
}
}
}
7 changes: 6 additions & 1 deletion Library.Encyclopedia.API/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
52 changes: 52 additions & 0 deletions Library.Encyclopedia.DataAccess/Email/EmailAdapter.cs
Original file line number Diff line number Diff line change
@@ -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<EmailAddress>();

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!");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="5.0.3" />
<PackageReference Include="SendGrid" Version="9.25.2" />
<PackageReference Include="SSH.NET" Version="2020.0.1" />
<PackageReference Include="System.DirectoryServices.AccountManagement" Version="6.0.0" />
<PackageReference Include="System.Runtime.Caching" Version="6.0.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<ItemGroup>
<PackageReference Include="Enums.NET" Version="4.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Http.Features" Version="5.0.12" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions Library.Encyclopedia.Entity/Models/EmailPropertiesV1.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}

0 comments on commit df635d7

Please sign in to comment.