- Extend CreateHistoryCommand to implement IRequest<long?> - Introduce CreateHistoryCommandHandler to handle command via IRepository<EnvelopeHistory> - Implement async creation and verification of EnvelopeHistory records
81 lines
1.9 KiB
C#
81 lines
1.9 KiB
C#
using DigitalData.Core.Abstraction.Application.Repository;
|
|
using EnvelopeGenerator.Domain;
|
|
using EnvelopeGenerator.Domain.Entities;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace EnvelopeGenerator.Application.Histories.Commands;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public record CreateHistoryCommand : IRequest<long?>
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public int EnvelopeId { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string UserReference { get; set; } = null!;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public Constants.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, long?>
|
|
{
|
|
private readonly IRepository<EnvelopeHistory> _repo;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="repo"></param>
|
|
public CreateHistoryCommandHandler(IRepository<EnvelopeHistory> repo)
|
|
{
|
|
_repo = repo;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <param name="cancel"></param>
|
|
/// <returns></returns>
|
|
public async Task<long?> Handle(CreateHistoryCommand request, CancellationToken cancel)
|
|
{
|
|
// create entitiy
|
|
await _repo.CreateAsync(request, cancel);
|
|
|
|
// check if created
|
|
var record = await _repo.ReadOnly()
|
|
.Where(h => h.EnvelopeId == request.EnvelopeId)
|
|
.Where(h => h.UserReference == request.UserReference)
|
|
.Where(h => h.ActionDate == request.ActionDate)
|
|
.SingleOrDefaultAsync(cancel);
|
|
|
|
return record?.Id;
|
|
}
|
|
} |