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

67 lines
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 De Usuario</c> de la base de datos.
/// </summary>
/// <remarks> Implementa la interfaz <c>IRolDeUsuarioRepo</c> </remarks>
public class RolDeUsuarioRepo : IRolDeUsuarioRepo
{
/// <summary>
/// Contexto de la base de datos en SQL Server.
/// </summary>
private readonly CuentasCobrarContext _cuentasCobrarContext;
public RolDeUsuarioRepo(CuentasCobrarContext cuentasCobrarContext)
{
_cuentasCobrarContext = cuentasCobrarContext;
}
public async Task<List<RolDeUsuario>> Get(string idUsuario)
{
var rolesDeUsuario = await _cuentasCobrarContext.RolesDeUsuarios.Where(x => x.IdUsuario == idUsuario).OrderBy(x => x.DescripcionDeRol).ToListAsync();
return rolesDeUsuario;
}
public async Task<RolDeUsuario> Get(string idUsuario, int idRol)
{
var rolDeUsuario = await _cuentasCobrarContext.RolesDeUsuarios.FirstOrDefaultAsync(x => x.IdUsuario == idUsuario && x.IdRol == idRol);
return rolDeUsuario;
}
public async Task<bool> Post(RolDeUsuario RolDeUsuario)
{
bool agregado = false;
await _cuentasCobrarContext.RolesDeUsuarios.AddAsync(RolDeUsuario);
int filasAfectadas = await _cuentasCobrarContext.SaveChangesAsync();
if (filasAfectadas > 0)
{
agregado = true;
}
return agregado;
}
public async Task<bool> Delete(string idUsuario, int idRol)
{
bool eliminado = false;
var rolDeUsuario = await Get(idUsuario, idRol);
_cuentasCobrarContext.Remove(rolDeUsuario);
int filasAfectadas = await _cuentasCobrarContext.SaveChangesAsync();
if (filasAfectadas > 0)
{
eliminado = true;
}
return eliminado;
}
}
}