Refactor: Replace EmailAttachmentContext with DTO
Replaced `EmailAttachmentContext` with `EmailAttachmentDto` across the codebase to align with the updated DTO naming convention. - Renamed `EmailAttachmentContext` to `EmailAttachmentDto`. - Updated property types, method signatures, and return types to use `EmailAttachmentDto`. - Modified XML documentation references to reflect the new class name. - Updated `ReceivedEmailContext` to `ReceivedEmailDto` and adjusted related methods and properties. - Refactored `FetchEmailsAsync` methods and handlers to use `ReceivedEmailDto`. - Adjusted `BuildAttachmentsAsync` and attachment handling logic in `EmailController` and `LimilabsImapEmailService`. This refactor ensures consistency and improves code clarity while maintaining functionality.
This commit is contained in:
@@ -3,7 +3,7 @@ namespace DigitalData.MessagingService.Abstraction;
|
||||
/// <summary>
|
||||
/// Represents a single email attachment.
|
||||
/// </summary>
|
||||
public sealed class EmailAttachmentContext
|
||||
public sealed class EmailAttachmentDto
|
||||
{
|
||||
/// <summary>
|
||||
/// Display name of the attachment (e.g. "invoice.pdf").
|
||||
@@ -48,8 +48,8 @@ public record EmailContext
|
||||
|
||||
/// <summary>
|
||||
/// Optional list of attachments to include with the email.
|
||||
/// Each entry may carry its content as a byte array (<see cref="EmailAttachmentContext.Content"/>)
|
||||
/// or reference a file on disk via <see cref="EmailAttachmentContext.FilePath"/>.
|
||||
/// Each entry may carry its content as a byte array (<see cref="EmailAttachmentDto.Content"/>)
|
||||
/// or reference a file on disk via <see cref="EmailAttachmentDto.FilePath"/>.
|
||||
/// </summary>
|
||||
public IEnumerable<EmailAttachmentContext> Attachments { get; set; } = [];
|
||||
public IEnumerable<EmailAttachmentDto> Attachments { get; set; } = [];
|
||||
}
|
||||
@@ -3,7 +3,7 @@ namespace DigitalData.MessagingService.Abstraction;
|
||||
/// <summary>
|
||||
/// Represents an email message received via IMAP.
|
||||
/// </summary>
|
||||
public sealed record ReceivedEmailContext
|
||||
public sealed record ReceivedEmailDto
|
||||
{
|
||||
/// <summary>
|
||||
/// Unique identifier of the message on the IMAP server (UID).
|
||||
@@ -81,9 +81,9 @@ public sealed record ReceivedEmailContext
|
||||
/// Attachments included with this message.
|
||||
/// </summary>
|
||||
#if NET
|
||||
public IEnumerable<EmailAttachmentContext> Attachments { get; init; } = [];
|
||||
public IEnumerable<EmailAttachmentDto> Attachments { get; init; } = [];
|
||||
#else
|
||||
public IEnumerable<EmailAttachmentContext> Attachments { get; set; } = [];
|
||||
public IEnumerable<EmailAttachmentDto> Attachments { get; set; } = [];
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
@@ -17,7 +17,7 @@ public interface IImapEmailService
|
||||
/// Set to <see langword="false"/> for a non-destructive read (uses <c>BODY.PEEK</c> internally).
|
||||
/// </param>
|
||||
/// <param name="cancellationToken">Cancellation token.</param>
|
||||
Task<IEnumerable<ReceivedEmailContext>> FetchEmailsAsync(
|
||||
Task<IEnumerable<ReceivedEmailDto>> FetchEmailsAsync(
|
||||
EmailAccountDto account,
|
||||
MailSearchFilter filter,
|
||||
CancellationToken cancellationToken = default);
|
||||
|
||||
@@ -10,7 +10,7 @@ namespace DigitalData.MessagingService.Application.EmailReceiving.Queries;
|
||||
/// <summary>
|
||||
/// Query to fetch emails from an IMAP mailbox.
|
||||
/// </summary>
|
||||
public record FetchEmailsQuery : IRequest<IEnumerable<ReceivedEmailContext>>
|
||||
public record FetchEmailsQuery : IRequest<IEnumerable<ReceivedEmailDto>>
|
||||
{
|
||||
/// <summary>
|
||||
/// Identifies the email account to use.
|
||||
@@ -25,9 +25,9 @@ public record FetchEmailsQuery : IRequest<IEnumerable<ReceivedEmailContext>>
|
||||
|
||||
public class FetchEmailsQueryHandler(
|
||||
ISender Sender,
|
||||
IImapEmailService ImapService) : IRequestHandler<FetchEmailsQuery, IEnumerable<ReceivedEmailContext>>
|
||||
IImapEmailService ImapService) : IRequestHandler<FetchEmailsQuery, IEnumerable<ReceivedEmailDto>>
|
||||
{
|
||||
public async Task<IEnumerable<ReceivedEmailContext>> Handle(FetchEmailsQuery request, CancellationToken cancellationToken)
|
||||
public async Task<IEnumerable<ReceivedEmailDto>> Handle(FetchEmailsQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var account = await Sender.Send(request.Account, cancellationToken)
|
||||
?? throw new NotFoundException(
|
||||
|
||||
@@ -35,13 +35,13 @@ public record PublishEmailCommand : IRequest<Guid>
|
||||
public bool IsHtml { get; init; } = true;
|
||||
|
||||
[JsonIgnore]
|
||||
internal IEnumerable<EmailAttachmentContext> Attachments { get; private init; } = [];
|
||||
internal IEnumerable<EmailAttachmentDto> Attachments { get; private init; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// Returns a new command instance with the supplied attachments.
|
||||
/// Called by the controller after resolving uploaded files.
|
||||
/// </summary>
|
||||
public PublishEmailCommand WithAttachments(IEnumerable<EmailAttachmentContext> attachments)
|
||||
public PublishEmailCommand WithAttachments(IEnumerable<EmailAttachmentDto> attachments)
|
||||
=> this with { Attachments = attachments };
|
||||
}
|
||||
|
||||
|
||||
@@ -111,7 +111,7 @@ public class LimilabsEmailService(
|
||||
return message.ToString();
|
||||
}
|
||||
|
||||
private static void AddAttachments(MailBuilder builder, IEnumerable<EmailAttachmentContext> attachments)
|
||||
private static void AddAttachments(MailBuilder builder, IEnumerable<EmailAttachmentDto> attachments)
|
||||
{
|
||||
foreach (var attachment in attachments)
|
||||
{
|
||||
|
||||
@@ -28,7 +28,7 @@ public class LimilabsImapEmailService(
|
||||
}
|
||||
|
||||
// Public API
|
||||
public async Task<IEnumerable<ReceivedEmailContext>> FetchEmailsAsync(
|
||||
public async Task<IEnumerable<ReceivedEmailDto>> FetchEmailsAsync(
|
||||
EmailAccountDto account,
|
||||
MailSearchFilter filter,
|
||||
CancellationToken cancellationToken = default)
|
||||
@@ -60,7 +60,7 @@ public class LimilabsImapEmailService(
|
||||
if (uids.Count == 0)
|
||||
return [];
|
||||
|
||||
var results = new List<ReceivedEmailContext>(uids.Count);
|
||||
var results = new List<ReceivedEmailDto>(uids.Count);
|
||||
|
||||
foreach (var uid in uids)
|
||||
{
|
||||
@@ -77,11 +77,11 @@ public class LimilabsImapEmailService(
|
||||
var mail = new MailBuilder().CreateFromEml(eml);
|
||||
var flags = await imap.GetFlagsByUIDAsync(uid, cancellationToken);
|
||||
|
||||
var attachments = new List<EmailAttachmentContext>();
|
||||
var attachments = new List<EmailAttachmentDto>();
|
||||
|
||||
foreach (var att in mail.Attachments)
|
||||
{
|
||||
attachments.Add(new EmailAttachmentContext
|
||||
attachments.Add(new EmailAttachmentDto
|
||||
{
|
||||
FileName = att.FileName ?? "attachment",
|
||||
Content = att.Data,
|
||||
@@ -93,7 +93,7 @@ public class LimilabsImapEmailService(
|
||||
|
||||
foreach (var vis in mail.Visuals)
|
||||
{
|
||||
attachments.Add(new EmailAttachmentContext
|
||||
attachments.Add(new EmailAttachmentDto
|
||||
{
|
||||
FileName = vis.FileName ?? "inline",
|
||||
Content = vis.Data,
|
||||
@@ -103,7 +103,7 @@ public class LimilabsImapEmailService(
|
||||
});
|
||||
}
|
||||
|
||||
return new ReceivedEmailContext
|
||||
return new ReceivedEmailDto
|
||||
{
|
||||
Uid = uid,
|
||||
From = mail.From.FirstOrDefault()?.Address ?? string.Empty,
|
||||
|
||||
@@ -56,21 +56,21 @@ public class EmailController(IMediator mediator) : ControllerBase
|
||||
return Accepted(new { Id = eventId });
|
||||
}
|
||||
|
||||
private static async Task<IEnumerable<EmailAttachmentContext>> BuildAttachmentsAsync(
|
||||
private static async Task<IEnumerable<EmailAttachmentDto>> BuildAttachmentsAsync(
|
||||
IFormFileCollection? files,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (files is null || files.Count == 0)
|
||||
return [];
|
||||
|
||||
var result = new List<EmailAttachmentContext>(files.Count);
|
||||
var result = new List<EmailAttachmentDto>(files.Count);
|
||||
|
||||
foreach (var file in files)
|
||||
{
|
||||
using var ms = new MemoryStream();
|
||||
await file.CopyToAsync(ms, cancellationToken);
|
||||
|
||||
result.Add(new EmailAttachmentContext
|
||||
result.Add(new EmailAttachmentDto
|
||||
{
|
||||
FileName = file.FileName,
|
||||
Content = ms.ToArray(),
|
||||
|
||||
Reference in New Issue
Block a user