TourIAm/TIAMWebApp/Shared/Services/ClientNoticeSenderService.cs

70 lines
2.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TIAM.Entities.Transfers;
using TIAMWebApp.Shared.Application.Interfaces;
using static System.Net.WebRequestMethods;
using TIAMWebApp.Shared.Application.Models.ClientSide;
using TIAMWebApp.Shared.Application.Models;
using System.Net.Http.Json;
//using AyCode.Models.Messages;
using TIAM.Entities.Emails;
using TIAMWebApp.Shared.Application.Utility;
using TIAMWebApp.Shared.Application.Models.ClientSide.Messages;
namespace TIAMWebApp.Shared.Application.Services
{
public class ClientNoticeSenderService : IClientNoticeSenderService
{
private readonly HttpClient http;
public ClientNoticeSenderService(HttpClient http)
{
this.http = http;
}
public async Task<string> SendNoticeAsync<TNotice>(TNotice message, int messageType) where TNotice : TIAM.Entities.Emails.EmailMessage
{
var url = $"{Setting.ApiBaseUrl}/{APIUrls.SendEmail}";
if (message != null)
{
EmailMessage? bleh = new EmailMessage();
bleh.Subject = message.Subject;
bleh.SenderId = message.SenderId;
if(messageType == (int)AyCode.Models.Enums.MessageTypesEnum.email)
{
MessageSenderModel<EmailMessage> messageModel = new EmailMessageSenderModel(bleh, (AyCode.Models.Enums.MessageTypesEnum)messageType);
messageModel.Message = message as EmailMessage;
var response = await http.PostAsJsonAsync(url, messageModel);
if (!response.IsSuccessStatusCode)
return null;
var result = (string)(await response.Content.ReadFromJsonAsync(typeof(string)));
return result;
}
else
{
return "Error sending the message";
}
}
else
{
return "Error sending the message";
}
}
}
}