90 lines
2.9 KiB
C#
90 lines
2.9 KiB
C#
using CuentasCobrar.CORE.Entities;
|
|
using CuentasCobrar.CORE.Interfaces;
|
|
using CuentasCobrar.CORE.Pagination;
|
|
using CuentasCobrar.CORE.QueryFilters;
|
|
using CuentasCobrar.INFRASTRUCTURE.Data;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace CuentasCobrar.INFRASTRUCTURE.Repositories
|
|
{
|
|
/// <summary>
|
|
/// Clase encargada de realizar todos las operaciones de lectura, modificación y adición de datos
|
|
/// en la tabla <c>Pais</c> de la base de datos.
|
|
/// </summary>
|
|
/// <remarks> Implementa la interfaz <c>IPaisRepo</c> </remarks>
|
|
public class PaisRepo : IPaisRepo
|
|
{
|
|
/// <summary>
|
|
/// Contexto de la base de datos en SQL Server.
|
|
/// </summary>
|
|
private readonly CuentasCobrarContext _cuentasCobrarContext;
|
|
public PaisRepo(CuentasCobrarContext cuentasCobrarContext)
|
|
{
|
|
_cuentasCobrarContext = cuentasCobrarContext;
|
|
}
|
|
|
|
public async Task<List<Pais>> Get()
|
|
{
|
|
|
|
var paises = await _cuentasCobrarContext.Paises.OrderBy(x => x.Nombre).ToListAsync();
|
|
return paises;
|
|
}
|
|
|
|
public async Task<ListaDePaginacion<Pais>> Get(PaginacionQueryFilter filtros)
|
|
{
|
|
var paises = await _cuentasCobrarContext.Paises.OrderBy(x => x.Nombre).ToListAsync();
|
|
var paisesPaginados = new ListaDePaginacion<Pais>(paises, filtros.NumeroDePagina, filtros.RegistrosPorPagina);
|
|
return paisesPaginados;
|
|
}
|
|
public async Task<Pais> Get(int id)
|
|
{
|
|
var pais = await _cuentasCobrarContext.Paises.FirstOrDefaultAsync(x => x.IdPais == id);
|
|
return pais;
|
|
}
|
|
|
|
public async Task<bool> Post(Pais pais)
|
|
{
|
|
bool agregado = false;
|
|
await _cuentasCobrarContext.Paises.AddAsync(pais);
|
|
int filasAfectadas = await _cuentasCobrarContext.SaveChangesAsync();
|
|
if (filasAfectadas > 0)
|
|
{
|
|
agregado = true;
|
|
}
|
|
return agregado;
|
|
}
|
|
|
|
public async Task<bool> Put(Pais pais)
|
|
{
|
|
try
|
|
{
|
|
var paisActual = await Get(pais.IdPais);
|
|
paisActual.Nombre = pais.Nombre;
|
|
paisActual.Nacionalidad = pais.Nacionalidad;
|
|
paisActual.CodigoDeArea = pais.CodigoDeArea;
|
|
paisActual.Habilitado = pais.Habilitado;
|
|
await _cuentasCobrarContext.SaveChangesAsync();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public async Task<bool> Delete(int id)
|
|
{
|
|
bool eliminado = false;
|
|
var pais = await Get(id);
|
|
_cuentasCobrarContext.Remove(pais);
|
|
int filasAfectadas = await _cuentasCobrarContext.SaveChangesAsync();
|
|
if (filasAfectadas > 0)
|
|
{
|
|
eliminado = true;
|
|
}
|
|
return eliminado;
|
|
}
|
|
|
|
}
|
|
}
|