| | | 1 | | using FAU.Entidades; |
| | | 2 | | using Microsoft.EntityFrameworkCore; |
| | | 3 | | |
| | | 4 | | namespace FAU.DataAccess.Repositories; |
| | | 5 | | |
| | | 6 | | public class UnidadRepository : IUnidadRepository |
| | | 7 | | { |
| | | 8 | | private readonly ApplicationDbContext _context; |
| | | 9 | | |
| | 9 | 10 | | public UnidadRepository(ApplicationDbContext context) |
| | | 11 | | { |
| | 9 | 12 | | _context = context; |
| | 9 | 13 | | } |
| | | 14 | | |
| | | 15 | | public async Task<IEnumerable<Unidad>> ObtenerTodosConSubUnidadesAsync() |
| | | 16 | | { |
| | 2 | 17 | | return await _context.Unidades |
| | 2 | 18 | | .Where(u => u.Vigente) |
| | 2 | 19 | | .Include(u => u.SubUnidades.Where(s => s.Vigente)) |
| | 2 | 20 | | .OrderBy(u => u.Denominacion) |
| | 2 | 21 | | .ToListAsync(); |
| | 2 | 22 | | } |
| | | 23 | | |
| | | 24 | | public async Task<Unidad?> ObtenerPorIdAsync(long id) |
| | | 25 | | { |
| | 3 | 26 | | return await _context.Unidades |
| | 3 | 27 | | .Include(u => u.SubUnidades.Where(s => s.Vigente)) |
| | 3 | 28 | | .FirstOrDefaultAsync(u => u.Id == id); |
| | 3 | 29 | | } |
| | | 30 | | |
| | | 31 | | public async Task<Unidad> CrearAsync(Unidad unidad) |
| | | 32 | | { |
| | 8 | 33 | | _context.Unidades.Add(unidad); |
| | 8 | 34 | | await _context.SaveChangesAsync(); |
| | 8 | 35 | | return unidad; |
| | 8 | 36 | | } |
| | | 37 | | |
| | | 38 | | public async Task<Unidad> ActualizarAsync(Unidad unidad) |
| | | 39 | | { |
| | 1 | 40 | | _context.Unidades.Update(unidad); |
| | 1 | 41 | | await _context.SaveChangesAsync(); |
| | 1 | 42 | | return unidad; |
| | 1 | 43 | | } |
| | | 44 | | |
| | | 45 | | public async Task<bool> ExisteCodigoAsync(string codigo) |
| | | 46 | | { |
| | 2 | 47 | | return await _context.Unidades.AnyAsync(u => u.Codigo == codigo); |
| | 2 | 48 | | } |
| | | 49 | | |
| | | 50 | | public async Task<bool> ExistePorIdAsync(long id) |
| | | 51 | | { |
| | 1 | 52 | | return await _context.Unidades.AnyAsync(u => u.Id == id); |
| | 1 | 53 | | } |
| | | 54 | | } |