-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #144 from FS-FAST-TRACK/feature/system-modules
Feature/System Modules
- Loading branch information
Showing
27 changed files
with
1,113 additions
and
16 deletions.
There are no files selected for viewing
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
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
10 changes: 10 additions & 0 deletions
10
WebApp/backend/QuizMaster.API.Gatewway/Configuration/ApplicationSettings.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,10 @@ | ||
using QuizMaster.API.Authentication.Configuration; | ||
|
||
namespace QuizMaster.API.Gateway.Configuration | ||
{ | ||
public class ApplicationSettings: AppSettings | ||
{ | ||
public string SMTP_EMAIL { get; set; } | ||
public string SMTP_PASSWORD { get; set; } | ||
} | ||
} |
201 changes: 201 additions & 0 deletions
201
WebApp/backend/QuizMaster.API.Gatewway/Controllers/SystemController.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,201 @@ | ||
using Grpc.Net.Client; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Options; | ||
using Newtonsoft.Json; | ||
using QuizMaster.API.Authentication.Models; | ||
using QuizMaster.API.Authentication.Proto; | ||
using QuizMaster.API.Gateway.Attributes; | ||
using QuizMaster.API.Gateway.Configuration; | ||
using QuizMaster.API.Gateway.Helper; | ||
using QuizMaster.API.Gateway.Helper.Email; | ||
using QuizMaster.API.Gateway.Models.System; | ||
using QuizMaster.API.Gateway.Services.Email; | ||
using QuizMaster.API.Gateway.Services.SystemService; | ||
using QuizMaster.API.Gatewway.Controllers; | ||
|
||
namespace QuizMaster.API.Gateway.Controllers | ||
{ | ||
[Route("gateway/api/[controller]")] | ||
public class SystemController : Controller | ||
{ | ||
private readonly SystemRepository systemRepository; | ||
private readonly EmailService emailService; | ||
private readonly GrpcChannel _authChannel; | ||
private readonly AuthService.AuthServiceClient _authChannelClient; | ||
private readonly ILogger<SystemController> logger; | ||
|
||
public SystemController(SystemRepository systemRepository, EmailService emailService, IOptions<GrpcServerConfiguration> options, ILogger<SystemController> logger) | ||
{ | ||
this.systemRepository = systemRepository; | ||
this.emailService = emailService; | ||
|
||
var handler = new HttpClientHandler(); | ||
handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator; | ||
_authChannel = GrpcChannel.ForAddress(options.Value.Authentication_Service, new GrpcChannelOptions { HttpHandler = handler }); | ||
_authChannelClient = new AuthService.AuthServiceClient(_authChannel); | ||
this.logger = logger; | ||
} | ||
|
||
// Get System About | ||
[HttpGet("information")] | ||
public async Task<IActionResult> GetSystemInformationAsync() | ||
{ | ||
return Ok(new { Status = "Success", Message = "Retrieved System Information", Data = await systemRepository.GetSystemAboutAsync() }); | ||
} | ||
|
||
// Update System About | ||
[QuizMasterAdminAuthorization] | ||
[HttpPost("information")] | ||
public async Task<IActionResult> UpdateSystemInformationAsync([FromBody] AboutModel model) | ||
{ | ||
if(!ModelState.IsValid) | ||
{ | ||
return BadRequest(ModelState); | ||
} | ||
await systemRepository.EditSystemAboutAsync(model); | ||
return Ok(new { Status = "Success", Message = "Saved", Data = await systemRepository.GetSystemAboutAsync() }); | ||
} | ||
|
||
// Get System Contact | ||
[HttpGet("contact_information")] | ||
public async Task<IActionResult> GetContactInformationAsync() | ||
{ | ||
return Ok(new { Status = "Success", Message = "Retrieved Contact Information", Data = await systemRepository.GetContactInformationAsync() }); | ||
} | ||
|
||
// Update System Contact | ||
[QuizMasterAdminAuthorization] | ||
[HttpPost("contact_information")] | ||
public async Task<IActionResult> UpdateContactInformation([FromBody] ContactModel model) | ||
{ | ||
if (!ModelState.IsValid) | ||
{ | ||
return BadRequest(ModelState); | ||
} | ||
await systemRepository.EditSystemContactInformationAsync(model); | ||
return Ok(new { Status = "Success", Message = "Saved", Data = await systemRepository.GetContactInformationAsync() }); | ||
} | ||
|
||
// Submit Contact | ||
[HttpPost("reachOut")] | ||
public async Task<IActionResult> ReachOutAsync([FromBody] SubmitContactModel model) | ||
{ | ||
if (!ModelState.IsValid) | ||
{ | ||
return BadRequest(ModelState); | ||
} | ||
await systemRepository.SaveReachingContactAsync(model); | ||
|
||
#region Send Email | ||
// I'll run this in a thread | ||
var contactInformation = await systemRepository.GetContactInformationAsync(); | ||
Task.Run(async () => | ||
{ | ||
// Lets try sending the email the the user authenticated, otherwise we'll send it to the email specified in the model | ||
var token = this.GetToken(); | ||
var email = model.Email; | ||
if (!string.IsNullOrEmpty(token)) | ||
{ | ||
var authStore = await GetAuthStoreInfo(token); | ||
if (authStore != null) email = authStore.UserData.Email; | ||
} | ||
|
||
// Create the templates and send the corresponding emails | ||
var clientEmail = EmailDefaults.SUBMIT_CONTACT_CLIENT(email); | ||
emailService.SendEmail(clientEmail); | ||
|
||
logger.LogInformation("Sending Email Copy to: " + contactInformation.Email); | ||
var registeredAdminContactCopy = EmailDefaults.SUBMIT_CONTACT_ADMIN(contactInformation.Email, model.Firstname + " " + model.Lastname, model.Message); | ||
emailService.SendEmail(registeredAdminContactCopy); | ||
emailService.SendEmailToAdmin(registeredAdminContactCopy); | ||
}); | ||
#endregion | ||
|
||
|
||
|
||
return Ok(new { Status = "Success", Message = "Successfully submitted a contact request." }); | ||
} | ||
|
||
// Get All Submitted Contact | ||
[QuizMasterAdminAuthorization] | ||
[HttpGet("reachOut")] | ||
public async Task<IActionResult> GetAllReachOutAsync() | ||
{ | ||
return Ok(new { Status = "Success", Message = "Retrieved Contacting Users", Data = await systemRepository.GetContactReachingsAsync() }); | ||
} | ||
|
||
// Submit Review | ||
[HttpPost("review")] | ||
public async Task<IActionResult> SubmitReviewAsync([FromBody] ReviewModel model) | ||
{ | ||
if (!ModelState.IsValid) | ||
{ | ||
return BadRequest(ModelState); | ||
} | ||
await systemRepository.SaveReviewsAsync(model); | ||
|
||
#region Send Email | ||
var contactInformation = await systemRepository.GetContactInformationAsync(); | ||
// I'll run this in a separate thread | ||
Task.Run(async () => | ||
{ | ||
var token = this.GetToken(); | ||
if (!string.IsNullOrEmpty(token)) | ||
{ | ||
var AuthStore = await GetAuthStoreInfo(token); | ||
|
||
if (AuthStore != null) | ||
{ | ||
var clientEmail = EmailDefaults.SUBMIT_REVIEW_CLIENT(AuthStore.UserData.Email, model.Comment, model.StarRating); | ||
emailService.SendEmail(clientEmail); | ||
|
||
} | ||
} | ||
/* | ||
* | ||
* This code right here will send an email to the SMTP account, which I commented it out first since we priority on | ||
* the admin that will receive the email must be the email specified in the contact us | ||
var adminEmailCopy = EmailDefaults.SUBMIT_REVIEW_ADMIN("", model.Comment, model.StarRating); | ||
emailService.SendEmailToAdmin(adminEmailCopy); | ||
*/ | ||
var registeredAdminContactCopy = EmailDefaults.SUBMIT_REVIEW_ADMIN(contactInformation.Email, model.Comment, model.StarRating); | ||
logger.LogInformation("Sending Email Copy to: " + contactInformation.Email); | ||
emailService.SendEmail(registeredAdminContactCopy); | ||
emailService.SendEmailToAdmin(registeredAdminContactCopy); | ||
}); | ||
#endregion | ||
|
||
return Ok(new { Status = "Success", Message = "Successfully submitted a system review." }); | ||
} | ||
|
||
// Get All Submitted Review | ||
[QuizMasterAdminAuthorization] | ||
[HttpGet("review")] | ||
public async Task<IActionResult> GetAllReviewsAsync() | ||
{ | ||
return Ok(new { Status = "Success", Message = "Retrieved System Reviews", Data = await systemRepository.GetReviewsAsync() }); | ||
} | ||
|
||
[HttpGet("review/client")] | ||
public async Task<IActionResult> GetAllReviewsClientCopyAsync() | ||
{ | ||
var data = await systemRepository.GetReviewsAsync(); | ||
data = data.Where(r => r.StarRating >= 4).ToList(); | ||
return Ok(new { Status = "Success", Message = "Retrieved System Reviews", Data = data }); | ||
} | ||
|
||
|
||
|
||
private async Task<AuthStore> GetAuthStoreInfo(string token) | ||
{ | ||
var requestValidation = new ValidationRequest() | ||
{ | ||
Token = token | ||
}; | ||
|
||
var authStore = await _authChannelClient.ValidateAuthenticationAsync(requestValidation); | ||
|
||
return !string.IsNullOrEmpty(authStore?.AuthStore) ? JsonConvert.DeserializeObject<AuthStore>(authStore.AuthStore) : null; | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
WebApp/backend/QuizMaster.API.Gatewway/Helper/Email/EmailTemplate.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,32 @@ | ||
namespace QuizMaster.API.Gateway.Helper.Email | ||
{ | ||
public class EmailTemplate | ||
{ | ||
public string Body { get; set; } = string.Empty; | ||
public string ToEmail { get; set; } = string.Empty; | ||
public string Subject { get; set; } = string.Empty; | ||
} | ||
|
||
public class EmailDefaults | ||
{ | ||
public static EmailTemplate SUBMIT_REVIEW_ADMIN(string ToEmail, string Message, int Rating) | ||
{ | ||
return new EmailTemplate { Body = $"Someone reviewed: {Message}, Rating: {Rating}/5", ToEmail = ToEmail, Subject = "[QuizMaster] System Review" }; | ||
} | ||
|
||
public static EmailTemplate SUBMIT_REVIEW_CLIENT(string ToEmail, string Message, int Rating) | ||
{ | ||
return new EmailTemplate { Body = $"You reviewed: {Message}, Rating: {Rating}/5", ToEmail = ToEmail, Subject = "[QuizMaster] System Review" }; | ||
} | ||
|
||
public static EmailTemplate SUBMIT_CONTACT_ADMIN(string ToEmail, string Name, string Message) | ||
{ | ||
return new EmailTemplate { Body = $"Hello admin, this {Name} would like to reach us out with a following message: {Message}", ToEmail = ToEmail, Subject = "[QuizMaster] Someone's reaching us" }; | ||
} | ||
|
||
public static EmailTemplate SUBMIT_CONTACT_CLIENT(string ToEmail) | ||
{ | ||
return new EmailTemplate { Body = $"Thank you for reaching us, we will reach you out soonest.", ToEmail = ToEmail, Subject = "[QuizMaster] Thanks for reaching us" }; | ||
} | ||
} | ||
} |
Oops, something went wrong.