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