| | | 1 | | using FAU.DataAccess.Repositories; |
| | | 2 | | using FAU.Entidades; |
| | | 3 | | |
| | | 4 | | namespace FAU.Logica.Services; |
| | | 5 | | |
| | | 6 | | public class RolService : IRolService |
| | | 7 | | { |
| | | 8 | | private readonly IRolRepository _rolRepository; |
| | | 9 | | private readonly IPermisoRepository _permisoRepository; |
| | | 10 | | |
| | 19 | 11 | | public RolService(IRolRepository rolRepository, IPermisoRepository permisoRepository) |
| | | 12 | | { |
| | 19 | 13 | | _rolRepository = rolRepository; |
| | 19 | 14 | | _permisoRepository = permisoRepository; |
| | 19 | 15 | | } |
| | | 16 | | |
| | | 17 | | public async Task<Rol?> GetByIdAsync(long id) |
| | | 18 | | { |
| | 2 | 19 | | return await _rolRepository.GetByIdAsync(id); |
| | 2 | 20 | | } |
| | | 21 | | |
| | | 22 | | public async Task<Rol?> GetByNombreAsync(string nombre) |
| | | 23 | | { |
| | 2 | 24 | | return await _rolRepository.GetByNombreAsync(nombre); |
| | 2 | 25 | | } |
| | | 26 | | |
| | | 27 | | public async Task<IEnumerable<Rol>> GetAllAsync() |
| | | 28 | | { |
| | 2 | 29 | | return await _rolRepository.GetAllAsync(); |
| | 2 | 30 | | } |
| | | 31 | | |
| | | 32 | | public async Task<(bool Success, Rol? Rol, string? Error)> CreateAsync(string nombre, string? descripcion = null) |
| | | 33 | | { |
| | | 34 | | try |
| | | 35 | | { |
| | 2 | 36 | | if (await _rolRepository.ExistsAsync(nombre)) |
| | | 37 | | { |
| | 1 | 38 | | return (false, null, "Ya existe un rol con ese nombre"); |
| | | 39 | | } |
| | | 40 | | |
| | 1 | 41 | | var nuevoRol = new Rol |
| | 1 | 42 | | { |
| | 1 | 43 | | Nombre = nombre, |
| | 1 | 44 | | Descripcion = descripcion |
| | 1 | 45 | | }; |
| | | 46 | | |
| | 1 | 47 | | var rolCreado = await _rolRepository.CreateAsync(nuevoRol); |
| | 1 | 48 | | return (true, rolCreado, null); |
| | | 49 | | } |
| | 0 | 50 | | catch (Exception ex) |
| | | 51 | | { |
| | 0 | 52 | | return (false, null, $"Error al crear rol: {ex.Message}"); |
| | | 53 | | } |
| | 2 | 54 | | } |
| | | 55 | | |
| | | 56 | | public async Task<(bool Success, Rol? Rol, string? Error)> UpdateAsync(long id, string nombre, string? descripcion = |
| | | 57 | | { |
| | | 58 | | try |
| | | 59 | | { |
| | 3 | 60 | | var rol = await _rolRepository.GetByIdAsync(id); |
| | 3 | 61 | | if (rol == null) |
| | | 62 | | { |
| | 1 | 63 | | return (false, null, "Rol no encontrado"); |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | // Verificar si el nuevo nombre ya existe (si es diferente al actual) |
| | 2 | 67 | | if (rol.Nombre != nombre && await _rolRepository.ExistsAsync(nombre)) |
| | | 68 | | { |
| | 1 | 69 | | return (false, null, "Ya existe un rol con ese nombre"); |
| | | 70 | | } |
| | | 71 | | |
| | 1 | 72 | | rol.Nombre = nombre; |
| | 1 | 73 | | rol.Descripcion = descripcion; |
| | | 74 | | |
| | 1 | 75 | | var rolActualizado = await _rolRepository.UpdateAsync(rol); |
| | 1 | 76 | | return (true, rolActualizado, null); |
| | | 77 | | } |
| | 0 | 78 | | catch (Exception ex) |
| | | 79 | | { |
| | 0 | 80 | | return (false, null, $"Error al actualizar rol: {ex.Message}"); |
| | | 81 | | } |
| | 3 | 82 | | } |
| | | 83 | | |
| | | 84 | | public async Task<(bool Success, string? Error)> AsignarPermisoAsync(long rolId, long permisoId) |
| | | 85 | | { |
| | | 86 | | try |
| | | 87 | | { |
| | 4 | 88 | | var rol = await _rolRepository.GetByIdAsync(rolId); |
| | 4 | 89 | | if (rol == null) |
| | | 90 | | { |
| | 1 | 91 | | return (false, "Rol no encontrado"); |
| | | 92 | | } |
| | | 93 | | |
| | 3 | 94 | | var permiso = await _permisoRepository.GetByIdAsync(permisoId); |
| | 3 | 95 | | if (permiso == null) |
| | | 96 | | { |
| | 1 | 97 | | return (false, "Permiso no encontrado"); |
| | | 98 | | } |
| | | 99 | | |
| | 3 | 100 | | if (rol.Permisos.Any(p => p.Id == permisoId)) |
| | | 101 | | { |
| | 1 | 102 | | return (false, "El rol ya tiene este permiso asignado"); |
| | | 103 | | } |
| | | 104 | | |
| | 1 | 105 | | rol.Permisos.Add(permiso); |
| | 1 | 106 | | await _rolRepository.UpdateAsync(rol); |
| | | 107 | | |
| | 1 | 108 | | return (true, null); |
| | | 109 | | } |
| | 0 | 110 | | catch (Exception ex) |
| | | 111 | | { |
| | 0 | 112 | | return (false, $"Error al asignar permiso: {ex.Message}"); |
| | | 113 | | } |
| | 4 | 114 | | } |
| | | 115 | | |
| | | 116 | | public async Task<(bool Success, string? Error)> RemoverPermisoAsync(long rolId, long permisoId) |
| | | 117 | | { |
| | | 118 | | try |
| | | 119 | | { |
| | 3 | 120 | | var rol = await _rolRepository.GetByIdAsync(rolId); |
| | 3 | 121 | | if (rol == null) |
| | | 122 | | { |
| | 1 | 123 | | return (false, "Rol no encontrado"); |
| | | 124 | | } |
| | | 125 | | |
| | 3 | 126 | | var permiso = rol.Permisos.FirstOrDefault(p => p.Id == permisoId); |
| | 2 | 127 | | if (permiso == null) |
| | | 128 | | { |
| | 1 | 129 | | return (false, "El rol no tiene este permiso asignado"); |
| | | 130 | | } |
| | | 131 | | |
| | 1 | 132 | | rol.Permisos.Remove(permiso); |
| | 1 | 133 | | await _rolRepository.UpdateAsync(rol); |
| | | 134 | | |
| | 1 | 135 | | return (true, null); |
| | | 136 | | } |
| | 0 | 137 | | catch (Exception ex) |
| | | 138 | | { |
| | 0 | 139 | | return (false, $"Error al remover permiso: {ex.Message}"); |
| | | 140 | | } |
| | 3 | 141 | | } |
| | | 142 | | |
| | | 143 | | public async Task DeleteAsync(long id) |
| | | 144 | | { |
| | 1 | 145 | | await _rolRepository.DeleteAsync(id); |
| | 1 | 146 | | } |
| | | 147 | | } |
| | | 148 | | |