messageservice, sendgrid send basic implementation
This commit is contained in:
parent
b520acd675
commit
affed0c7a5
|
|
@ -58,6 +58,21 @@
|
||||||
<Reference Include="AyCode.Interfaces.Server">
|
<Reference Include="AyCode.Interfaces.Server">
|
||||||
<HintPath>..\..\AyCode.Core\AyCode.Services.Server\bin\Debug\net8.0\AyCode.Interfaces.Server.dll</HintPath>
|
<HintPath>..\..\AyCode.Core\AyCode.Services.Server\bin\Debug\net8.0\AyCode.Interfaces.Server.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
|
<Reference Include="AyCode.Models">
|
||||||
|
<HintPath>..\..\AyCode.Core\AyCode.Services.Server\bin\Debug\net8.0\AyCode.Models.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="AyCode.Models.Server">
|
||||||
|
<HintPath>..\..\AyCode.Core\AyCode.Services.Server\bin\Debug\net8.0\AyCode.Models.Server.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="AyCode.Services">
|
||||||
|
<HintPath>..\..\AyCode.Core\AyCode.Services.Server\bin\Debug\net8.0\AyCode.Services.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="AyCode.Services.Server">
|
||||||
|
<HintPath>..\..\AyCode.Core\AyCode.Services.Server\bin\Debug\net8.0\AyCode.Services.Server.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="AyCode.Utils">
|
||||||
|
<HintPath>..\..\AyCode.Core\AyCode.Services.Server\bin\Debug\net8.0\AyCode.Utils.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
||||||
|
|
@ -43,6 +43,21 @@
|
||||||
<Reference Include="AyCode.Interfaces.Server">
|
<Reference Include="AyCode.Interfaces.Server">
|
||||||
<HintPath>..\..\AyCode.Core\AyCode.Services.Server\bin\Debug\net8.0\AyCode.Interfaces.Server.dll</HintPath>
|
<HintPath>..\..\AyCode.Core\AyCode.Services.Server\bin\Debug\net8.0\AyCode.Interfaces.Server.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
|
<Reference Include="AyCode.Models">
|
||||||
|
<HintPath>..\..\AyCode.Core\AyCode.Services.Server\bin\Debug\net8.0\AyCode.Models.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="AyCode.Models.Server">
|
||||||
|
<HintPath>..\..\AyCode.Core\AyCode.Services.Server\bin\Debug\net8.0\AyCode.Models.Server.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="AyCode.Services">
|
||||||
|
<HintPath>..\..\AyCode.Core\AyCode.Services.Server\bin\Debug\net8.0\AyCode.Services.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="AyCode.Services.Server">
|
||||||
|
<HintPath>..\..\AyCode.Core\AyCode.Services.Server\bin\Debug\net8.0\AyCode.Services.Server.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="AyCode.Utils">
|
||||||
|
<HintPath>..\..\AyCode.Core\AyCode.Services.Server\bin\Debug\net8.0\AyCode.Utils.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,12 @@
|
||||||
<Reference Include="AyCode.Interfaces">
|
<Reference Include="AyCode.Interfaces">
|
||||||
<HintPath>..\..\..\AyCode.Core\AyCode.Services.Server\bin\Debug\net8.0\AyCode.Interfaces.dll</HintPath>
|
<HintPath>..\..\..\AyCode.Core\AyCode.Services.Server\bin\Debug\net8.0\AyCode.Interfaces.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
|
<Reference Include="AyCode.Models">
|
||||||
|
<HintPath>..\..\..\AyCode.Core\AyCode.Services.Server\bin\Debug\net8.0\AyCode.Models.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="AyCode.Models.Server">
|
||||||
|
<HintPath>..\..\..\AyCode.Core\AyCode.Services.Server\bin\Debug\net8.0\AyCode.Models.Server.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,79 @@
|
||||||
|
using AyCode.Interfaces.Messages;
|
||||||
|
using SendGrid;
|
||||||
|
using SendGrid.Helpers.Mail;
|
||||||
|
using AyCode.Models.Enums;
|
||||||
|
using AyCode.Entities.Messages;
|
||||||
|
using AyCode.Models.Messages;
|
||||||
|
using TIAM.Database.DataLayers.Users;
|
||||||
|
|
||||||
|
|
||||||
|
namespace TIAMWebApp.Server.Controllers
|
||||||
|
{
|
||||||
|
public class MessageService : IMessageSenderService
|
||||||
|
{
|
||||||
|
private readonly HttpClient http;
|
||||||
|
private readonly IConfiguration _configuration;
|
||||||
|
public UserDal _userDal;
|
||||||
|
|
||||||
|
public MessageService(HttpClient http, IConfiguration configuration, UserDal userDal)
|
||||||
|
{
|
||||||
|
this.http = http;
|
||||||
|
_configuration = configuration;
|
||||||
|
_userDal = userDal;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> SendMessageAsync(IMessageBase message, int[] messageTypes)
|
||||||
|
{
|
||||||
|
foreach (var messageType in messageTypes)
|
||||||
|
{
|
||||||
|
switch (messageType)
|
||||||
|
{
|
||||||
|
case (int)MessageTypesEnum.email:
|
||||||
|
await SendMailWithSendgrid(((EMailMessage)message));
|
||||||
|
break;
|
||||||
|
case (int)MessageTypesEnum.sms:
|
||||||
|
//await SendSmsWithTwilio(message.Message);
|
||||||
|
break;
|
||||||
|
case (int)MessageTypesEnum.push:
|
||||||
|
//await SendPushWithFirebase(message.Message);
|
||||||
|
break;
|
||||||
|
case (int)MessageTypesEnum.chat:
|
||||||
|
//await SendChatWithSignalR(message.Message);
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//return true here
|
||||||
|
return await Task.FromResult(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<string> SendMailWithSendgrid(EMailMessage message)
|
||||||
|
{
|
||||||
|
//resolve user!!!
|
||||||
|
var senderUser = _userDal.Ctx.Users.FirstOrDefault(x => x.Id == message.SenderId);
|
||||||
|
var receiverUser = _userDal.Ctx.Users.FirstOrDefault(x => x.Id == message.ReceiverId);
|
||||||
|
string apiKey = _configuration["SendGrid:Key"];
|
||||||
|
var _client = new SendGridClient(apiKey);
|
||||||
|
var _from = new EmailAddress("", "");
|
||||||
|
if(message.SenderId == null)
|
||||||
|
{
|
||||||
|
|
||||||
|
_from = new EmailAddress("noreply@tiam.com", "TourIAm mailservice");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_from = new EmailAddress(senderUser.Email, senderUser.Email);
|
||||||
|
}
|
||||||
|
var _subject = message.Subject;
|
||||||
|
var _to = new EmailAddress(receiverUser.Email, receiverUser.Email);
|
||||||
|
var _plainTextContent = message.Message;
|
||||||
|
var _htmlContent = message.HtmlContent;
|
||||||
|
var _msg = MailHelper.CreateSingleEmail(_from, _to, message.Subject, _plainTextContent, _htmlContent);
|
||||||
|
var response = await _client.SendEmailAsync(_msg).ConfigureAwait(false);
|
||||||
|
return response.StatusCode.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -49,6 +49,12 @@
|
||||||
<Reference Include="AyCode.Interfaces.Server">
|
<Reference Include="AyCode.Interfaces.Server">
|
||||||
<HintPath>..\..\..\AyCode.Core\AyCode.Services.Server\bin\Debug\net8.0\AyCode.Interfaces.Server.dll</HintPath>
|
<HintPath>..\..\..\AyCode.Core\AyCode.Services.Server\bin\Debug\net8.0\AyCode.Interfaces.Server.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
|
<Reference Include="AyCode.Models">
|
||||||
|
<HintPath>..\..\..\AyCode.Core\AyCode.Services.Server\bin\Debug\net8.0\AyCode.Models.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="AyCode.Models.Server">
|
||||||
|
<HintPath>..\..\..\AyCode.Core\AyCode.Services.Server\bin\Debug\net8.0\AyCode.Models.Server.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
<Reference Include="AyCode.Utils">
|
<Reference Include="AyCode.Utils">
|
||||||
<HintPath>..\..\..\AyCode.Core\AyCode.Utils\bin\Debug\net8.0\AyCode.Utils.dll</HintPath>
|
<HintPath>..\..\..\AyCode.Core\AyCode.Utils\bin\Debug\net8.0\AyCode.Utils.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
|
|
|
||||||
|
|
@ -9,5 +9,8 @@
|
||||||
"Key": "Cee4400-rDMFkVvHPufyLDSzbfu2grgRhpepos299IhTLOXsljkcpt3yUR4RRjPQ",
|
"Key": "Cee4400-rDMFkVvHPufyLDSzbfu2grgRhpepos299IhTLOXsljkcpt3yUR4RRjPQ",
|
||||||
"Issuer": "https://localhost:7116",
|
"Issuer": "https://localhost:7116",
|
||||||
"Audience": "http://localhost:7116"
|
"Audience": "http://localhost:7116"
|
||||||
|
},
|
||||||
|
"SendGrid": {
|
||||||
|
"Key": ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,5 +13,9 @@
|
||||||
"Key": "Cee4400-rDMFkVvHPufyLDSzbfu2grgRhpepos299IhTLOXsljkcpt3yUR4RRjPQ",
|
"Key": "Cee4400-rDMFkVvHPufyLDSzbfu2grgRhpepos299IhTLOXsljkcpt3yUR4RRjPQ",
|
||||||
"Issuer": "http://localhost:5000",
|
"Issuer": "http://localhost:5000",
|
||||||
"Audience": "http://localhost:5000"
|
"Audience": "http://localhost:5000"
|
||||||
|
},
|
||||||
|
"SendGrid": {
|
||||||
|
"Key": ""
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue