using DigitalData.Core.DTO; using EnvelopeGenerator.Application.Contracts; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace EnvelopeGenerator.GeneratorAPI.Controllers { [Route("api/[controller]")] [ApiController] public class EnvelopeReceiverController : ControllerBase { private readonly ILogger _logger; private readonly IEnvelopeReceiverService _erService; public EnvelopeReceiverController(ILogger logger, IEnvelopeReceiverService envelopeReceiverService) { _logger = logger; _erService = envelopeReceiverService; } [Authorize] [HttpGet] public async Task GetEnvelopeReceiver() { try { var username = this.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}].", this.GetId(), this.GetUsername(), this.GetName(), this.GetPrename(), this.GetEmail()); return StatusCode(StatusCodes.Status500InternalServerError); } return await _erService.ReadByUsernameAsync(username).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); } } } }