Refactor ReadEmailQuery to return structured response
Updated ReadEmailQuery to return a new response type, ReadEmailQueryResponse, which includes both email data and metadata (e.g., LastSync). Added the Account property to ReadEmailQuery for specifying the email account. Refactored ReadEmailQueryHandler to: - Use IImapEmailService to fetch LastSync metadata. - Return ReadEmailQueryResponse instead of a collection of ReceivedEmailDto. Introduced ReadEmailQueryResponse class to encapsulate LastSync and Emails properties. Updated EmailController to handle the new response structure, ensuring compatibility with the updated ReadEmailQuery and its handler.
This commit is contained in:
@@ -15,7 +15,7 @@ namespace DigitalData.MessagingService.Application.EmailReceiving.Queries;
|
||||
/// <summary>
|
||||
/// Query to fetch emails from an IMAP mailbox.
|
||||
/// </summary>
|
||||
public record ReadEmailQuery : IRequest<IEnumerable<ReceivedEmailDto>>
|
||||
public record ReadEmailQuery : IRequest<ReadEmailQueryResponse>
|
||||
{
|
||||
/// <summary>
|
||||
/// Identifies the email account to use.
|
||||
@@ -28,9 +28,9 @@ public record ReadEmailQuery : IRequest<IEnumerable<ReceivedEmailDto>>
|
||||
public MailSearchFilter Mail { get; init; } = new();
|
||||
}
|
||||
|
||||
public class ReadEmailQueryHandler(IMapper Mapper, ILogger<ReadEmailQueryHandler> Logger, IRepository<EmailAccount> EmailAccountRepo, IReceivedEmailRepository MailRepo) : IRequestHandler<ReadEmailQuery, IEnumerable<ReceivedEmailDto>>
|
||||
public class ReadEmailQueryHandler(IMapper Mapper, ILogger<ReadEmailQueryHandler> Logger, IRepository<EmailAccount> EmailAccountRepo, IReceivedEmailRepository MailRepo, IImapEmailService imapEmailService) : IRequestHandler<ReadEmailQuery, ReadEmailQueryResponse>
|
||||
{
|
||||
public async Task<IEnumerable<ReceivedEmailDto>> Handle(ReadEmailQuery request, CancellationToken cancellationToken)
|
||||
public async Task<ReadEmailQueryResponse> Handle(ReadEmailQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var accounts = await EmailAccountRepo.FindAsync(request.Account.Id is int id ? x => x.Id == id : x => x.Username == request.Account.Username, cancellationToken: cancellationToken);
|
||||
|
||||
@@ -45,7 +45,14 @@ public class ReadEmailQueryHandler(IMapper Mapper, ILogger<ReadEmailQueryHandler
|
||||
$"IMAP is not configured for account '{account.Username}' (Id: {account.Id}). Set ImapServer in EmailAccounts configuration.");
|
||||
|
||||
var mails = await MailRepo.FindAsync(request.Mail, account, cancellationToken);
|
||||
return Mapper.Map<IEnumerable<ReceivedEmailDto>>(mails);
|
||||
|
||||
var lastSync = imapEmailService.GetLastImapSyncDate(account.Id, request.Mail.Folder);
|
||||
|
||||
return new ReadEmailQueryResponse
|
||||
{
|
||||
LastSync = lastSync,
|
||||
Emails = Mapper.Map<IEnumerable<ReceivedEmailDto>>(mails)
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,17 @@
|
||||
#if NET
|
||||
using DigitalData.MessagingService.Application.Common.Dto;
|
||||
|
||||
namespace DigitalData.MessagingService.Application.EmailReceiving.Queries;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="Emails"></param>
|
||||
/// <param name="LastSync"></param>
|
||||
public class ReadEmailQueryResponse
|
||||
{
|
||||
public DateTime? LastSync { get; init; } = null;
|
||||
|
||||
public IEnumerable<ReceivedEmailDto> Emails { get; init; } = [];
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user