feat(controller): add endpoint to retrieve last used receiver name by email

- Implemented `Get` endpoint in `EnvelopeReceiverController` to fetch the last used receiver's name by email.
- Added handling for successful and failed responses, including proper status codes and logging.
This commit is contained in:
Developer 02 2024-08-29 17:15:00 +02:00
parent 1ededc1f64
commit 8831436809
2 changed files with 26 additions and 3 deletions

View File

@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Mvc;
namespace EnvelopeGenerator.GeneratorAPI.Controllers
{
[Route("api/[controller]")]
[Authorize]
[ApiController]
public class EnvelopeReceiverController : ControllerBase
{
@ -18,7 +19,6 @@ namespace EnvelopeGenerator.GeneratorAPI.Controllers
_erService = envelopeReceiverService;
}
[Authorize]
[HttpGet]
public async Task<IActionResult> GetEnvelopeReceiver([FromQuery] int? min_status = null, [FromQuery] int? max_status = null, [FromQuery] int[]? ignore_status = null)
{
@ -49,5 +49,28 @@ namespace EnvelopeGenerator.GeneratorAPI.Controllers
return new StatusCodeResult(StatusCodes.Status500InternalServerError);
}
}
[HttpGet("receiver-name/{mail}")]
public async Task<IActionResult> GetReceiverName([FromRoute] string mail)
{
try
{
return await _erService.ReadLastUsedReceiverNameByMail(mail).ThenAsync(
Success: res => res is null ? Ok(string.Empty) : Ok(res),
Fail: IActionResult (msg, ntc) =>
{
if (ntc.HasFlag(Flag.NotFound))
return NotFound();
_logger.LogNotice(ntc);
return StatusCode(StatusCodes.Status500InternalServerError);
});
}
catch(Exception ex)
{
_logger.LogError(ex, "{message}", ex.Message);
return StatusCode(StatusCodes.Status500InternalServerError);
}
}
}
}

View File

@ -78,7 +78,7 @@ namespace EnvelopeGenerator.Infrastructure.Repositories
public async Task<EnvelopeReceiver?> ReadLastByReceiver(string email)
{
return await _dbSet.Where(er => er.Receiver!.EmailAddress == email).LastOrDefaultAsync();
return await _dbSet.Where(er => er.Receiver!.EmailAddress == email).OrderBy(er => er.EnvelopeId).LastOrDefaultAsync();
}
}
}