Added XML documentation to extension and handler classes for improved maintainability. Refactored repository access to use .Query instead of .ReadOnly() for consistency. Updated async extension methods for better readability and error handling.
108 lines
3.0 KiB
C#
108 lines
3.0 KiB
C#
using AutoMapper;
|
|
using DigitalData.Core.Abstraction.Application.Repository;
|
|
using DigitalData.Core.Exceptions;
|
|
using EnvelopeGenerator.Application.Common.Dto.History;
|
|
using EnvelopeGenerator.Application.Common.Extensions;
|
|
using EnvelopeGenerator.Application.Common.Query;
|
|
using EnvelopeGenerator.Domain.Constants;
|
|
using EnvelopeGenerator.Domain.Entities;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace EnvelopeGenerator.Application.Histories.Commands;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public record CreateHistoryCommand : EnvelopeReceiverQueryBase, IRequest<HistoryDto?>
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public int? EnvelopeId { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string? UserReference { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public EnvelopeStatus Status { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public DateTime AddedWhen { get; } = DateTime.Now;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public DateTime ActionDate => AddedWhen;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string? Comment { get; set; }
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class CreateHistoryCommandHandler : IRequestHandler<CreateHistoryCommand, HistoryDto?>
|
|
{
|
|
private readonly IRepository<History> _repo;
|
|
|
|
private readonly IRepository<EnvelopeReceiver> _erRepo;
|
|
|
|
private readonly IMapper _mapper;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="repo"></param>
|
|
/// <param name="erRepo"></param>
|
|
/// <param name="mapper"></param>
|
|
public CreateHistoryCommandHandler(IRepository<History> repo, IRepository<EnvelopeReceiver> erRepo, IMapper mapper)
|
|
{
|
|
_repo = repo;
|
|
_erRepo = erRepo;
|
|
_mapper = mapper;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <param name="cancel"></param>
|
|
/// <returns></returns>
|
|
public async Task<HistoryDto?> Handle(CreateHistoryCommand request, CancellationToken cancel)
|
|
{
|
|
if(request.UserReference is null)
|
|
{
|
|
var receivers = await _erRepo
|
|
.Query
|
|
.Where(request)
|
|
.Include(er => er.Receiver)
|
|
.ToListAsync(cancel);
|
|
|
|
if (receivers.Count != 1)
|
|
throw new BadRequestException(
|
|
receivers.Count > 1
|
|
? "Multiple receivers found for the given envelope and receiver criteria."
|
|
: "No receiver found for the given envelope and receiver criteria."
|
|
);
|
|
|
|
var receiver = receivers.Single().Receiver
|
|
?? throw new BadRequestException("No receiver found for the given envelope and receiver criteria.");
|
|
|
|
request.UserReference = receiver.EmailAddress;
|
|
}
|
|
|
|
// create entitiy
|
|
var hist = await _repo.CreateAsync(request, cancel);
|
|
|
|
return _mapper.Map<HistoryDto>(hist);
|
|
}
|
|
} |