using DigitalData.Core.Abstraction.Application.Repository;
using EnvelopeGenerator.Domain.Entities;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;
using System.Security.Cryptography;
using System.Text;
namespace EnvelopeGenerator.Application.Receivers.Commands;
///
///
///
[ApiExplorerSettings(IgnoreApi = true)]
public record CreateReceiverCommand : IRequest
{
///
///
///
[EmailAddress]
public required string EmailAddress { get; init; }
///
///
///
public string? TotpSecretkey { get; init; }
///
/// var bytes_arr = Encoding.UTF8.GetBytes(EmailAddress.ToUpper());
/// var hash_arr = SHA256.HashData(bytes_arr);
/// var hexa_str = BitConverter.ToString(hash_arr);
/// return hexa_str.Replace("-", string.Empty);
///
public string Signature
{
get
{
var bytes_arr = Encoding.UTF8.GetBytes(EmailAddress!.ToUpper());
var hash_arr = SHA256.HashData(bytes_arr);
var hexa_str = BitConverter.ToString(hash_arr);
return hexa_str.Replace("-", string.Empty);
}
}
///
/// Default value is DateTime.Now
///
public DateTime AddedWhen { get; } = DateTime.Now;
};
///
///
///
public class CreateReceiverCommandHandler : IRequestHandler
{
///
///
///
private readonly IRepository _repo;
///
///
///
///
public CreateReceiverCommandHandler(IRepository repo)
{
_repo = repo;
}
///
///
///
///
///
///
public async Task Handle(CreateReceiverCommand request, CancellationToken cancel)
{
Receiver receiver = await _repo.CreateAsync(request, cancel);
return receiver.Id;
}
}