- `CreateReceiverCommand` wurde aktualisiert, sodass nun `(Id, AlreadyExists)` anstelle von nur `Id` zurückgegeben wird. - Der Handler wurde geändert, um zu überprüfen, ob bereits ein Empfänger mit derselben E-Mail-Adresse vorhanden ist. - Es wird nur dann ein neuer Empfänger erstellt, wenn dieser noch nicht vorhanden ist. - `Microsoft.EntityFrameworkCore` wurde für die Abfrageunterstützung hinzugefügt.
90 lines
2.4 KiB
C#
90 lines
2.4 KiB
C#
using DigitalData.Core.Abstraction.Application.Repository;
|
|
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<(int Id, 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, (int Id, bool AlreadyExists)>
|
|
{
|
|
/// <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 Id, 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);
|
|
|
|
return (receiver!.Id, alreadyExists);
|
|
}
|
|
} |