77 lines
No EOL
2.3 KiB
C#
77 lines
No EOL
2.3 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>Rol</c> de la base de datos.
|
|
/// </summary>
|
|
/// <remarks> Implementa la interfaz <c>IRolRepo</c> </remarks>
|
|
public class RolRepo : IRolRepo
|
|
{
|
|
/// <summary>
|
|
/// Contexto de la base de datos en SQL Server.
|
|
/// </summary>
|
|
private readonly CuentasCobrarContext _cuentasCobrarContext;
|
|
public RolRepo(CuentasCobrarContext cuentasCobrarContext)
|
|
{
|
|
_cuentasCobrarContext = cuentasCobrarContext;
|
|
}
|
|
public async Task<IEnumerable<Rol>> Get()
|
|
{
|
|
var roles = await _cuentasCobrarContext.Roles.OrderBy(x => x.Descripcion).ToListAsync();
|
|
return roles;
|
|
}
|
|
public async Task<Rol> Get(int id)
|
|
{
|
|
var rol = await _cuentasCobrarContext.Roles.FirstOrDefaultAsync(x => x.IdRol == id);
|
|
return rol;
|
|
}
|
|
|
|
public async Task<bool> Post(Rol rol)
|
|
{
|
|
bool agregado = false;
|
|
await _cuentasCobrarContext.Roles.AddAsync(rol);
|
|
int filasAfectadas = await _cuentasCobrarContext.SaveChangesAsync();
|
|
if (filasAfectadas > 0)
|
|
{
|
|
agregado = true;
|
|
}
|
|
return agregado;
|
|
}
|
|
|
|
public async Task<bool> Put(Rol rol)
|
|
{
|
|
try
|
|
{
|
|
var rolActual = await Get(rol.IdRol);
|
|
rolActual.Habilitado = rol.Habilitado;
|
|
|
|
await _cuentasCobrarContext.SaveChangesAsync();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public async Task<bool> Delete(int id)
|
|
{
|
|
bool modificado = false;
|
|
var rol = await Get(id);
|
|
_cuentasCobrarContext.Remove(rol);
|
|
int filasAfectadas = await _cuentasCobrarContext.SaveChangesAsync();
|
|
if (filasAfectadas > 0)
|
|
{
|
|
modificado = true;
|
|
}
|
|
return modificado;
|
|
}
|
|
|
|
}
|
|
} |