81 lines
2.1 KiB
C#
81 lines
2.1 KiB
C#
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;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
[ApiExplorerSettings(IgnoreApi = true)]
|
|
public record CreateReceiverCommand : IRequest<int>
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
[EmailAddress]
|
|
public required string EmailAddress { get; init; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string? TotpSecretkey { get; init; }
|
|
|
|
/// <summary>
|
|
/// var bytes_arr = Encoding.UTF8.GetBytes(EmailAddress.ToUpper());<br/>
|
|
/// var hash_arr = SHA256.HashData(bytes_arr);
|
|
/// var hexa_str = BitConverter.ToString(hash_arr);
|
|
/// return hexa_str.Replace("-", string.Empty);
|
|
/// </summary>
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Default value is DateTime.Now
|
|
/// </summary>
|
|
public DateTime AddedWhen { get; } = DateTime.Now;
|
|
};
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class CreateReceiverCommandHandler : IRequestHandler<CreateReceiverCommand, int>
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
private readonly IRepository<Receiver> _repo;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="repo"></param>
|
|
public CreateReceiverCommandHandler(IRepository<Receiver> repo)
|
|
{
|
|
_repo = repo;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <param name="cancel"></param>
|
|
/// <returns></returns>
|
|
public async Task<int> Handle(CreateReceiverCommand request, CancellationToken cancel)
|
|
{
|
|
Receiver receiver = await _repo.CreateAsync(request, cancel);
|
|
return receiver.Id;
|
|
}
|
|
} |