Files
DigitalData.MessagingService/src/core/DigitalData.MessagingService.Application/EmailAccount/Queries/GetSenderQuery.cs
TekH 4dd0974c6f Add multi-targeting support for .NET frameworks
Introduced conditional compilation using `#if NET` directives to
enable support for multiple frameworks (`net462`, `net480`, and
`net8.0`). Updated the project file to support multi-targeting
and added framework-specific dependencies conditionally.

Refactored namespaces, interfaces, classes, validators, and
handlers to ensure compatibility with targeted frameworks.
Enhanced dependency injection setup and adjusted logic in
`GetSenderQueryHandler` for framework-specific behavior.

Ensured consistency and maintainability by wrapping framework-
specific code blocks with `#if NET` directives.
2026-08-12 14:40:47 +02:00

41 lines
1.3 KiB
C#

#if NET
using DigitalData.MessagingService.Application.Common.Options;
using DigitalData.MessagingService.Abstraction;
using MediatR;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace DigitalData.MessagingService.Application.EmailAccount.Queries;
public record GetSenderQuery : IRequest<EmailAccountDto?>
{
public int? Id { get; init; }
public string? Username { get; init; }
}
/// <summary>
///
/// </summary>
/// <param name="Options"></param>
/// <param name="Logger"></param>
public class GetSenderQueryHandler(IOptions<EmailAccountsOptions> Options, ILogger<GetSenderQueryHandler> Logger) : IRequestHandler<GetSenderQuery, EmailAccountDto?>
{
public Task<EmailAccountDto?> Handle(GetSenderQuery request, CancellationToken cancellationToken)
{
var accounts = request.Id is not null
? Options.Value.Accounts.Where(a => a.Id == request.Id)
: Options.Value.Accounts.Where(a => a.Username == request.Username);
if(accounts.Count() > 1)
{
Logger.LogWarning(
"Multiple email accounts found for the given criteria ({Criteria}). Returning the first one.",
request.Id is not null ? $"Id: {request.Id}" : $"Username: {request.Username}"
);
}
return Task.FromResult(accounts.FirstOrDefault());
}
}
#endif