65 lines
2.2 KiB
C#
65 lines
2.2 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using LCU.Shared.Common.NotificationApiLib.Models;
|
|
using ReserveManagementSystemUI.Helpers;
|
|
using Serilog;
|
|
using LCU.Library.Internal.LcuHttpClient;
|
|
|
|
namespace ReserveManagementSystemUI.Service
|
|
{
|
|
public class NotificationService
|
|
{
|
|
private ILcuHttpClient _lcuHttpClient;
|
|
private IConfiguration _config;
|
|
private ServiceHelpers _serviceHelper;
|
|
|
|
public NotificationService(ILcuHttpClient lcuHttpClient,
|
|
IConfiguration config,
|
|
ServiceHelpers serviceHelper)
|
|
{
|
|
_lcuHttpClient = lcuHttpClient;
|
|
_config = config;
|
|
_serviceHelper = serviceHelper;
|
|
}
|
|
|
|
public async Task<bool> SendMessageAsync(string recipient)
|
|
{
|
|
List<string> recipients = new List<string>() { recipient };
|
|
string subject = "!Test!";
|
|
string body = "<h1>This is a test notification sent from Blazor Base application!<h1/>";
|
|
|
|
var emailRequest = new EmailRequest
|
|
{
|
|
Recipient = recipients,
|
|
Subject = subject,
|
|
Body = body
|
|
};
|
|
|
|
try
|
|
{
|
|
var apiUrl = _serviceHelper.GetControllerApiUrl("NotificationsUrl", "EmailController");
|
|
var encoded = JsonConvert.SerializeObject(emailRequest);
|
|
var client = _lcuHttpClient.CreateClient(new Uri(apiUrl));
|
|
var content = new StringContent(encoded, Encoding.UTF8, "application/json");
|
|
HttpResponseMessage response = await client.PostAsync(apiUrl, content);
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
Log.Information("Emailer SendMessageAsync Method message sent");
|
|
|
|
return response.IsSuccessStatusCode;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error("Emailer SendMessageAsync Method: {0} {1}", ex.Message, ex.StackTrace);
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|