diff --git a/EnvelopeGenerator.Application/Histories/Queries/Read/ReadHistoryQueryHandler.cs b/EnvelopeGenerator.Application/Histories/Queries/Read/ReadHistoryQueryHandler.cs index ddc88001..cff4c4c7 100644 --- a/EnvelopeGenerator.Application/Histories/Queries/Read/ReadHistoryQueryHandler.cs +++ b/EnvelopeGenerator.Application/Histories/Queries/Read/ReadHistoryQueryHandler.cs @@ -1,7 +1,9 @@ using AutoMapper; -using EnvelopeGenerator.Application.Interfaces.Repositories; +using DigitalData.Core.Abstraction.Application.Repository; using DigitalData.Core.Exceptions; +using EnvelopeGenerator.Domain.Entities; using MediatR; +using Microsoft.EntityFrameworkCore; namespace EnvelopeGenerator.Application.Histories.Queries.Read; @@ -10,20 +12,18 @@ namespace EnvelopeGenerator.Application.Histories.Queries.Read; /// public class ReadHistoryQueryHandler : IRequestHandler> { - [Obsolete("Use IRepository")] - private readonly IEnvelopeHistoryRepository _repository; + private readonly IRepository _repo; private readonly IMapper _mapper; /// /// /// - /// + /// /// - [Obsolete("Use IRepository")] - public ReadHistoryQueryHandler(IEnvelopeHistoryRepository repository, IMapper mapper) + public ReadHistoryQueryHandler(IRepository repo, IMapper mapper) { - _repository = repository; + _repo = repo; _mapper = mapper; } @@ -31,16 +31,20 @@ public class ReadHistoryQueryHandler : IRequestHandler /// - /// + /// /// /// - public async Task> Handle(ReadHistoryQuery request, CancellationToken cancellationToken) + public async Task> Handle(ReadHistoryQuery request, CancellationToken cancel = default) { - var hists = await _repository.ReadAsync(request.EnvelopeId, status: request.Status is null ? null : request.Status); + var query = _repo.ReadOnly().Where(h => h.EnvelopeId == request.EnvelopeId); + if (request.Status is not null) + query = query.Where(h => h.Status == request.Status); - if (!hists.Any()) - throw new NotFoundException(); - - return _mapper.Map>(hists); + var hists = await query.ToListAsync(cancel); + + if (hists.Count == 0) + return _mapper.Map>(hists); + + throw new NotFoundException(); } } \ No newline at end of file