| | | 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 | | |
| | 11 | 10 | | public RolRepository(ApplicationDbContext context) |
| | | 11 | | { |
| | 11 | 12 | | _context = context; |
| | 11 | 13 | | } |
| | | 14 | | |
| | | 15 | | public async Task<Rol?> GetByIdAsync(long id) |
| | | 16 | | { |
| | 2 | 17 | | return await _context.Roles |
| | 2 | 18 | | .FirstOrDefaultAsync(r => r.Id == id); |
| | 2 | 19 | | } |
| | | 20 | | |
| | | 21 | | public async Task<Rol?> GetByNombreAsync(string nombre) |
| | | 22 | | { |
| | 2 | 23 | | return await _context.Roles |
| | 2 | 24 | | .FirstOrDefaultAsync(r => r.Nombre == nombre); |
| | 2 | 25 | | } |
| | | 26 | | |
| | | 27 | | public async Task<IEnumerable<Rol>> GetAllAsync() |
| | | 28 | | { |
| | 2 | 29 | | return await _context.Roles |
| | 2 | 30 | | .ToListAsync(); |
| | 2 | 31 | | } |
| | | 32 | | |
| | | 33 | | public async Task<Rol> CreateAsync(Rol rol) |
| | | 34 | | { |
| | 1 | 35 | | _context.Roles.Add(rol); |
| | 1 | 36 | | await _context.SaveChangesAsync(); |
| | 1 | 37 | | return rol; |
| | 1 | 38 | | } |
| | | 39 | | |
| | | 40 | | public async Task<Rol> UpdateAsync(Rol rol) |
| | | 41 | | { |
| | 1 | 42 | | _context.Roles.Update(rol); |
| | 1 | 43 | | await _context.SaveChangesAsync(); |
| | 1 | 44 | | return rol; |
| | 1 | 45 | | } |
| | | 46 | | |
| | | 47 | | public async Task DeleteAsync(long id) |
| | | 48 | | { |
| | 1 | 49 | | var rol = await _context.Roles.FindAsync(id); |
| | 1 | 50 | | if (rol != null) |
| | | 51 | | { |
| | 1 | 52 | | _context.Roles.Remove(rol); |
| | 1 | 53 | | await _context.SaveChangesAsync(); |
| | | 54 | | } |
| | 1 | 55 | | } |
| | | 56 | | |
| | | 57 | | public async Task<bool> ExistsAsync(string nombre) |
| | | 58 | | { |
| | 2 | 59 | | return await _context.Roles.AnyAsync(r => r.Nombre == nombre); |
| | 2 | 60 | | } |
| | | 61 | | } |
| | | 62 | | |