30 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			30 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using Microsoft.AspNetCore.Mvc;
 | |
| using Newtonsoft.Json;
 | |
| using Refit;
 | |
| 
 | |
| namespace Core.Cerberos.BFF.Api.Controllers
 | |
| {
 | |
|     [Route("api/[controller]")]
 | |
|     [ApiController]
 | |
|     public class BaseController(ILogger<BaseController> logger) : ControllerBase
 | |
|     {
 | |
|         private readonly ILogger<BaseController> _logger = logger;
 | |
| 
 | |
|         protected Guid TrackingId => (Guid)(HttpContext.Items["TrackingId"] ?? Guid.NewGuid());
 | |
| 
 | |
|         protected async Task<IActionResult> Handle<T>(Func<Task<ApiResponse<T>>> apiCall) where T : class
 | |
|         {
 | |
|             var response = await apiCall().ConfigureAwait(false);
 | |
| 
 | |
|             _logger.LogInformation($"{TrackingId} - {response.RequestMessage?.Method} {response.RequestMessage?.RequestUri} - Status: {response.StatusCode}");
 | |
| 
 | |
|             return FromAPIResponse(response);
 | |
|         }
 | |
| 
 | |
|         private IActionResult FromAPIResponse<T>(ApiResponse<T> response) where T : class
 | |
|         {
 | |
|             var errorContent = JsonConvert.DeserializeObject<string>(response.Error?.Content ?? string.Empty) ?? string.Empty;
 | |
|             return StatusCode((int)response.StatusCode, (response.Content is not null) ? response.Content : errorContent);
 | |
|         }
 | |
|     }
 | |
| } | 
