| | | 1 | | using FAU.Entidades; |
| | | 2 | | using Microsoft.EntityFrameworkCore; |
| | | 3 | | |
| | | 4 | | namespace FAU.DataAccess.Repositories; |
| | | 5 | | |
| | | 6 | | public class DependienteRepository : IDependienteRepository |
| | | 7 | | { |
| | | 8 | | private readonly ApplicationDbContext _context; |
| | | 9 | | |
| | 15 | 10 | | public DependienteRepository(ApplicationDbContext context) |
| | | 11 | | { |
| | 15 | 12 | | _context = context; |
| | 15 | 13 | | } |
| | | 14 | | |
| | | 15 | | public async Task<(IEnumerable<Dependiente> Items, int TotalCount)> GetDependientesPagedAsync( |
| | | 16 | | int page, int pageSize, long? personaId = null, string? tipo = null, bool? activo = null) |
| | | 17 | | { |
| | 5 | 18 | | var query = _context.Dependientes |
| | 5 | 19 | | .Include(d => d.Persona) |
| | 5 | 20 | | .AsQueryable(); |
| | | 21 | | |
| | 5 | 22 | | if (personaId.HasValue) |
| | 1 | 23 | | query = query.Where(d => d.PersonaId == personaId.Value); |
| | | 24 | | |
| | 5 | 25 | | if (!string.IsNullOrWhiteSpace(tipo)) |
| | 1 | 26 | | query = query.Where(d => d.Tipo == tipo); |
| | | 27 | | |
| | 5 | 28 | | if (activo.HasValue) |
| | 1 | 29 | | query = query.Where(d => d.Activo == activo.Value); |
| | | 30 | | |
| | 5 | 31 | | var totalCount = await query.CountAsync(); |
| | 5 | 32 | | var items = await query |
| | 5 | 33 | | .OrderBy(d => d.PersonaId) |
| | 5 | 34 | | .ThenBy(d => d.Tipo) |
| | 5 | 35 | | .Skip((page - 1) * pageSize) |
| | 5 | 36 | | .Take(pageSize) |
| | 5 | 37 | | .ToListAsync(); |
| | | 38 | | |
| | 5 | 39 | | return (items, totalCount); |
| | 5 | 40 | | } |
| | | 41 | | |
| | | 42 | | public async Task<IEnumerable<Dependiente>> GetDependientesByPersonaAsync(long personaId) |
| | | 43 | | { |
| | 2 | 44 | | return await _context.Dependientes |
| | 2 | 45 | | .Include(d => d.Persona) |
| | 2 | 46 | | .Where(d => d.PersonaId == personaId) |
| | 2 | 47 | | .OrderBy(d => d.Tipo) |
| | 2 | 48 | | .ThenBy(d => d.Nombre) |
| | 2 | 49 | | .ToListAsync(); |
| | 2 | 50 | | } |
| | | 51 | | |
| | | 52 | | public async Task<Dependiente?> GetDependienteByIdAsync(long id) |
| | | 53 | | { |
| | 3 | 54 | | return await _context.Dependientes |
| | 3 | 55 | | .Include(d => d.Persona) |
| | 3 | 56 | | .FirstOrDefaultAsync(d => d.Id == id); |
| | 3 | 57 | | } |
| | | 58 | | |
| | | 59 | | public async Task<bool> ExisteConyugeActivoAsync(long personaId) |
| | | 60 | | { |
| | 4 | 61 | | return await _context.Dependientes |
| | 4 | 62 | | .AnyAsync(d => d.PersonaId == personaId && d.Tipo == "conyuge_sin_ingresos" && d.Activo); |
| | 4 | 63 | | } |
| | | 64 | | |
| | | 65 | | public async Task<Dependiente> CreateDependienteAsync(Dependiente dependiente) |
| | | 66 | | { |
| | 3 | 67 | | _context.Dependientes.Add(dependiente); |
| | 3 | 68 | | await _context.SaveChangesAsync(); |
| | 3 | 69 | | return dependiente; |
| | 3 | 70 | | } |
| | | 71 | | |
| | | 72 | | public async Task<Dependiente> UpdateDependienteAsync(Dependiente dependiente) |
| | | 73 | | { |
| | 1 | 74 | | _context.Dependientes.Update(dependiente); |
| | 1 | 75 | | await _context.SaveChangesAsync(); |
| | 1 | 76 | | return dependiente; |
| | 1 | 77 | | } |
| | | 78 | | } |