Refactor receiver methods for async support

Updated repository and service interfaces to use async methods for retrieving receiver information. Changed method signatures to include optional parameters for `id` and `signature`, and made existing parameters nullable. Adjusted related service and controller implementations to ensure consistent usage of the new async methods. Improved error handling in the repository to enforce parameter requirements. Updated using directives in the repository for necessary dependencies.
This commit is contained in:
Developer 02
2025-05-12 09:38:29 +02:00
parent 4401a70217
commit c1bce7c639
6 changed files with 33 additions and 12 deletions

View File

@@ -2,6 +2,9 @@
using EnvelopeGenerator.Domain.Entities;
using EnvelopeGenerator.Application.Contracts.Repositories;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Http.HttpResults;
using EnvelopeGenerator.Application.Exceptions;
using EnvelopeGenerator.Common.My.Resources;
namespace EnvelopeGenerator.Infrastructure.Repositories;
@@ -93,8 +96,26 @@ public class EnvelopeReceiverRepository : CRUDRepository<EnvelopeReceiver, (int
return await query.Include(er => er.Envelope).Include(er => er.Receiver).ToListAsync();
}
public async Task<EnvelopeReceiver?> ReadLastByReceiver(string email)
public async Task<EnvelopeReceiver?> ReadLastByReceiverAsync(string? email = null, int? id = null, string? signature = null)
{
return await _dbSet.Where(er => er.Receiver!.EmailAddress == email).OrderBy(er => er.EnvelopeId).LastOrDefaultAsync();
var parameters = new[] { email, id?.ToString(), signature }.Count(p => p != null);
if (parameters == 0)
throw new BadRequestException("You must provide either 'email', 'id', or 'signature' for the query.");
if (parameters > 1)
throw new BadRequestException("Please provide only one parameter: either 'email', 'id', or 'signature'.");
var query = _dbSet.AsNoTracking();
if(email is not null)
query = query.Where(er => er.Receiver!.EmailAddress == email);
if (id is not null)
query = query.Where(er => er.Receiver!.Id == id);
if (signature is not null)
query = query.Where(er => er.Receiver!.Signature == signature);
return await query.OrderBy(er => er.EnvelopeId).LastOrDefaultAsync();
}
}