using AutoMapper;
using DigitalData.Core.Abstraction.Application.Repository;
using DigitalData.Core.Exceptions;
using EnvelopeGenerator.Domain.Entities;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace EnvelopeGenerator.Application.EnvelopeReports;
///
///
///
public record ReadEnvelopeReportQuery(int EnvelopeId) : IRequest>
{
///
///
///
[JsonIgnore]
[NotMapped]
public bool ThrowIfNotFound { get; init; } = true;
};
///
///
///
public static class ReadEnvelopeReportQueryExtensions
{
///
///
///
///
///
///
///
///
public static Task> ReadEnvelopeReportAsync(this ISender sender, int envelopeId, bool throwIfNotFound = true, CancellationToken cancel = default)
=> sender.Send(new ReadEnvelopeReportQuery(envelopeId)
{
ThrowIfNotFound = throwIfNotFound
}, cancel);
}
///
///
///
public class ReadEnvelopeReportQueryHandler : IRequestHandler>
{
///
///
///
private readonly IRepository _repo;
///
///
///
private readonly IMapper _mapper;
///
///
///
///
///
public ReadEnvelopeReportQueryHandler(IRepository repo, IMapper mapper)
{
_repo = repo;
_mapper = mapper;
}
///
///
///
///
///
///
public async Task> Handle(ReadEnvelopeReportQuery request, CancellationToken cancel = default)
{
var reports = await _repo.Where(r => r.EnvelopeId == request.EnvelopeId).ToListAsync(cancel);
var reportDtos = _mapper.Map>(reports);
if(request.ThrowIfNotFound && !reportDtos.Any())
throw new NotFoundException($"EnvelopeReport with EnvelopeId '{request.EnvelopeId}' was not found.");
return reportDtos;
}
}