Add FetchEmailByUid endpoint and improve middleware

Added a new endpoint to `EmailController` for fetching a single email by UID, including route and query parameter handling, and appropriate Swagger annotations.

Refactored `ExceptionHandlingMiddleware` to use constructor injection, simplified exception handling with a `switch` expression, and improved logging and response formatting. Added support for `ValidationException`.

Updated `FormatValidationErrors` to handle `ValidationException` and improved error formatting.

Made minor documentation updates in `EmailController` and performed general code cleanup for readability and modernization.
This commit is contained in:
2026-08-11 09:48:22 +02:00
parent 59e8780345
commit 1162a07454
2 changed files with 74 additions and 61 deletions

View File

@@ -72,6 +72,7 @@ public class EmailController(IMediator mediator) : ControllerBase
/// Fetch emails from an IMAP mailbox.
/// </summary>
/// <param name="query">Query parameters for filtering and fetching emails from the IMAP mailbox.</param>
/// <param name="firstHtmlBodyOnly"></param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>HTTP 200 with list of received emails.</returns>
[HttpGet]
@@ -91,6 +92,28 @@ public class EmailController(IMediator mediator) : ControllerBase
return Ok(emails);
}
/// <summary>
/// Fetch a single email by its UID from an IMAP mailbox.
/// </summary>
/// <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="cancellationToken">Cancellation token.</param>
/// <returns>HTTP 200 with the matched email, or HTTP 404 if not found.</returns>
[HttpGet("{uid:long}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> FetchEmailByUid([FromRoute] long uid, [FromQuery] FetchEmailByUidQuery query, CancellationToken cancellationToken = default)
{
var queryWithUid = query.WithUid(uid);
var email = await mediator.Send(queryWithUid, cancellationToken);
if (email is null)
return NotFound($"No email found with UID {uid}.");
return Ok(email);
}
/// <summary>
/// Mark a single IMAP message as seen (read).
/// </summary>