Files
2024-01-25 04:51:13 +03:30

118 lines
3.5 KiB
C#

using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using xCommons.Extensions;
using xExceptions.Constants;
using xMessageService.Interfaces;
using xMessageService.Models;
namespace xMessageService.Providers {
public partial class XMessageProvider : IXMessageProvider {
private readonly XMessageConfiguration configuration;
private readonly ILogger<XMessageProvider> logger;
private readonly IXSmsService smsService;
private readonly IXMailService mailService;
public XMessageProvider (
XMessageConfiguration configuration,
ILogger<XMessageProvider> logger,
IXSmsService smsService,
IXMailService mailService
) {
this.configuration = configuration;
this.logger = logger;
this.smsService = smsService;
this.mailService = mailService;
}
/// <summary>
/// Send a Mail Message
/// </summary>
/// <param name="message"></param>
/// <param name="provider"></param>
/// <param name="throwException"></param>
/// <param name="exception"></param>
/// <returns></returns>
public async Task SendMailAsync (
XMessage message,
string provider = null,
bool throwException = true,
Exception exception = null
) {
//
if (exception.IsNull ()) {
exception = XException.ActionFailed.ToException ();
}
//
try {
await mailService.SendMailAsync (message, provider);
} catch (Exception ex) {
//
logger.LogError ($"Exeption: {ex.Message}");
//
if (throwException) {
throw exception;
}
}
}
/// <summary>
/// Send SMS Message
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public async Task SendSmsAsync (
XMessage message,
string provider = null,
bool throwException = true,
Exception exception = null
) {
//
if (exception.IsNull ()) {
exception = XException.ActionFailed.ToException ();
}
//
try {
await smsService.SendSmsAsync (message, provider);
} catch {
if (throwException) {
throw exception;
}
}
}
/// <summary>
/// Check Message Provider is Ready or not
/// </summary>
/// <param name="checkMailService"></param>
/// <param name="checkSmsService"></param>
/// <returns></returns>
public bool IsReady (
bool? checkMailService = false,
bool? checkSmsService = false
) {
//
var result = true;
//
// Check Mail Service ...
if (checkMailService.HasValue &&
checkMailService.GetValueOrDefault ()) {
result = result && mailService != null;
}
//
// Check Sms Service ...
if (checkSmsService.HasValue &&
checkSmsService.GetValueOrDefault ()) {
result = result && smsService != null;
}
//
return result;
}
}
}