< Summary

Information
Class: FAU.Logica.Services.RolService
Assembly: FAU.Logica
File(s): /home/runner/work/Sistema.Liquidacion.BE/Sistema.Liquidacion.BE/FAU.Logica/Services/RolService.cs
Line coverage
87%
Covered lines: 54
Uncovered lines: 8
Coverable lines: 62
Total lines: 148
Line coverage: 87%
Branch coverage
100%
Covered branches: 16
Total branches: 16
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
GetByIdAsync()100%11100%
GetByNombreAsync()100%11100%
GetAllAsync()100%11100%
CreateAsync()100%2283.33%
UpdateAsync()100%6683.33%
AsignarPermisoAsync()100%6684.61%
RemoverPermisoAsync()100%4481.81%
DeleteAsync()100%11100%

File(s)

/home/runner/work/Sistema.Liquidacion.BE/Sistema.Liquidacion.BE/FAU.Logica/Services/RolService.cs

#LineLine coverage
 1using FAU.DataAccess.Repositories;
 2using FAU.Entidades;
 3
 4namespace FAU.Logica.Services;
 5
 6public class RolService : IRolService
 7{
 8    private readonly IRolRepository _rolRepository;
 9    private readonly IPermisoRepository _permisoRepository;
 10
 1911    public RolService(IRolRepository rolRepository, IPermisoRepository permisoRepository)
 12    {
 1913        _rolRepository = rolRepository;
 1914        _permisoRepository = permisoRepository;
 1915    }
 16
 17    public async Task<Rol?> GetByIdAsync(long id)
 18    {
 219        return await _rolRepository.GetByIdAsync(id);
 220    }
 21
 22    public async Task<Rol?> GetByNombreAsync(string nombre)
 23    {
 224        return await _rolRepository.GetByNombreAsync(nombre);
 225    }
 26
 27    public async Task<IEnumerable<Rol>> GetAllAsync()
 28    {
 229        return await _rolRepository.GetAllAsync();
 230    }
 31
 32    public async Task<(bool Success, Rol? Rol, string? Error)> CreateAsync(string nombre, string? descripcion = null)
 33    {
 34        try
 35        {
 236            if (await _rolRepository.ExistsAsync(nombre))
 37            {
 138                return (false, null, "Ya existe un rol con ese nombre");
 39            }
 40
 141            var nuevoRol = new Rol
 142            {
 143                Nombre = nombre,
 144                Descripcion = descripcion
 145            };
 46
 147            var rolCreado = await _rolRepository.CreateAsync(nuevoRol);
 148            return (true, rolCreado, null);
 49        }
 050        catch (Exception ex)
 51        {
 052            return (false, null, $"Error al crear rol: {ex.Message}");
 53        }
 254    }
 55
 56    public async Task<(bool Success, Rol? Rol, string? Error)> UpdateAsync(long id, string nombre, string? descripcion =
 57    {
 58        try
 59        {
 360            var rol = await _rolRepository.GetByIdAsync(id);
 361            if (rol == null)
 62            {
 163                return (false, null, "Rol no encontrado");
 64            }
 65
 66            // Verificar si el nuevo nombre ya existe (si es diferente al actual)
 267            if (rol.Nombre != nombre && await _rolRepository.ExistsAsync(nombre))
 68            {
 169                return (false, null, "Ya existe un rol con ese nombre");
 70            }
 71
 172            rol.Nombre = nombre;
 173            rol.Descripcion = descripcion;
 74
 175            var rolActualizado = await _rolRepository.UpdateAsync(rol);
 176            return (true, rolActualizado, null);
 77        }
 078        catch (Exception ex)
 79        {
 080            return (false, null, $"Error al actualizar rol: {ex.Message}");
 81        }
 382    }
 83
 84    public async Task<(bool Success, string? Error)> AsignarPermisoAsync(long rolId, long permisoId)
 85    {
 86        try
 87        {
 488            var rol = await _rolRepository.GetByIdAsync(rolId);
 489            if (rol == null)
 90            {
 191                return (false, "Rol no encontrado");
 92            }
 93
 394            var permiso = await _permisoRepository.GetByIdAsync(permisoId);
 395            if (permiso == null)
 96            {
 197                return (false, "Permiso no encontrado");
 98            }
 99
 3100            if (rol.Permisos.Any(p => p.Id == permisoId))
 101            {
 1102                return (false, "El rol ya tiene este permiso asignado");
 103            }
 104
 1105            rol.Permisos.Add(permiso);
 1106            await _rolRepository.UpdateAsync(rol);
 107
 1108            return (true, null);
 109        }
 0110        catch (Exception ex)
 111        {
 0112            return (false, $"Error al asignar permiso: {ex.Message}");
 113        }
 4114    }
 115
 116    public async Task<(bool Success, string? Error)> RemoverPermisoAsync(long rolId, long permisoId)
 117    {
 118        try
 119        {
 3120            var rol = await _rolRepository.GetByIdAsync(rolId);
 3121            if (rol == null)
 122            {
 1123                return (false, "Rol no encontrado");
 124            }
 125
 3126            var permiso = rol.Permisos.FirstOrDefault(p => p.Id == permisoId);
 2127            if (permiso == null)
 128            {
 1129                return (false, "El rol no tiene este permiso asignado");
 130            }
 131
 1132            rol.Permisos.Remove(permiso);
 1133            await _rolRepository.UpdateAsync(rol);
 134
 1135            return (true, null);
 136        }
 0137        catch (Exception ex)
 138        {
 0139            return (false, $"Error al remover permiso: {ex.Message}");
 140        }
 3141    }
 142
 143    public async Task DeleteAsync(long id)
 144    {
 1145        await _rolRepository.DeleteAsync(id);
 1146    }
 147}
 148