TFS_ANTIGUO/TPAtesa_CuentasCobrar/CuentasCobrar.INFRASTRUCTURE/Repositories/TelefonoRepo.cs

84 lines
2.6 KiB
C#

using CuentasCobrar.CORE.Entities;
using CuentasCobrar.CORE.Interfaces;
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>Telefono</c> de la base de datos.
/// </summary>
/// <remarks> Implementa la interfaz <c>ITelefonoRepo</c> </remarks>
public class TelefonoRepo : ITelefonoRepo
{
/// <summary>
/// Contexto de la base de datos en SQL Server.
/// </summary>
private CuentasCobrarContext _cuentasCobrarContext;
public TelefonoRepo(CuentasCobrarContext cuentasCobrarContext)
{
_cuentasCobrarContext = cuentasCobrarContext;
}
public async Task<IEnumerable<Telefono>> Get()
{
var telefonos = await _cuentasCobrarContext.Telefonos.ToListAsync();
return telefonos;
}
public async Task<Telefono> Get(int id)
{
var telefono = await _cuentasCobrarContext.Telefonos.FirstOrDefaultAsync(x => x.IdTelefono == id);
return telefono;
}
public async Task<bool> Post(Telefono telefono)
{
bool agregado = false;
telefono.NoIdentificacionCliente = telefono.NoIdentificacionCliente.ToUpper();
await _cuentasCobrarContext.Telefonos.AddAsync(telefono);
int filasAfectadas = await _cuentasCobrarContext.SaveChangesAsync();
if (filasAfectadas > 0)
{
agregado = true;
}
return agregado;
}
public async Task<bool> Put(Telefono telefono)
{
try
{
var telefonoActual = await Get(telefono.IdTelefono);
telefonoActual.Numero = telefono.Numero;
telefonoActual.IdTipoTelefono = telefono.IdTipoTelefono;
telefonoActual.Habilitado = telefono.Habilitado;
await _cuentasCobrarContext.SaveChangesAsync();
}
catch (Exception)
{
return false;
}
return true;
}
public async Task<bool> Delete(int id)
{
bool eliminado = false;
var telefono = await Get(id);
_cuentasCobrarContext.Remove(telefono);
int filasAfectadas = await _cuentasCobrarContext.SaveChangesAsync();
if (filasAfectadas > 0)
{
eliminado = true;
}
return eliminado;
}
}
}