| | | 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 IAuditoriaService _auditoriaService; |
| | | 10 | | private readonly ICurrentUserContext _currentUser; |
| | | 11 | | |
| | 12 | 12 | | public RolService(IRolRepository rolRepository, IAuditoriaService auditoriaService, ICurrentUserContext currentUser) |
| | | 13 | | { |
| | 12 | 14 | | _rolRepository = rolRepository; |
| | 12 | 15 | | _auditoriaService = auditoriaService; |
| | 12 | 16 | | _currentUser = currentUser; |
| | 12 | 17 | | } |
| | | 18 | | |
| | | 19 | | public async Task<Rol?> GetByIdAsync(long id) |
| | | 20 | | { |
| | 2 | 21 | | return await _rolRepository.GetByIdAsync(id); |
| | 2 | 22 | | } |
| | | 23 | | |
| | | 24 | | public async Task<Rol?> GetByNombreAsync(string nombre) |
| | | 25 | | { |
| | 2 | 26 | | return await _rolRepository.GetByNombreAsync(nombre); |
| | 2 | 27 | | } |
| | | 28 | | |
| | | 29 | | public async Task<IEnumerable<Rol>> GetAllAsync() |
| | | 30 | | { |
| | 2 | 31 | | return await _rolRepository.GetAllAsync(); |
| | 2 | 32 | | } |
| | | 33 | | |
| | | 34 | | public async Task<(bool Success, Rol? Rol, string? Error)> CreateAsync(string nombre, string? descripcion = null) |
| | | 35 | | { |
| | | 36 | | try |
| | | 37 | | { |
| | 2 | 38 | | if (await _rolRepository.ExistsAsync(nombre)) |
| | | 39 | | { |
| | 1 | 40 | | return (false, null, "Ya existe un rol con ese nombre"); |
| | | 41 | | } |
| | | 42 | | |
| | 1 | 43 | | var nuevoRol = new Rol |
| | 1 | 44 | | { |
| | 1 | 45 | | Nombre = nombre, |
| | 1 | 46 | | Descripcion = descripcion |
| | 1 | 47 | | }; |
| | | 48 | | |
| | 1 | 49 | | var rolCreado = await _rolRepository.CreateAsync(nuevoRol); |
| | 1 | 50 | | await _auditoriaService.LogAuditoriaAsync(_currentUser.UserId ?? 0, AccionEnum.CrearRol, ContextoEnum.Roles, |
| | 1 | 51 | | return (true, rolCreado, null); |
| | | 52 | | } |
| | 0 | 53 | | catch (Exception ex) |
| | | 54 | | { |
| | 0 | 55 | | return (false, null, $"Error al crear rol: {ex.Message}"); |
| | | 56 | | } |
| | 2 | 57 | | } |
| | | 58 | | |
| | | 59 | | public async Task<(bool Success, Rol? Rol, string? Error)> UpdateAsync(long id, string nombre, string? descripcion = |
| | | 60 | | { |
| | | 61 | | try |
| | | 62 | | { |
| | 3 | 63 | | var rol = await _rolRepository.GetByIdAsync(id); |
| | 3 | 64 | | if (rol == null) |
| | | 65 | | { |
| | 1 | 66 | | return (false, null, "Rol no encontrado"); |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | // Verificar si el nuevo nombre ya existe (si es diferente al actual) |
| | 2 | 70 | | if (rol.Nombre != nombre && await _rolRepository.ExistsAsync(nombre)) |
| | | 71 | | { |
| | 1 | 72 | | return (false, null, "Ya existe un rol con ese nombre"); |
| | | 73 | | } |
| | | 74 | | |
| | 1 | 75 | | rol.Nombre = nombre; |
| | 1 | 76 | | rol.Descripcion = descripcion; |
| | | 77 | | |
| | 1 | 78 | | var rolActualizado = await _rolRepository.UpdateAsync(rol); |
| | 1 | 79 | | await _auditoriaService.LogAuditoriaAsync(_currentUser.UserId ?? 0, AccionEnum.ActualizarRol, ContextoEnum.R |
| | 1 | 80 | | return (true, rolActualizado, null); |
| | | 81 | | } |
| | 0 | 82 | | catch (Exception ex) |
| | | 83 | | { |
| | 0 | 84 | | return (false, null, $"Error al actualizar rol: {ex.Message}"); |
| | | 85 | | } |
| | 3 | 86 | | } |
| | | 87 | | |
| | | 88 | | public async Task DeleteAsync(long id) |
| | | 89 | | { |
| | 1 | 90 | | await _auditoriaService.LogAuditoriaAsync(_currentUser.UserId ?? 0, AccionEnum.ActualizarRol, ContextoEnum.Roles |
| | 1 | 91 | | await _rolRepository.DeleteAsync(id); |
| | 1 | 92 | | } |
| | | 93 | | } |
| | | 94 | | |