Fase 5: Implementada la configuración dinámica. Implementado el scraping web.
This commit is contained in:
		| @@ -0,0 +1,48 @@ | ||||
| // backend/src/Titulares.Api/Controllers/ConfiguracionController.cs | ||||
| using Microsoft.AspNetCore.Mvc; | ||||
| using Microsoft.Extensions.Options; | ||||
| using System.Text.Json; | ||||
| using Titulares.Api.Models; | ||||
|  | ||||
| [ApiController] | ||||
| [Route("api/[controller]")] | ||||
| public class ConfiguracionController : ControllerBase | ||||
| { | ||||
|   private readonly IOptionsMonitor<ConfiguracionApp> _configuracionMonitor; | ||||
|   private readonly string _configFilePath = "configuracion.json"; | ||||
|  | ||||
|   public ConfiguracionController(IOptionsMonitor<ConfiguracionApp> configuracionMonitor) | ||||
|   { | ||||
|     _configuracionMonitor = configuracionMonitor; | ||||
|   } | ||||
|  | ||||
|   [HttpGet] | ||||
|   public IActionResult ObtenerConfiguracion() | ||||
|   { | ||||
|     // IOptionsMonitor siempre nos da el valor más reciente. | ||||
|     return Ok(_configuracionMonitor.CurrentValue); | ||||
|   } | ||||
|  | ||||
|   [HttpPost] | ||||
|   public async Task<IActionResult> GuardarConfiguracion([FromBody] ConfiguracionApp nuevaConfiguracion) | ||||
|   { | ||||
|     if (!ModelState.IsValid) | ||||
|     { | ||||
|       return BadRequest(ModelState); | ||||
|     } | ||||
|  | ||||
|     try | ||||
|     { | ||||
|       var options = new JsonSerializerOptions { WriteIndented = true }; | ||||
|       var jsonString = JsonSerializer.Serialize(nuevaConfiguracion, options); | ||||
|       await System.IO.File.WriteAllTextAsync(_configFilePath, jsonString); | ||||
|  | ||||
|       // No es necesario reiniciar la app, gracias a `reloadOnChange: true` y IOptionsMonitor. | ||||
|       return Ok(new { message = "Configuración guardada correctamente." }); | ||||
|     } | ||||
|     catch (Exception ex) | ||||
|     { | ||||
|       return StatusCode(500, $"Error al guardar el archivo de configuración: {ex.Message}"); | ||||
|     } | ||||
|   } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user