diff --git a/EnvelopeGenerator.Application/Contracts/IReceiverService.cs b/EnvelopeGenerator.Application/Contracts/IReceiverService.cs index bde7b25c..7721d2ab 100644 --- a/EnvelopeGenerator.Application/Contracts/IReceiverService.cs +++ b/EnvelopeGenerator.Application/Contracts/IReceiverService.cs @@ -1,4 +1,5 @@ using DigitalData.Core.Abstractions.Application; +using DigitalData.Core.DTO; using EnvelopeGenerator.Application.DTOs.Receiver; using EnvelopeGenerator.Domain.Entities; @@ -6,5 +7,6 @@ namespace EnvelopeGenerator.Application.Contracts { public interface IReceiverService : ICRUDService { + public Task> ReadByAsync(string? emailAddress = null, string? signature = null); } } \ No newline at end of file diff --git a/EnvelopeGenerator.Application/Services/ReceiverService.cs b/EnvelopeGenerator.Application/Services/ReceiverService.cs index 2aa4818e..352a8240 100644 --- a/EnvelopeGenerator.Application/Services/ReceiverService.cs +++ b/EnvelopeGenerator.Application/Services/ReceiverService.cs @@ -6,6 +6,7 @@ using EnvelopeGenerator.Domain.Entities; using EnvelopeGenerator.Infrastructure.Contracts; using EnvelopeGenerator.Application.Resources; using EnvelopeGenerator.Application.DTOs.Receiver; +using DigitalData.Core.DTO; namespace EnvelopeGenerator.Application.Services { @@ -15,5 +16,15 @@ namespace EnvelopeGenerator.Application.Services : base(repository, mapper) { } + + public async Task> ReadByAsync(string? emailAddress = null, string? signature = null) + { + var rcv = await _repository.ReadByAsync(emailAddress: emailAddress, signature: signature); + + if (rcv is null) + return Result.Fail(); + + return Result.Success(_mapper.MapOrThrow(rcv)); + } } } \ No newline at end of file diff --git a/EnvelopeGenerator.GeneratorAPI/Controllers/ReceiverController.cs b/EnvelopeGenerator.GeneratorAPI/Controllers/ReceiverController.cs index 5656b947..ac19b08b 100644 --- a/EnvelopeGenerator.GeneratorAPI/Controllers/ReceiverController.cs +++ b/EnvelopeGenerator.GeneratorAPI/Controllers/ReceiverController.cs @@ -1,8 +1,10 @@ using DigitalData.Core.API; +using DigitalData.Core.DTO; using EnvelopeGenerator.Application.Contracts; using EnvelopeGenerator.Application.DTOs.Receiver; using EnvelopeGenerator.Domain.Entities; using Microsoft.AspNetCore.Mvc; +using System.ComponentModel.DataAnnotations.Schema; namespace EnvelopeGenerator.GeneratorAPI.Controllers { @@ -13,5 +15,26 @@ namespace EnvelopeGenerator.GeneratorAPI.Controllers public ReceiverController(ILogger logger, IReceiverService service) : base(logger, service) { } + + [NonAction] + public override Task GetAll() + { + return base.GetAll(); + } + + [HttpGet] + public async Task Get([FromQuery] string? emailAddress = null, [FromQuery] string? signature = null) + { + if (emailAddress is null && signature is null) + return await base.GetAll(); + + return await _service.ReadByAsync(emailAddress: emailAddress, signature: signature).ThenAsync( + Success: Ok, + Fail: IActionResult (msg,ntc) => + { + _logger.LogNotice(ntc); + return StatusCode(StatusCodes.Status500InternalServerError); + }); + } } } \ No newline at end of file diff --git a/EnvelopeGenerator.Infrastructure/Contracts/IReceiverRepository.cs b/EnvelopeGenerator.Infrastructure/Contracts/IReceiverRepository.cs index bde2bd04..83ad4cad 100644 --- a/EnvelopeGenerator.Infrastructure/Contracts/IReceiverRepository.cs +++ b/EnvelopeGenerator.Infrastructure/Contracts/IReceiverRepository.cs @@ -5,5 +5,6 @@ namespace EnvelopeGenerator.Infrastructure.Contracts { public interface IReceiverRepository : ICRUDRepository { + Task ReadByAsync(string? emailAddress = null, string? signature = null); } } \ No newline at end of file diff --git a/EnvelopeGenerator.Infrastructure/Repositories/ReceiverRepository.cs b/EnvelopeGenerator.Infrastructure/Repositories/ReceiverRepository.cs index 8258c76b..53ffb2ba 100644 --- a/EnvelopeGenerator.Infrastructure/Repositories/ReceiverRepository.cs +++ b/EnvelopeGenerator.Infrastructure/Repositories/ReceiverRepository.cs @@ -1,7 +1,7 @@ using DigitalData.Core.Infrastructure; -using DigitalData.UserManager.Infrastructure.Repositories; using EnvelopeGenerator.Domain.Entities; using EnvelopeGenerator.Infrastructure.Contracts; +using Microsoft.EntityFrameworkCore; namespace EnvelopeGenerator.Infrastructure.Repositories { @@ -10,5 +10,20 @@ namespace EnvelopeGenerator.Infrastructure.Repositories public ReceiverRepository(EGDbContext dbContext) : base(dbContext) { } + + protected IQueryable ReadBy(string? emailAddress = null, string? signature = null) + { + IQueryable query = _dbSet.AsNoTracking(); + + if(emailAddress is not null) + query = query.Where(r => r.EmailAddress == emailAddress); + + if(signature is not null) + query = query.Where(r => r.Signature == signature); + + return query; + } + + public async Task ReadByAsync(string? emailAddress = null, string? signature = null) => await ReadBy(emailAddress, signature).FirstOrDefaultAsync(); } } \ No newline at end of file