- Add ReadEnvelopeReportQuery record that returns IEnumerable<EnvelopeReport> - Implement ReadEnvelopeReportQueryHandler with repository and AutoMapper integration - Add ThrowIfNotFound property with JsonIgnore and NotMapped attributes - Integrate IRepository<EnvelopeReport> for data access - Add NotFoundException when no reports found and ThrowIfNotFound is true - Configure Entity Framework Core using Microsoft.EntityFrameworkCore - Add JSON serialization configuration with System.Text.Json - Update using statements with necessary abstractions and exceptions
72 lines
2.0 KiB
C#
72 lines
2.0 KiB
C#
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;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public record ReadEnvelopeReportQuery(int EnvelopeId) : IRequest<IEnumerable<EnvelopeReport>>
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
[JsonIgnore]
|
|
[NotMapped]
|
|
public bool ThrowIfNotFound { get; init; } = true;
|
|
};
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class ReadEnvelopeReportQueryHandler : IRequestHandler<ReadEnvelopeReportQuery, IEnumerable<EnvelopeReport>>
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
private readonly IRepository<EnvelopeReport> _repo;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
private readonly IMapper _mapper;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="repo"></param>
|
|
/// <param name="mapper"></param>
|
|
public ReadEnvelopeReportQueryHandler(IRepository<EnvelopeReport> repo, IMapper mapper)
|
|
{
|
|
_repo = repo;
|
|
_mapper = mapper;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <param name="cancel"></param>
|
|
/// <returns></returns>
|
|
public async Task<IEnumerable<EnvelopeReport>> Handle(ReadEnvelopeReportQuery request, CancellationToken cancel = default)
|
|
{
|
|
var reports = await _repo.Where(r => r.EnvelopeId == request.EnvelopeId).ToListAsync(cancel);
|
|
var reportDtos = _mapper.Map<IEnumerable<EnvelopeReport>>(reports);
|
|
|
|
if(request.ThrowIfNotFound && !reportDtos.Any())
|
|
throw new NotFoundException($"EnvelopeReport with EnvelopeId '{request.EnvelopeId}' was not found.");
|
|
|
|
return reportDtos;
|
|
}
|
|
} |