Enhance EmailController with new endpoint and improvements

- Changed HTML body response handling to return Content with
  "text/html" content type for better response clarity.
- Added a new `FetchEmailUids` endpoint to retrieve only email
  UIDs based on query filters, with proper status handling.
- Updated `FetchEmailByUid` to support an optional `htmlBodyOnly`
  parameter, allowing clients to fetch only the HTML body.
- Added XML documentation for new and updated methods.
- Improved response handling and ensured proper HTTP status codes.
This commit is contained in:
2026-08-11 10:03:22 +02:00
parent 1162a07454
commit 962cb52e0b

View File

@@ -87,23 +87,44 @@ public class EmailController(IMediator mediator) : ControllerBase
return NotFound("No emails found matching the specified criteria."); return NotFound("No emails found matching the specified criteria.");
if (firstHtmlBodyOnly && emails.FirstOrDefault()?.HtmlBody is string htmlBody) if (firstHtmlBodyOnly && emails.FirstOrDefault()?.HtmlBody is string htmlBody)
return Ok(htmlBody); return Content(htmlBody, "text/html");
return Ok(emails); return Ok(emails);
} }
/// <summary>
/// Fetch only the UIDs of emails matching the given filter from an IMAP mailbox.
/// </summary>
/// <param name="query">Query parameters for filtering emails.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>HTTP 200 with the list of matching UIDs, or HTTP 404 if none found.</returns>
[HttpGet("uid")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> FetchEmailUids([FromQuery] FetchEmailUidsQuery query, CancellationToken cancellationToken = default)
{
var uids = await mediator.Send(query, cancellationToken);
if (!uids.Any())
return NotFound("No emails found matching the specified criteria.");
return Ok(uids);
}
/// <summary> /// <summary>
/// Fetch a single email by its UID from an IMAP mailbox. /// Fetch a single email by its UID from an IMAP mailbox.
/// </summary> /// </summary>
/// <param name="uid">The unique identifier (UID) of the message.</param> /// <param name="uid">The unique identifier (UID) of the message.</param>
/// <param name="query">Query parameters including account, folder and attachment flag.</param> /// <param name="query">Query parameters including account, folder and attachment flag.</param>
/// <param name="htmlBodyOnly"></param>
/// <param name="cancellationToken">Cancellation token.</param> /// <param name="cancellationToken">Cancellation token.</param>
/// <returns>HTTP 200 with the matched email, or HTTP 404 if not found.</returns> /// <returns>HTTP 200 with the matched email, or HTTP 404 if not found.</returns>
[HttpGet("{uid:long}")] [HttpGet("{uid:long}")]
[ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> FetchEmailByUid([FromRoute] long uid, [FromQuery] FetchEmailByUidQuery query, CancellationToken cancellationToken = default) public async Task<IActionResult> FetchEmailByUid([FromRoute] long uid, [FromQuery] FetchEmailByUidQuery query, [FromQuery] bool htmlBodyOnly = false, CancellationToken cancellationToken = default)
{ {
var queryWithUid = query.WithUid(uid); var queryWithUid = query.WithUid(uid);
var email = await mediator.Send(queryWithUid, cancellationToken); var email = await mediator.Send(queryWithUid, cancellationToken);
@@ -111,6 +132,9 @@ public class EmailController(IMediator mediator) : ControllerBase
if (email is null) if (email is null)
return NotFound($"No email found with UID {uid}."); return NotFound($"No email found with UID {uid}.");
if (htmlBodyOnly)
return Content(email.HtmlBody, "text/html");
return Ok(email); return Ok(email);
} }