From 8dc561cb8f5454a143f210816450a4ed67d34a26 Mon Sep 17 00:00:00 2001 From: TekH Date: Sat, 6 Jun 2026 18:07:31 +0200 Subject: [PATCH] Refactor query to include related document data Refactored the `Handle` method in `ReadDocumentQueryHandler` to eagerly load related data (`Elements` and `Annotations`) using `Include` and `ThenInclude`. Introduced a reusable `docQuery` variable to reduce code duplication and ensure consistency in queries for both `query.Id` and `query.EnvelopeId`. No changes were made to the mapping logic or exception handling. --- .../Documents/Queries/ReadDocumentQuery.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/EnvelopeGenerator.Application/Documents/Queries/ReadDocumentQuery.cs b/EnvelopeGenerator.Application/Documents/Queries/ReadDocumentQuery.cs index 34c7fd7a..03b2d0f8 100644 --- a/EnvelopeGenerator.Application/Documents/Queries/ReadDocumentQuery.cs +++ b/EnvelopeGenerator.Application/Documents/Queries/ReadDocumentQuery.cs @@ -53,14 +53,17 @@ public class ReadDocumentQueryHandler : IRequestHandler public async Task Handle(ReadDocumentQuery query, CancellationToken cancel) { + var docQuery = _repo.Query.Include(doc => doc.Elements).ThenInclude(e => e.Annotations); + if (query.Id is not null) { - var doc = await _repo.Query.Where(d => d.Id == query.Id).FirstOrDefaultAsync(cancel); + var doc = await docQuery.Where(d => d.Id == query.Id).FirstOrDefaultAsync(cancel); + return _mapper.Map(doc); } else if (query.EnvelopeId is not null) { - var doc = await _repo.Query.Where(d => d.EnvelopeId == query.EnvelopeId).FirstOrDefaultAsync(cancel); + var doc = await docQuery.Where(d => d.EnvelopeId == query.EnvelopeId).FirstOrDefaultAsync(cancel); return _mapper.Map(doc); }