| | | 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 | | { |
| | 6 | 17 | | return await _context.TablaPermanencias |
| | 6 | 18 | | .FirstOrDefaultAsync(t => t.Id == id && t.Activo); |
| | 6 | 19 | | } |
| | | 20 | | |
| | | 21 | | public async Task<(IEnumerable<TablaPermanencia> Items, int TotalCount)> GetPagedAsync( |
| | | 22 | | int page, |
| | | 23 | | int pageSize, |
| | | 24 | | long? escalafonId = null) |
| | | 25 | | { |
| | 1 | 26 | | var query = _context.TablaPermanencias |
| | 1 | 27 | | .Where(t => t.Activo) |
| | 1 | 28 | | .AsQueryable(); |
| | | 29 | | |
| | 1 | 30 | | if (escalafonId.HasValue) |
| | | 31 | | { |
| | 1 | 32 | | query = query.Where(t => t.EscalafonId == escalafonId.Value); |
| | | 33 | | } |
| | | 34 | | |
| | 1 | 35 | | var totalCount = await query.CountAsync(); |
| | | 36 | | |
| | 1 | 37 | | var items = await query |
| | 1 | 38 | | .OrderBy(t => t.EscalafonId) |
| | 1 | 39 | | .ThenBy(t => t.AniosDesde) |
| | 1 | 40 | | .ThenBy(t => t.AniosHasta) |
| | 1 | 41 | | .Skip((page - 1) * pageSize) |
| | 1 | 42 | | .Take(pageSize) |
| | 1 | 43 | | .ToListAsync(); |
| | | 44 | | |
| | 1 | 45 | | return (items, totalCount); |
| | 1 | 46 | | } |
| | | 47 | | |
| | | 48 | | public async Task<IEnumerable<TablaPermanencia>> GetAllAsync() |
| | | 49 | | { |
| | 2 | 50 | | return await _context.TablaPermanencias |
| | 2 | 51 | | .Where(t => t.Activo) |
| | 2 | 52 | | .OrderBy(t => t.EscalafonId) |
| | 2 | 53 | | .ThenBy(t => t.AniosDesde) |
| | 2 | 54 | | .ThenBy(t => t.AniosHasta) |
| | 2 | 55 | | .ToListAsync(); |
| | 2 | 56 | | } |
| | | 57 | | |
| | | 58 | | public async Task<TablaPermanencia> CreateAsync(TablaPermanencia tablaPermanencia) |
| | | 59 | | { |
| | 11 | 60 | | _context.TablaPermanencias.Add(tablaPermanencia); |
| | 11 | 61 | | await _context.SaveChangesAsync(); |
| | 11 | 62 | | return tablaPermanencia; |
| | 11 | 63 | | } |
| | | 64 | | |
| | | 65 | | public async Task<TablaPermanencia> UpdateAsync(TablaPermanencia tablaPermanencia) |
| | | 66 | | { |
| | 1 | 67 | | _context.TablaPermanencias.Update(tablaPermanencia); |
| | 1 | 68 | | await _context.SaveChangesAsync(); |
| | 1 | 69 | | return tablaPermanencia; |
| | 1 | 70 | | } |
| | | 71 | | |
| | | 72 | | public async Task DeleteAsync(long id) |
| | | 73 | | { |
| | 4 | 74 | | var entidad = await _context.TablaPermanencias.FindAsync(id); |
| | 4 | 75 | | if (entidad != null) |
| | | 76 | | { |
| | 4 | 77 | | entidad.Activo = false; |
| | 4 | 78 | | await _context.SaveChangesAsync(); |
| | | 79 | | } |
| | 4 | 80 | | } |
| | | 81 | | |
| | | 82 | | public async Task<bool> ExistsOverlapAsync(long escalafonId, short aniosDesde, short aniosHasta, long? excludingId = |
| | | 83 | | { |
| | 5 | 84 | | var query = _context.TablaPermanencias |
| | 5 | 85 | | .Where(t => t.Activo && |
| | 5 | 86 | | t.EscalafonId == escalafonId && |
| | 5 | 87 | | t.AniosDesde <= aniosHasta && |
| | 5 | 88 | | t.AniosHasta >= aniosDesde); |
| | | 89 | | |
| | 5 | 90 | | if (excludingId.HasValue) |
| | | 91 | | { |
| | 1 | 92 | | query = query.Where(t => t.Id != excludingId.Value); |
| | | 93 | | } |
| | | 94 | | |
| | 5 | 95 | | return await query.AnyAsync(); |
| | 5 | 96 | | } |
| | | 97 | | |
| | | 98 | | public async Task<bool> ExistsByIdAsync(long id) |
| | | 99 | | { |
| | 0 | 100 | | return await _context.TablaPermanencias.AnyAsync(t => t.Id == id && t.Activo); |
| | 0 | 101 | | } |
| | | 102 | | } |