initial commit from an older template

This commit is contained in:
2025-09-20 22:33:35 +03:00
commit b6778046c2
134 changed files with 6657 additions and 0 deletions

38
Utils/Clients/EmailClient.cs Executable file
View 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
}

View 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
}