108 lines
3.9 KiB
C#
108 lines
3.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>Cliente</c> de la base de datos.
|
|
/// </summary>
|
|
/// <remarks> Implementa la interfaz <c>IClienteRepo</c> </remarks>
|
|
public class ClienteRepo : IClienteRepo
|
|
{
|
|
/// <summary>
|
|
/// Contexto de la base de datos en SQL Server.
|
|
/// </summary>
|
|
private readonly CuentasCobrarContext _cuentasCobrarContext;
|
|
public ClienteRepo(CuentasCobrarContext cuentasCobrarContext)
|
|
{
|
|
_cuentasCobrarContext = cuentasCobrarContext;
|
|
}
|
|
|
|
public async Task<List<Cliente>> Get()
|
|
{
|
|
|
|
var clientes = await _cuentasCobrarContext.Clientes.OrderBy(x => x.Nombre).ToListAsync();
|
|
return clientes;
|
|
}
|
|
|
|
public async Task<ListaDePaginacion<Cliente>> Get(ClienteQueryFilter filtros)
|
|
{
|
|
var clientes = await _cuentasCobrarContext.Clientes.OrderBy(x => x.Nombre).ToListAsync();
|
|
var paises = await _cuentasCobrarContext.Paises.ToListAsync();
|
|
var tiposIdentificacion = await _cuentasCobrarContext.TipoIdentificaciones.ToListAsync();
|
|
|
|
if (filtros.Busqueda != null) clientes = filtros.FiltrarPorBusqueda(clientes, paises, tiposIdentificacion);
|
|
|
|
var clientesPaginados = new ListaDePaginacion<Cliente>(clientes, filtros.NumeroDePagina, filtros.RegistrosPorPagina);
|
|
return clientesPaginados;
|
|
}
|
|
|
|
public async Task<Cliente> Get(string noIdentificacion)
|
|
{
|
|
var cliente = await _cuentasCobrarContext.Clientes.FirstOrDefaultAsync(x => x.NoIdentificacion.ToUpper() == noIdentificacion.ToUpper());
|
|
return cliente;
|
|
}
|
|
|
|
public async Task<bool> Post(Cliente cliente)
|
|
{
|
|
|
|
bool agregado = false;
|
|
cliente.NoIdentificacion = cliente.NoIdentificacion.ToUpper();
|
|
await _cuentasCobrarContext.Clientes.AddAsync(cliente);
|
|
int filasAfectadas = await _cuentasCobrarContext.SaveChangesAsync();
|
|
|
|
if (filasAfectadas > 0)
|
|
{
|
|
agregado = true;
|
|
}
|
|
return agregado;
|
|
}
|
|
|
|
public async Task<bool> Put(string noIdentificacion, Cliente cliente)
|
|
{
|
|
try
|
|
{
|
|
var clienteActual = await Get(noIdentificacion.ToUpper());
|
|
clienteActual.NoIdentificacion = cliente.NoIdentificacion.ToUpper();
|
|
clienteActual.IdTipoIdentificacion = cliente.IdTipoIdentificacion;
|
|
clienteActual.Nombre = cliente.Nombre;
|
|
clienteActual.Apellido1 = cliente.Apellido1;
|
|
clienteActual.Apellido2 = cliente.Apellido2;
|
|
clienteActual.IdPaisNacionalidad = cliente.IdPaisNacionalidad;
|
|
clienteActual.Correo = cliente.Correo;
|
|
clienteActual.Direccion = cliente.Direccion;
|
|
clienteActual.Habilitado = cliente.Habilitado;
|
|
await _cuentasCobrarContext.SaveChangesAsync();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public async Task<bool> Delete(string noIdentificacion)
|
|
{
|
|
bool eliminado = false;
|
|
var cliente = await Get(noIdentificacion.ToUpper());
|
|
_cuentasCobrarContext.Remove(cliente);
|
|
int filasAfectadas = await _cuentasCobrarContext.SaveChangesAsync();
|
|
if (filasAfectadas > 0)
|
|
{
|
|
eliminado = true;
|
|
}
|
|
|
|
return eliminado;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|