98 lines
4.0 KiB
C#
98 lines
4.0 KiB
C#
using DigitalData.Core.DTO;
|
|
using EnvelopeGenerator.Application.Contracts.Services;
|
|
using EnvelopeGenerator.Common.My.Resources;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace EnvelopeGenerator.GeneratorAPI.Controllers
|
|
{
|
|
[Route("api/[controller]")]
|
|
[Authorize]
|
|
[ApiController]
|
|
public class EnvelopeReceiverController : ControllerBase
|
|
{
|
|
private readonly ILogger<EnvelopeReceiverController> _logger;
|
|
private readonly IEnvelopeReceiverService _erService;
|
|
|
|
public EnvelopeReceiverController(ILogger<EnvelopeReceiverController> logger, IEnvelopeReceiverService envelopeReceiverService)
|
|
{
|
|
_logger = logger;
|
|
_erService = envelopeReceiverService;
|
|
}
|
|
|
|
[HttpGet]
|
|
public async Task<IActionResult> GetEnvelopeReceiver([FromQuery] int? min_status = null, [FromQuery] int? max_status = null, [FromQuery] int[]? ignore_status = null)
|
|
{
|
|
try
|
|
{
|
|
var username = User.GetUsername();
|
|
|
|
if (username is null)
|
|
{
|
|
_logger.LogError(@"Envelope Receiver dto cannot be sent because username claim is null. Potential authentication and authorization error. The value of other claims are [id: {id}], [username: {username}], [name: {name}], [prename: {prename}], [email: {email}].",
|
|
User.GetId(), User.GetUsername(), User.GetName(), User.GetPrename(), User.GetEmail());
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
|
|
ignore_status ??= Array.Empty<int>();
|
|
|
|
return await _erService.ReadByUsernameAsync(username: username, min_status: min_status, max_status: max_status, ignore_statuses: ignore_status).ThenAsync(
|
|
Success: Ok,
|
|
Fail: IActionResult (msg, ntc) =>
|
|
{
|
|
_logger.LogNotice(ntc);
|
|
return StatusCode(StatusCodes.Status500InternalServerError, msg);
|
|
});
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
_logger.LogError(ex, "An unexpected error occurred. {message}", ex.Message);
|
|
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);
|
|
}
|
|
}
|
|
|
|
[HttpGet("secret")]
|
|
[Authorize]
|
|
public async Task<IActionResult> GetSecretAsync([FromQuery] string uuid)
|
|
{
|
|
try
|
|
{
|
|
return await _erService.ReadWithSecretByUuidAsync(uuid: uuid).ThenAsync(
|
|
Success: Ok,
|
|
Fail: IActionResult (msg, ntc) =>
|
|
{
|
|
_logger.LogNotice(ntc);
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "{message}", ex.Message);
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
}
|
|
} |