feat: Duplikatsprüfung beim Erstellen eines Empfängers hinzufügen
- `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.
This commit is contained in:
parent
c8dae1d8ff
commit
bcb2e79fa1
@ -2,6 +2,7 @@
|
|||||||
using EnvelopeGenerator.Domain.Entities;
|
using EnvelopeGenerator.Domain.Entities;
|
||||||
using MediatR;
|
using MediatR;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
@ -12,7 +13,7 @@ namespace EnvelopeGenerator.Application.Receivers.Commands;
|
|||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[ApiExplorerSettings(IgnoreApi = true)]
|
[ApiExplorerSettings(IgnoreApi = true)]
|
||||||
public record CreateReceiverCommand : IRequest<int>
|
public record CreateReceiverCommand : IRequest<(int Id, bool AlreadyExists)>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
@ -51,7 +52,7 @@ public record CreateReceiverCommand : IRequest<int>
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class CreateReceiverCommandHandler : IRequestHandler<CreateReceiverCommand, int>
|
public class CreateReceiverCommandHandler : IRequestHandler<CreateReceiverCommand, (int Id, bool AlreadyExists)>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
///
|
||||||
@ -73,9 +74,17 @@ public class CreateReceiverCommandHandler : IRequestHandler<CreateReceiverComman
|
|||||||
/// <param name="request"></param>
|
/// <param name="request"></param>
|
||||||
/// <param name="cancel"></param>
|
/// <param name="cancel"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task<int> Handle(CreateReceiverCommand request, CancellationToken cancel)
|
public async Task<(int Id, bool AlreadyExists)> Handle(CreateReceiverCommand request, CancellationToken cancel)
|
||||||
{
|
{
|
||||||
Receiver receiver = await _repo.CreateAsync(request, cancel);
|
var receiver = await _repo.ReadOnly()
|
||||||
return receiver.Id;
|
.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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -64,8 +64,8 @@ public class HistoryTests
|
|||||||
.Select(async email =>
|
.Select(async email =>
|
||||||
{
|
{
|
||||||
var cmd = new CreateReceiverCommand { EmailAddress = email };
|
var cmd = new CreateReceiverCommand { EmailAddress = email };
|
||||||
var id = await Mediator.Send(cmd);
|
var res = await Mediator.Send(cmd);
|
||||||
return (id, email);
|
return (res.Id, email);
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
_receivers.AddRange(receivers);
|
_receivers.AddRange(receivers);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user