using DigitalData.Core.Abstraction.Application.Repository;
using EnvelopeGenerator.Domain;
using EnvelopeGenerator.Domain.Entities;
using MediatR;
using Microsoft.EntityFrameworkCore;
namespace EnvelopeGenerator.Application.Histories.Commands;
///
///
///
public record CreateHistoryCommand : IRequest
{
///
///
///
public int EnvelopeId { get; set; }
///
///
///
public string UserReference { get; set; } = null!;
///
///
///
public Constants.EnvelopeStatus Status { get; set; }
///
///
///
public DateTime AddedWhen { get; } = DateTime.Now;
///
///
///
public DateTime ActionDate => AddedWhen;
///
///
///
public string? Comment { get; set; }
}
///
///
///
public class CreateHistoryCommandHandler : IRequestHandler
{
private readonly IRepository _repo;
///
///
///
///
public CreateHistoryCommandHandler(IRepository repo)
{
_repo = repo;
}
///
///
///
///
///
///
public async Task 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;
}
}