using System.Text.Json; using Application.Endpoints; using Application.Endpoints.SicknessPrediction; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace API.Controllers; [Route("api/[controller]")] public class SicknessPredictionController(IHttpClientFactory clientFactory, IConfiguration configuration) : ControllerBase { [Authorize(Policy = Policies.DoctorPolicy)] [HttpPost] public async Task GetPrediction(SicknessPredictionCommand command) { var client = clientFactory.CreateClient(); var apiKey = configuration["SicknessPredictionService:ApiKey"]; var baseUrl = configuration["SicknessPredictionService:BaseUrl"]; client.DefaultRequestHeaders.Add("x-api-key", apiKey); var response = await client.PostAsJsonAsync($"{baseUrl}/predict", command); if (response.IsSuccessStatusCode) { var responseContent = await response.Content.ReadAsStringAsync(); var baseResponse = JsonSerializer.Deserialize(responseContent); return Ok(baseResponse); } return StatusCode((int)response.StatusCode, "Failed to retrieve data from the prediction service"); } }