refactor(SaveDocStatusCommand): simplify repository filtering in SaveDocStatusCommandHandler

- Replaced explicit envelope and receiver expression filters with a single `.Where(request)` call.
- Removed redundant checks for null and empty values in handler.
- Updated using statements to include `EnvelopeGenerator.Application.Extensions`.
- Maintains the same functionality while reducing code complexity.
This commit is contained in:
Developer 02 2025-08-26 22:34:54 +02:00
parent 783d91a658
commit 8a4d3ff6f9

View File

@ -1,9 +1,8 @@
using DigitalData.Core.Abstraction.Application.Repository; using DigitalData.Core.Abstraction.Application.Repository;
using DigitalData.Core.Exceptions;
using EnvelopeGenerator.Domain.Entities; using EnvelopeGenerator.Domain.Entities;
using MediatR; using MediatR;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System.Linq.Expressions; using EnvelopeGenerator.Application.Extensions;
namespace EnvelopeGenerator.Application.DocStatus.Commands; namespace EnvelopeGenerator.Application.DocStatus.Commands;
@ -60,30 +59,13 @@ public class SaveDocStatusCommandHandler : IRequestHandler<SaveDocStatusCommand,
/// <returns></returns> /// <returns></returns>
public async Task<int?> Handle(SaveDocStatusCommand request, CancellationToken cancel) public async Task<int?> Handle(SaveDocStatusCommand request, CancellationToken cancel)
{ {
// envelope filter
Expression<Func<DocumentStatus, bool>>? eExp =
request.Envelope.Id is not null
? ds => ds.EnvelopeId == request.Envelope.Id
: !string.IsNullOrWhiteSpace(request.Envelope.Uuid)
? ds => ds.Envelope.Uuid == request.Envelope.Uuid
: throw new BadRequestException();
// receiver filter
Expression<Func<DocumentStatus, bool>>? rExp =
request.Receiver.Id is not null
? ds => ds.ReceiverId == request.Receiver.Id
: request.Receiver.EmailAddress is not null
? ds => ds.Receiver.EmailAddress == request.Receiver.EmailAddress
: !string.IsNullOrWhiteSpace(request.Receiver.Signature) ? ds => ds.Receiver.Signature == request.Receiver.Signature
: throw new BadRequestException();
// ceck if exists // ceck if exists
bool isExists = await _repo.ReadOnly().Where(eExp).Where(rExp).AnyAsync(cancel); bool isExists = await _repo.ReadOnly().Where(request).AnyAsync(cancel);
if (isExists) if (isExists)
{ {
var uReq = request.To<UpdateDocStatusCommand>(); var uReq = request.To<UpdateDocStatusCommand>();
await _repo.UpdateAsync(uReq, q => q.Where(eExp).Where(rExp), cancel); await _repo.UpdateAsync(uReq, q => q.Where(request), cancel);
} }
else else
{ {
@ -91,7 +73,7 @@ public class SaveDocStatusCommandHandler : IRequestHandler<SaveDocStatusCommand,
await _repo.CreateAsync(cReq, cancel); await _repo.CreateAsync(cReq, cancel);
} }
var docStatus = await _repo.ReadOnly().Where(eExp).Where(rExp).SingleOrDefaultAsync(cancel); var docStatus = await _repo.ReadOnly().Where(request).SingleOrDefaultAsync(cancel);
return docStatus?.Id; return docStatus?.Id;
} }
} }