| | | 1 | | using FAU.Entidades; |
| | | 2 | | using Microsoft.EntityFrameworkCore; |
| | | 3 | | |
| | | 4 | | namespace FAU.DataAccess.Repositories; |
| | | 5 | | |
| | | 6 | | public class TablaPermanenciaRepository : ITablaPermanenciaRepository |
| | | 7 | | { |
| | | 8 | | private readonly ApplicationDbContext _context; |
| | | 9 | | |
| | 10 | 10 | | public TablaPermanenciaRepository(ApplicationDbContext context) |
| | | 11 | | { |
| | 10 | 12 | | _context = context; |
| | 10 | 13 | | } |
| | | 14 | | |
| | | 15 | | public async Task<TablaPermanencia?> GetByIdAsync(long id) |
| | | 16 | | { |
| | 5 | 17 | | return await _context.TablaPermanencias |
| | 5 | 18 | | .AsNoTracking() |
| | 5 | 19 | | .Include(t => t.Escalafon) |
| | 5 | 20 | | .Include(t => t.Grado) |
| | 5 | 21 | | .FirstOrDefaultAsync(t => t.Id == id && t.Activo); |
| | 5 | 22 | | } |
| | | 23 | | |
| | | 24 | | public async Task<(IEnumerable<TablaPermanencia> Items, int TotalCount)> GetPagedAsync( |
| | | 25 | | int page, |
| | | 26 | | int pageSize, |
| | | 27 | | long? escalafonId = null, |
| | | 28 | | long? gradoId = null) |
| | | 29 | | { |
| | 1 | 30 | | var query = _context.TablaPermanencias |
| | 1 | 31 | | .AsNoTracking() |
| | 1 | 32 | | .Where(t => t.Activo) |
| | 1 | 33 | | .Include(t => t.Escalafon) |
| | 1 | 34 | | .Include(t => t.Grado) |
| | 1 | 35 | | .AsQueryable(); |
| | | 36 | | |
| | 1 | 37 | | if (escalafonId.HasValue) |
| | 1 | 38 | | query = query.Where(t => t.EscalafonId == escalafonId.Value); |
| | | 39 | | |
| | 1 | 40 | | if (gradoId.HasValue) |
| | 0 | 41 | | query = query.Where(t => t.GradoId == gradoId.Value); |
| | | 42 | | |
| | 1 | 43 | | var totalCount = await query.CountAsync(); |
| | | 44 | | |
| | 1 | 45 | | var items = await query |
| | 1 | 46 | | .OrderBy(t => t.EscalafonId) |
| | 1 | 47 | | .ThenBy(t => t.GradoId) |
| | 1 | 48 | | .ThenBy(t => t.AplicaRiesgoVuelo) |
| | 1 | 49 | | .ThenBy(t => t.MesesDesde) |
| | 1 | 50 | | .Skip((page - 1) * pageSize) |
| | 1 | 51 | | .Take(pageSize) |
| | 1 | 52 | | .ToListAsync(); |
| | | 53 | | |
| | 1 | 54 | | return (items, totalCount); |
| | 1 | 55 | | } |
| | | 56 | | |
| | | 57 | | public async Task<IEnumerable<TablaPermanencia>> GetAllAsync() |
| | | 58 | | { |
| | 2 | 59 | | return await _context.TablaPermanencias |
| | 2 | 60 | | .AsNoTracking() |
| | 2 | 61 | | .Where(t => t.Activo) |
| | 2 | 62 | | .OrderBy(t => t.EscalafonId) |
| | 2 | 63 | | .ThenBy(t => t.GradoId) |
| | 2 | 64 | | .ThenBy(t => t.AplicaRiesgoVuelo) |
| | 2 | 65 | | .ThenBy(t => t.MesesDesde) |
| | 2 | 66 | | .ToListAsync(); |
| | 2 | 67 | | } |
| | | 68 | | |
| | | 69 | | public async Task<IEnumerable<TablaPermanencia>> GetAllWithIncludesAsync() |
| | | 70 | | { |
| | 0 | 71 | | return await _context.TablaPermanencias |
| | 0 | 72 | | .AsNoTracking() |
| | 0 | 73 | | .Where(t => t.Activo) |
| | 0 | 74 | | .Include(t => t.Escalafon) |
| | 0 | 75 | | .Include(t => t.Grado) |
| | 0 | 76 | | .OrderBy(t => t.EscalafonId) |
| | 0 | 77 | | .ThenBy(t => t.GradoId) |
| | 0 | 78 | | .ThenBy(t => t.AplicaRiesgoVuelo) |
| | 0 | 79 | | .ThenBy(t => t.MesesDesde) |
| | 0 | 80 | | .ToListAsync(); |
| | 0 | 81 | | } |
| | | 82 | | |
| | | 83 | | public async Task<TablaPermanencia> CreateAsync(TablaPermanencia tablaPermanencia) |
| | | 84 | | { |
| | 11 | 85 | | _context.TablaPermanencias.Add(tablaPermanencia); |
| | 11 | 86 | | await _context.SaveChangesAsync(); |
| | 11 | 87 | | return tablaPermanencia; |
| | 11 | 88 | | } |
| | | 89 | | |
| | | 90 | | public async Task<TablaPermanencia> UpdateAsync(TablaPermanencia tablaPermanencia) |
| | | 91 | | { |
| | | 92 | | // Si ya existe una instancia trackeada con el mismo Id, actualizamos sus valores |
| | | 93 | | // en vez de intentar añadir una nueva (evita IdentityConflict). |
| | 1 | 94 | | var tracked = _context.ChangeTracker.Entries<TablaPermanencia>() |
| | 2 | 95 | | .FirstOrDefault(e => e.Entity.Id == tablaPermanencia.Id); |
| | 1 | 96 | | if (tracked is not null) |
| | 1 | 97 | | tracked.CurrentValues.SetValues(tablaPermanencia); |
| | | 98 | | else |
| | 0 | 99 | | _context.TablaPermanencias.Update(tablaPermanencia); |
| | 1 | 100 | | await _context.SaveChangesAsync(); |
| | 1 | 101 | | return tablaPermanencia; |
| | 1 | 102 | | } |
| | | 103 | | |
| | | 104 | | public async Task DeleteAsync(long id) |
| | | 105 | | { |
| | 3 | 106 | | var entidad = await _context.TablaPermanencias.FindAsync(id); |
| | 3 | 107 | | if (entidad != null) |
| | | 108 | | { |
| | 3 | 109 | | entidad.Activo = false; |
| | 3 | 110 | | await _context.SaveChangesAsync(); |
| | | 111 | | } |
| | 3 | 112 | | } |
| | | 113 | | |
| | | 114 | | public async Task<bool> ExistsOverlapAsync( |
| | | 115 | | long escalafonId, |
| | | 116 | | long gradoId, |
| | | 117 | | short mesesDesde, |
| | | 118 | | short mesesHasta, |
| | | 119 | | bool aplicaRiesgoVuelo, |
| | | 120 | | long? excludingId = null) |
| | | 121 | | { |
| | 6 | 122 | | var query = _context.TablaPermanencias |
| | 6 | 123 | | .Where(t => t.Activo && |
| | 6 | 124 | | t.EscalafonId == escalafonId && |
| | 6 | 125 | | t.GradoId == gradoId && |
| | 6 | 126 | | t.AplicaRiesgoVuelo == aplicaRiesgoVuelo && |
| | 6 | 127 | | t.MesesDesde <= mesesHasta && |
| | 6 | 128 | | t.MesesHasta >= mesesDesde); |
| | | 129 | | |
| | 6 | 130 | | if (excludingId.HasValue) |
| | 1 | 131 | | query = query.Where(t => t.Id != excludingId.Value); |
| | | 132 | | |
| | 6 | 133 | | return await query.AnyAsync(); |
| | 6 | 134 | | } |
| | | 135 | | |
| | | 136 | | public async Task<bool> ExistsByIdAsync(long id) |
| | | 137 | | { |
| | 0 | 138 | | return await _context.TablaPermanencias.AnyAsync(t => t.Id == id && t.Activo); |
| | 0 | 139 | | } |
| | | 140 | | } |