feat(SaveDocStatusCommand): Füge SaveDocStatusCommand und Handler hinzu, um den Dokumentstatus zu erstellen oder zu aktualisieren.
This commit is contained in:
parent
85a855fe64
commit
2f8401073f
@ -5,7 +5,7 @@ namespace EnvelopeGenerator.Application.DocStatus.Commands;
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public abstract record ModifyDocStatusCommandBase
|
||||
public record ModifyDocStatusCommandBase
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the ID of the associated envelope.
|
||||
|
||||
@ -0,0 +1,58 @@
|
||||
using DigitalData.Core.Abstraction.Application.Repository;
|
||||
using EnvelopeGenerator.Domain.Entities;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace EnvelopeGenerator.Application.DocStatus.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a command to save the status of a document, either by creating a new status or updating an existing one based on the provided envelope and receiver identifiers.
|
||||
/// It returns the identifier of the saved document status.
|
||||
/// </summary>
|
||||
public record SaveDocStatusCommand : ModifyDocStatusCommandBase, IRequest<int?>;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class SaveDocStatusCommandHandler : IRequestHandler<SaveDocStatusCommand, int?>
|
||||
{
|
||||
private readonly IRepository<DocumentStatus> _repo;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="repo"></param>
|
||||
public SaveDocStatusCommandHandler(IRepository<DocumentStatus> repo)
|
||||
{
|
||||
_repo = repo;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <param name="cancel"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<int?> Handle(SaveDocStatusCommand request, CancellationToken cancel)
|
||||
{
|
||||
Expression<Func<DocumentStatus, bool>> filter = ds => ds.EnvelopeId == request.EnvelopeId && ds.ReceiverId == request.ReceiverId;
|
||||
|
||||
// ceck if exists
|
||||
bool isExists = await _repo.ReadOnly().Where(filter).AnyAsync(cancel);
|
||||
|
||||
if (isExists)
|
||||
{
|
||||
var uReq = request.To<UpdateDocStatusCommand>();
|
||||
await _repo.UpdateAsync(uReq, filter, cancel);
|
||||
}
|
||||
else
|
||||
{
|
||||
var cReq = request.To<CreateDocStatusCommand>();
|
||||
await _repo.CreateAsync(cReq, cancel);
|
||||
}
|
||||
|
||||
var docStatus = await _repo.ReadOnly().Where(filter).FirstOrDefaultAsync(cancel);
|
||||
return docStatus?.Id;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user