diff --git a/src/core/DigitalData.MessagingService.Application/EmailAccounts/Commands/EmailAccountModificationCommand.cs b/src/core/DigitalData.MessagingService.Application/EmailAccounts/Commands/EmailAccountModificationCommand.cs new file mode 100644 index 0000000..ddf78ca --- /dev/null +++ b/src/core/DigitalData.MessagingService.Application/EmailAccounts/Commands/EmailAccountModificationCommand.cs @@ -0,0 +1,36 @@ +#if NET +using DigitalData.MessagingService.Application.Common.Dto.EmailAccounts; +using DigitalData.MessagingService.Application.Common.Interfaces.Repositories; +using DigitalData.MessagingService.Application.Common.ValueObjects; +using DigitalData.MessagingService.Domain.Entities; +using MediatR; +using System.Security.Principal; + +namespace DigitalData.MessagingService.Application.EmailAccounts.Commands; + +/// +/// DTO for a single email account configuration. +/// +public record EmailAccountModificationCommand : IRequest<(EmailAccount, bool)> +{ + public required EmailAccountModificationDto ModifiedAccount { get; init; } + + public required Modification Modification { get; init; } +} + +/// +/// +/// +/// +public class EmailAccountModificationCommandHandler(IRepository Repo) : IRequestHandler +{ + public async Task<(EmailAccount, bool)> Handle(EmailAccountModificationCommand request, CancellationToken cancellationToken) + { + return request.Modification switch + { + Modification.Upsert => await Repo.UpsertAsync(a => a.Username == request.ModifiedAccount.Username, request.ModifiedAccount, cancellationToken), + _ => throw new NotSupportedException($"Modification type '{request.Modification}' is not supported in email account modification."), + }; + } +} +#endif \ No newline at end of file diff --git a/src/core/DigitalData.MessagingService.Application/EmailAccounts/Queries/GetEmailAccountQuery.cs b/src/core/DigitalData.MessagingService.Application/EmailAccounts/Queries/GetEmailAccountQuery.cs new file mode 100644 index 0000000..c18008b --- /dev/null +++ b/src/core/DigitalData.MessagingService.Application/EmailAccounts/Queries/GetEmailAccountQuery.cs @@ -0,0 +1,37 @@ +#if NET +using MediatR; +using DigitalData.MessagingService.Domain.Entities; +using DigitalData.MessagingService.Application.Common.Interfaces.Repositories; +using AutoMapper; +using DigitalData.MessagingService.Domain.Exceptions; +using DigitalData.MessagingService.Application.Common.Dto.EmailAccounts; + +namespace DigitalData.MessagingService.Application.EmailAccounts.Queries; + +public record GetEmailAccountQuery : IRequest> +{ + public int? Id { get; init; } + + public string? Username { get; init; } +} + +/// +/// Handles queries for retrieving email accounts. +/// +/// +/// +public class GetEmailAccountQueryHandler(IRepository Repo, IMapper Mapper) : IRequestHandler> +{ + public async Task> Handle(GetEmailAccountQuery request, CancellationToken cancellationToken) + { + var accounts = request.Id is null && request.Username is null + ? await Repo.GetAllAsync(cancellationToken) + : await Repo.FindAsync(request.Id is int id ? x => x.Id == id : x => x.Username == request.Username, cancellationToken: cancellationToken); + + if (accounts.Any()) + return Mapper.Map>(accounts); + else + throw new NotFoundException($"No email account found for the given criteria ({(request.Id is not null ? $"Id: {request.Id}" : $"Username: {request.Username}")})."); + } +} +#endif \ No newline at end of file diff --git a/src/core/DigitalData.MessagingService.Application/EmailAccounts/Queries/GetSenderQuery.cs b/src/core/DigitalData.MessagingService.Application/EmailAccounts/Queries/GetSenderQuery.cs deleted file mode 100644 index a0e3cf6..0000000 --- a/src/core/DigitalData.MessagingService.Application/EmailAccounts/Queries/GetSenderQuery.cs +++ /dev/null @@ -1,41 +0,0 @@ -#if NET -using DigitalData.MessagingService.Application.Common.Options; -using MediatR; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; -using DigitalData.MessagingService.Domain.Entities; - -namespace DigitalData.MessagingService.Application.EmailAccounts.Queries; - -public record GetSenderQuery : IRequest -{ - public int? Id { get; init; } - - public string? Username { get; init; } -} - -/// -/// -/// -/// -/// -public class GetSenderQueryHandler(IOptions Options, ILogger Logger) : IRequestHandler -{ - public Task 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 \ No newline at end of file diff --git a/src/core/DigitalData.MessagingService.Application/EmailAccounts/Validators/GetSenderQueryValidator.cs b/src/core/DigitalData.MessagingService.Application/EmailAccounts/Validators/GetSenderQueryValidator.cs index 846d9c4..6254fe2 100644 --- a/src/core/DigitalData.MessagingService.Application/EmailAccounts/Validators/GetSenderQueryValidator.cs +++ b/src/core/DigitalData.MessagingService.Application/EmailAccounts/Validators/GetSenderQueryValidator.cs @@ -5,17 +5,13 @@ using FluentValidation; namespace DigitalData.MessagingService.Application.EmailAccounts.Validators; /// -/// Validator for . -/// Either or must be provided, but not both. +/// Validator for . +/// Either or must be provided, but not both. /// -public class GetSenderQueryValidator : AbstractValidator +public class GetSenderQueryValidator : AbstractValidator { public GetSenderQueryValidator() { - RuleFor(x => x) - .Must(x => (x.Id is not null) ^ (x.Username is not null)) - .WithMessage("Either Id or Username must be provided, but not both."); - When(x => x.Username is not null, () => { RuleFor(x => x.Username)