initial commit from an older template
This commit is contained in:
38
Utils/Clients/EmailClient.cs
Executable file
38
Utils/Clients/EmailClient.cs
Executable file
@@ -0,0 +1,38 @@
|
||||
using System.Net.Mail;
|
||||
using GamificationService.Exceptions.UtilServices.Email;
|
||||
|
||||
namespace GamificationService.Utils;
|
||||
|
||||
public class EmailClient(SmtpClient smtpClient, string emailFrom, ILogger<EmailClient> logger)
|
||||
{
|
||||
#region Fields
|
||||
|
||||
private readonly string _emailFrom = emailFrom;
|
||||
private readonly ILogger<EmailClient> _logger = logger;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <summary>
|
||||
/// Sends the email using the SmtpClient instance.
|
||||
/// </summary>
|
||||
/// <param name="email">Email to send.</param>
|
||||
/// <exception cref="SendEmailException">If the SmtpClient instance fails to send the email.</exception>
|
||||
/// <returns>Task that represents the asynchronous operation.</returns>
|
||||
public async Task SendEmail(MailMessage email, string emailTo)
|
||||
{
|
||||
try
|
||||
{
|
||||
email.To.Add(new MailAddress(emailTo));
|
||||
await smtpClient.SendMailAsync(email);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
throw new SendEmailException("Failed to send email", ex);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
150
Utils/Clients/PushNotificationsClient.cs
Executable file
150
Utils/Clients/PushNotificationsClient.cs
Executable file
@@ -0,0 +1,150 @@
|
||||
using System.Net;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using GamificationService.Exceptions.UtilServices.Api;
|
||||
|
||||
namespace GamificationService.Utils;
|
||||
|
||||
public class PushNotificationsClient
|
||||
{
|
||||
#region Fields
|
||||
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly ILogger<PushNotificationsClient> _logger;
|
||||
private readonly string _applicationToken;
|
||||
private readonly string _projectId;
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
public PushNotificationsClient(HttpClient httpClient, ILogger<PushNotificationsClient> logger, string applicationToken, string projectId)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
_logger = logger;
|
||||
_applicationToken = applicationToken;
|
||||
_projectId = projectId;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
public async Task SendPushNotification(PushNotification pushNotification)
|
||||
{
|
||||
try
|
||||
{
|
||||
var payload = new
|
||||
{
|
||||
message = new
|
||||
{
|
||||
token = _applicationToken,
|
||||
notification = new
|
||||
{
|
||||
body = pushNotification.Message,
|
||||
title = pushNotification.Title,
|
||||
image = pushNotification.Image
|
||||
},
|
||||
android = new
|
||||
{
|
||||
notification = new
|
||||
{
|
||||
body = pushNotification.Message,
|
||||
title = pushNotification.Title,
|
||||
image = pushNotification.Image,
|
||||
click_action = pushNotification.ClickAction,
|
||||
click_action_type = pushNotification.ClickActionType
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var jsonPayload = JsonSerializer.Serialize(payload);
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Post,$"/{_projectId}/messages")
|
||||
{
|
||||
Content = new StringContent(jsonPayload, Encoding.UTF8, "application/json")
|
||||
};
|
||||
|
||||
var response = await _httpClient.SendAsync(request);
|
||||
|
||||
if (response.StatusCode == HttpStatusCode.BadRequest)
|
||||
{
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
_logger.LogError($"Failed to send push notification. Status Code: {response.StatusCode}, Response: {responseContent}");
|
||||
throw new BadRequestException($"Failed to send push notification: {response.StatusCode}");
|
||||
}
|
||||
else if (response.StatusCode == HttpStatusCode.Forbidden)
|
||||
{
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
_logger.LogError($"Failed to send push notification: {response.StatusCode}, Response: {responseContent}");
|
||||
throw new ForbiddenException($"Failed to send push notification: {response.StatusCode}");
|
||||
}
|
||||
else
|
||||
{
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
_logger.LogError($"Failed to send push notification: {response.StatusCode}, Response: {responseContent}");
|
||||
throw new Exception($"Failed to send push notification: {response.StatusCode}");
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, e.Message);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SendPushNotification(PushNotification pushNotification, string topic)
|
||||
{
|
||||
try
|
||||
{
|
||||
var payload = new
|
||||
{
|
||||
message = new
|
||||
{
|
||||
notification = new
|
||||
{
|
||||
body = pushNotification.Message,
|
||||
title = pushNotification.Title,
|
||||
image = pushNotification.Image
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var jsonPayload = JsonSerializer.Serialize(payload);
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Post,$"/{_projectId}/topics/{topic}/publish")
|
||||
{
|
||||
Content = new StringContent(jsonPayload, Encoding.UTF8, "application/json")
|
||||
};
|
||||
|
||||
var response = await _httpClient.SendAsync(request);
|
||||
|
||||
if (response.StatusCode == HttpStatusCode.BadRequest)
|
||||
{
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
_logger.LogError($"Failed to send push notification. Status Code: {response.StatusCode}, Response: {responseContent}");
|
||||
throw new BadRequestException($"Failed to send push notification: {response.StatusCode}");
|
||||
}
|
||||
else if (response.StatusCode == HttpStatusCode.Forbidden)
|
||||
{
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
_logger.LogError($"Failed to send push notification: {response.StatusCode}, Response: {responseContent}");
|
||||
throw new ForbiddenException($"Failed to send push notification: {response.StatusCode}");
|
||||
}
|
||||
else
|
||||
{
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
_logger.LogError($"Failed to send push notification: {response.StatusCode}, Response: {responseContent}");
|
||||
throw new Exception($"Failed to send push notification: {response.StatusCode}");
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, e.Message);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
6
Utils/Enums/ClickActionType.cs
Executable file
6
Utils/Enums/ClickActionType.cs
Executable file
@@ -0,0 +1,6 @@
|
||||
namespace GamificationService.Utils;
|
||||
|
||||
public enum ClickActionType
|
||||
{
|
||||
NONE
|
||||
}
|
||||
7
Utils/Enums/Gender.cs
Normal file
7
Utils/Enums/Gender.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace GamificationService.Utils.Enums;
|
||||
|
||||
public enum Gender
|
||||
{
|
||||
Male,
|
||||
Female
|
||||
}
|
||||
8
Utils/Enums/InstructionTestScoreCalcMethod.cs
Normal file
8
Utils/Enums/InstructionTestScoreCalcMethod.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace GamificationService.Utils.Enums
|
||||
{
|
||||
public enum InstructionTestScoreCalcMethod
|
||||
{
|
||||
AverageGrade,
|
||||
MaxGrade
|
||||
}
|
||||
}
|
||||
9
Utils/Enums/NotificationInformationType.cs
Executable file
9
Utils/Enums/NotificationInformationType.cs
Executable file
@@ -0,0 +1,9 @@
|
||||
namespace GamificationService.Utils;
|
||||
|
||||
public enum NotificationInformationType
|
||||
{
|
||||
AUTH,
|
||||
INFO,
|
||||
WARNING,
|
||||
ERROR
|
||||
}
|
||||
21
Utils/Factory/MailNotificationsFactory.cs
Executable file
21
Utils/Factory/MailNotificationsFactory.cs
Executable file
@@ -0,0 +1,21 @@
|
||||
using System.Net.Mail;
|
||||
|
||||
namespace GamificationService.Utils.Factory;
|
||||
|
||||
public class MailNotificationsFactory
|
||||
{
|
||||
public static Notification CreateNotification(NotificationInformationType type,
|
||||
string title,
|
||||
string message,List<Attachment> attachments)
|
||||
{
|
||||
return new MailNotification(type, title, message, attachments);
|
||||
}
|
||||
public Notification CreateNotification(NotificationInformationType type,
|
||||
string title,
|
||||
string message)
|
||||
{
|
||||
|
||||
return new MailNotification(type, title, message);
|
||||
|
||||
}
|
||||
}
|
||||
28
Utils/Factory/PushNotificationsFactory.cs
Executable file
28
Utils/Factory/PushNotificationsFactory.cs
Executable file
@@ -0,0 +1,28 @@
|
||||
namespace GamificationService.Utils.Factory;
|
||||
|
||||
public class PushNotificationsFactory
|
||||
{
|
||||
public Notification CreateNotification(NotificationInformationType type,
|
||||
string title,
|
||||
string message)
|
||||
{
|
||||
return new PushNotification(type, title, message);
|
||||
}
|
||||
|
||||
public Notification CreateNotification(NotificationInformationType type,
|
||||
string title,
|
||||
string message,
|
||||
string image)
|
||||
{
|
||||
return new PushNotification(type, title, message, image);
|
||||
}
|
||||
public Notification CreateNotification(NotificationInformationType type,
|
||||
string title,
|
||||
string message,
|
||||
string? image,
|
||||
string clickAction,
|
||||
ClickActionType clickActionType)
|
||||
{
|
||||
return new PushNotification(type, title, message, image, clickAction, clickActionType);
|
||||
}
|
||||
}
|
||||
111
Utils/MailNotification.cs
Executable file
111
Utils/MailNotification.cs
Executable file
@@ -0,0 +1,111 @@
|
||||
using System.Net.Mail;
|
||||
|
||||
namespace GamificationService.Utils;
|
||||
|
||||
public class MailNotification : Notification
|
||||
{
|
||||
#region Fields
|
||||
|
||||
private List<Attachment> _attachments;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
public List<Attachment> Attachments { get => _attachments; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
public MailNotification(NotificationInformationType type, string title, string message, List<Attachment> attachments) : base(type, title, message)
|
||||
{
|
||||
_attachments = attachments;
|
||||
}
|
||||
public MailNotification(NotificationInformationType type, string title, string message) : base(type, title, message)
|
||||
{
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
public MailMessage ConvertToMailMessage()
|
||||
{
|
||||
var mailMessage = new MailMessage
|
||||
{
|
||||
Subject = CreateTitle(),
|
||||
Body = CreateBody(),
|
||||
IsBodyHtml = true,
|
||||
Priority = GetPriority()
|
||||
};
|
||||
if (_attachments != null)
|
||||
{
|
||||
mailMessage.Attachments.ToList().AddRange(_attachments);
|
||||
}
|
||||
return mailMessage;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
private string CreateTitle()
|
||||
{
|
||||
switch (Type)
|
||||
{
|
||||
case NotificationInformationType.AUTH:
|
||||
return "Авторизация " + Title;
|
||||
case NotificationInformationType.INFO:
|
||||
return "Информация "+ Title;
|
||||
case NotificationInformationType.WARNING:
|
||||
return "Предупреждение "+ Title;
|
||||
case NotificationInformationType.ERROR:
|
||||
return "Ошибка "+ Title;
|
||||
default:
|
||||
return "Информация "+ Title;
|
||||
}
|
||||
}
|
||||
|
||||
private string CreateBody()
|
||||
{
|
||||
string formattedMessage;
|
||||
|
||||
switch (Type)
|
||||
{
|
||||
case NotificationInformationType.AUTH:
|
||||
formattedMessage = "Вы успешно авторизовались.";
|
||||
break;
|
||||
case NotificationInformationType.INFO:
|
||||
formattedMessage = "Это информационное сообщение.";
|
||||
break;
|
||||
case NotificationInformationType.WARNING:
|
||||
formattedMessage = "Внимание! Обратите внимание на это предупреждение.";
|
||||
break;
|
||||
case NotificationInformationType.ERROR:
|
||||
formattedMessage = "Произошла ошибка. Пожалуйста, проверьте детали.";
|
||||
break;
|
||||
default:
|
||||
formattedMessage = "Сообщение не определено.";
|
||||
break;
|
||||
}
|
||||
|
||||
return $"<p style='font-size: 16px; line-height: 1.5; color: #555;'>{formattedMessage} {Message}</p>";
|
||||
}
|
||||
|
||||
private MailPriority GetPriority()
|
||||
{
|
||||
switch (Type)
|
||||
{
|
||||
case NotificationInformationType.AUTH:
|
||||
return MailPriority.High;
|
||||
case NotificationInformationType.INFO:
|
||||
return MailPriority.Normal;
|
||||
case NotificationInformationType.WARNING:
|
||||
return MailPriority.Low;
|
||||
case NotificationInformationType.ERROR:
|
||||
return MailPriority.High;
|
||||
default:
|
||||
return MailPriority.Normal;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
33
Utils/Notification.cs
Executable file
33
Utils/Notification.cs
Executable file
@@ -0,0 +1,33 @@
|
||||
using System.Net.Mail;
|
||||
|
||||
namespace GamificationService.Utils;
|
||||
|
||||
public abstract class Notification
|
||||
{
|
||||
#region Fields
|
||||
|
||||
private string _title;
|
||||
private string _message;
|
||||
private NotificationInformationType _type;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Parameters
|
||||
|
||||
public string Title { get => _title;}
|
||||
public string Message { get => _message; }
|
||||
public NotificationInformationType Type { get => _type; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
public Notification(NotificationInformationType type, string title, string message)
|
||||
{
|
||||
_type = type;
|
||||
_title = title;
|
||||
_message = message;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
40
Utils/PushNotification.cs
Executable file
40
Utils/PushNotification.cs
Executable file
@@ -0,0 +1,40 @@
|
||||
namespace GamificationService.Utils;
|
||||
|
||||
public class PushNotification : Notification
|
||||
{
|
||||
#region Fields
|
||||
|
||||
private readonly string? _image;
|
||||
private readonly string? _clickAction;
|
||||
private readonly ClickActionType? _clickActionType;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
|
||||
public string? Image { get => _image; }
|
||||
public string? ClickAction { get => _clickAction; }
|
||||
public int? ClickActionType { get => (int)_clickActionType; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
public PushNotification(NotificationInformationType type, string title, string message, string image, string clickAction, ClickActionType clickActionType) : base(type, title, message)
|
||||
{
|
||||
_image = image;
|
||||
_clickAction = clickAction;
|
||||
_clickActionType = clickActionType;
|
||||
}
|
||||
|
||||
public PushNotification(NotificationInformationType type, string title, string message, string? image) : base(type, title, message)
|
||||
{
|
||||
_image = image;
|
||||
}
|
||||
|
||||
public PushNotification(NotificationInformationType type, string title, string message) : base(type, title, message)
|
||||
{
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
10
Utils/TwoFactorProvider.cs
Normal file
10
Utils/TwoFactorProvider.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace GamificationService.Utils;
|
||||
|
||||
public enum TwoFactorProvider
|
||||
{
|
||||
NONE,
|
||||
EMAIL,
|
||||
PHONE,
|
||||
PUSH,
|
||||
AUTHENTICATOR
|
||||
}
|
||||
Reference in New Issue
Block a user