| | | 1 | | using FAU.Entidades; |
| | | 2 | | using Microsoft.EntityFrameworkCore; |
| | | 3 | | |
| | | 4 | | namespace FAU.DataAccess.Repositories; |
| | | 5 | | |
| | | 6 | | public class Form3100Repository : IForm3100Repository |
| | | 7 | | { |
| | | 8 | | private readonly ApplicationDbContext _context; |
| | | 9 | | |
| | 0 | 10 | | public Form3100Repository(ApplicationDbContext context) => _context = context; |
| | | 11 | | |
| | | 12 | | public async Task<Form3100?> GetByIdAsync(long id) => |
| | 0 | 13 | | await _context.Form3100 |
| | 0 | 14 | | .Include(f => f.PersonasACargo) |
| | 0 | 15 | | .Include(f => f.Documento) |
| | 0 | 16 | | .FirstOrDefaultAsync(f => f.Id == id); |
| | | 17 | | |
| | | 18 | | public async Task<Form3100?> GetVigenteParaPeriodoAsync(long personaId, int anio, int mes) |
| | | 19 | | { |
| | 0 | 20 | | return await _context.Form3100 |
| | 0 | 21 | | .Include(f => f.PersonasACargo) |
| | 0 | 22 | | .Include(f => f.Documento) |
| | 0 | 23 | | .Where(f => f.PersonaId == personaId && |
| | 0 | 24 | | (f.VigenteDesde_Anio < anio || |
| | 0 | 25 | | (f.VigenteDesde_Anio == anio && f.VigenteDesde_Mes <= mes))) |
| | 0 | 26 | | .OrderByDescending(f => f.VigenteDesde_Anio) |
| | 0 | 27 | | .ThenByDescending(f => f.VigenteDesde_Mes) |
| | 0 | 28 | | .FirstOrDefaultAsync(); |
| | 0 | 29 | | } |
| | | 30 | | |
| | | 31 | | public async Task<IEnumerable<Form3100>> GetHistorialAsync(long personaId, int? anio = null, int? mes = null) |
| | | 32 | | { |
| | 0 | 33 | | var query = _context.Form3100 |
| | 0 | 34 | | .Include(f => f.PersonasACargo) |
| | 0 | 35 | | .Where(f => f.PersonaId == personaId); |
| | | 36 | | |
| | 0 | 37 | | if (anio.HasValue) query = query.Where(f => f.VigenteDesde_Anio == anio.Value); |
| | 0 | 38 | | if (mes.HasValue) query = query.Where(f => f.VigenteDesde_Mes == mes.Value); |
| | | 39 | | |
| | 0 | 40 | | return await query |
| | 0 | 41 | | .OrderByDescending(f => f.VigenteDesde_Anio) |
| | 0 | 42 | | .ThenByDescending(f => f.VigenteDesde_Mes) |
| | 0 | 43 | | .ToListAsync(); |
| | 0 | 44 | | } |
| | | 45 | | |
| | | 46 | | public async Task<bool> ExisteVigenciaAsync(long personaId, int anio, int mes, long? excludeId = null) |
| | | 47 | | { |
| | 0 | 48 | | var query = _context.Form3100.Where(f => |
| | 0 | 49 | | f.PersonaId == personaId && |
| | 0 | 50 | | f.VigenteDesde_Anio == anio && |
| | 0 | 51 | | f.VigenteDesde_Mes == mes); |
| | | 52 | | |
| | 0 | 53 | | if (excludeId.HasValue) query = query.Where(f => f.Id != excludeId.Value); |
| | 0 | 54 | | return await query.AnyAsync(); |
| | 0 | 55 | | } |
| | | 56 | | |
| | | 57 | | public async Task<Form3100> CreateAsync(Form3100 form) |
| | | 58 | | { |
| | 0 | 59 | | _context.Form3100.Add(form); |
| | 0 | 60 | | await _context.SaveChangesAsync(); |
| | 0 | 61 | | return form; |
| | 0 | 62 | | } |
| | | 63 | | |
| | | 64 | | public async Task<Form3100PersonaACargo> AddPersonaACargoAsync(Form3100PersonaACargo persona) |
| | | 65 | | { |
| | 0 | 66 | | _context.Form3100PersonasACargo.Add(persona); |
| | 0 | 67 | | await _context.SaveChangesAsync(); |
| | 0 | 68 | | return persona; |
| | 0 | 69 | | } |
| | | 70 | | |
| | | 71 | | public async Task<Form3100PersonaACargo?> GetPersonaACargoAsync(long id) => |
| | 0 | 72 | | await _context.Form3100PersonasACargo.FindAsync(id); |
| | | 73 | | |
| | | 74 | | public async Task DeletePersonaACargoAsync(long id) |
| | | 75 | | { |
| | 0 | 76 | | var p = await _context.Form3100PersonasACargo.FindAsync(id); |
| | 0 | 77 | | if (p != null) |
| | | 78 | | { |
| | 0 | 79 | | _context.Form3100PersonasACargo.Remove(p); |
| | 0 | 80 | | await _context.SaveChangesAsync(); |
| | | 81 | | } |
| | 0 | 82 | | } |
| | | 83 | | |
| | | 84 | | public async Task<Form3100> UpdateAsync(Form3100 form) |
| | | 85 | | { |
| | 0 | 86 | | _context.Form3100.Update(form); |
| | 0 | 87 | | await _context.SaveChangesAsync(); |
| | 0 | 88 | | return form; |
| | 0 | 89 | | } |
| | | 90 | | } |