96 lines
2.7 KiB
C#
96 lines
2.7 KiB
C#
using AutoMapper;
|
|
using DigitalData.Core.Abstraction.Application.Repository;
|
|
using EnvelopeGenerator.Application.Dto.Receiver;
|
|
using EnvelopeGenerator.Domain.Entities;
|
|
using MediatR;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
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<(ReceiverReadDto Receiver, bool AlreadyExists)>
|
|
{
|
|
/// <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, (ReceiverReadDto Receiver, bool AlreadyExists)>
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
private readonly IRepository<Receiver> _repo;
|
|
|
|
private readonly IMapper _mapper;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="repo"></param>
|
|
public CreateReceiverCommandHandler(IRepository<Receiver> repo, IMapper mapper)
|
|
{
|
|
_repo = repo;
|
|
_mapper = mapper;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <param name="cancel"></param>
|
|
/// <returns></returns>
|
|
public async Task<(ReceiverReadDto Receiver, bool AlreadyExists)> Handle(CreateReceiverCommand request, CancellationToken cancel)
|
|
{
|
|
var receiver = await _repo.ReadOnly()
|
|
.Where(r => r.EmailAddress == request.EmailAddress)
|
|
.SingleOrDefaultAsync(cancel);
|
|
|
|
var alreadyExists = receiver is not null;
|
|
|
|
if (!alreadyExists)
|
|
receiver = await _repo.CreateAsync(request, cancel);
|
|
|
|
var receiverDto = _mapper.Map<ReceiverReadDto>(receiver);
|
|
return (receiverDto, alreadyExists);
|
|
}
|
|
} |