| | | 1 | | using FAU.Entidades; |
| | | 2 | | using Microsoft.EntityFrameworkCore; |
| | | 3 | | |
| | | 4 | | namespace FAU.DataAccess.Repositories; |
| | | 5 | | |
| | | 6 | | public class RolRepository : IRolRepository |
| | | 7 | | { |
| | | 8 | | private readonly ApplicationDbContext _context; |
| | | 9 | | |
| | 9 | 10 | | public RolRepository(ApplicationDbContext context) |
| | | 11 | | { |
| | 9 | 12 | | _context = context; |
| | 9 | 13 | | } |
| | | 14 | | |
| | | 15 | | public async Task<Rol?> GetByIdAsync(long id) |
| | | 16 | | { |
| | 2 | 17 | | return await _context.Roles |
| | 2 | 18 | | .Include(r => r.Permisos) |
| | 2 | 19 | | .FirstOrDefaultAsync(r => r.Id == id); |
| | 2 | 20 | | } |
| | | 21 | | |
| | | 22 | | public async Task<Rol?> GetByNombreAsync(string nombre) |
| | | 23 | | { |
| | 0 | 24 | | return await _context.Roles |
| | 0 | 25 | | .FirstOrDefaultAsync(r => r.Nombre == nombre); |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | public async Task<Rol?> GetByNombreWithPermisosAsync(string nombre) |
| | | 29 | | { |
| | 0 | 30 | | return await _context.Roles |
| | 0 | 31 | | .Include(r => r.Permisos) |
| | 0 | 32 | | .FirstOrDefaultAsync(r => r.Nombre == nombre); |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | public async Task<IEnumerable<Rol>> GetAllAsync() |
| | | 36 | | { |
| | 2 | 37 | | return await _context.Roles |
| | 2 | 38 | | .Include(r => r.Permisos) |
| | 2 | 39 | | .ToListAsync(); |
| | 2 | 40 | | } |
| | | 41 | | |
| | | 42 | | public async Task<Rol> CreateAsync(Rol rol) |
| | | 43 | | { |
| | 1 | 44 | | _context.Roles.Add(rol); |
| | 1 | 45 | | await _context.SaveChangesAsync(); |
| | 1 | 46 | | return rol; |
| | 1 | 47 | | } |
| | | 48 | | |
| | | 49 | | public async Task<Rol> UpdateAsync(Rol rol) |
| | | 50 | | { |
| | 1 | 51 | | _context.Roles.Update(rol); |
| | 1 | 52 | | await _context.SaveChangesAsync(); |
| | 1 | 53 | | return rol; |
| | 1 | 54 | | } |
| | | 55 | | |
| | | 56 | | public async Task DeleteAsync(long id) |
| | | 57 | | { |
| | 1 | 58 | | var rol = await _context.Roles.FindAsync(id); |
| | 1 | 59 | | if (rol != null) |
| | | 60 | | { |
| | 1 | 61 | | _context.Roles.Remove(rol); |
| | 1 | 62 | | await _context.SaveChangesAsync(); |
| | | 63 | | } |
| | 1 | 64 | | } |
| | | 65 | | |
| | | 66 | | public async Task<bool> ExistsAsync(string nombre) |
| | | 67 | | { |
| | 2 | 68 | | return await _context.Roles.AnyAsync(r => r.Nombre == nombre); |
| | 2 | 69 | | } |
| | | 70 | | } |
| | | 71 | | |