TFS_ANTIGUO/TPAtesa_RecHuma/PruebaAzureAD/Servicios/Persona.cs
2026-06-26 10:14:39 -06:00

77 lines
2.2 KiB
C#

using PruebaAzureAD.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace PruebaAzureAD.Servicios
{
public class Persona
{
readonly string url = "http://localhost:46647/api/";
public async Task<List<PersonaViewModel>?> ObtenerPersonas()
{
List<PersonaViewModel>? personas = new List<PersonaViewModel>();
try
{
using (var httpClient = new HttpClient())
{
var respuesta = await httpClient.GetAsync(url + "Persona");
var respuestaString = await respuesta.Content.ReadAsStringAsync();
personas = JsonSerializer.Deserialize<List<PersonaViewModel>>(respuestaString,
new JsonSerializerOptions() { PropertyNameCaseInsensitive = true });
}
}
catch (Exception)
{
throw;
}
return personas;
}
public async Task<HttpResponseMessage> AgregarPersona(PersonaViewModel funcionario)
{
try
{
if (funcionario != null)
{
using (var httpClient = new HttpClient())
{
var data = JsonSerializer.Serialize(funcionario);
var content = new StringContent(data, Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync(url + "Persona", content);
return response;
}
}
else
{
var response = new HttpResponseMessage(HttpStatusCode.BadRequest)
{
Content = new StringContent("Bad Request")
};
return await Task.FromResult(response);
}
}
catch (Exception ex)
{
throw;
}
}
//MODIFICAR
//ELIMINAR
}
}