using Azure; using Domain.Entities; using Domain.Generics; using Microsoft.EntityFrameworkCore; using Models.Helpers; using Models.Interfaces; using Models.Models; namespace Models.Repositories { public class PhSPeopleRepository(PhronCareOperationsHubContext context) : IPhSPeopleRepository { #region Declaraciones private readonly PhronCareOperationsHubContext _context = context; #endregion #region Métodos public async Task> GetAllAsync(int page = 1, int pageSize = 50) { var query = _context.PhSPeople .Include(p => p.Businessunits) .Include(p => p.Peoplegroups) .AsQueryable(); var pagedEntities = await query.ToPagedResultAsync(page, pageSize); return new PagedResult { Items = pagedEntities.Items.Select(EntityMapper.MapEntity), TotalItems = pagedEntities.TotalItems, Page = pagedEntities.Page, PageSize = pagedEntities.PageSize }; } public async Task> SearchAsync(string? name, string? email, int page = 1, int pageSize = 50) { var query = _context.PhSPeople.AsQueryable(); if (!string.IsNullOrEmpty(name)) { query = query.Where(p => (p.Name).Contains(name)); } if (!string.IsNullOrEmpty(email)) { query = query.Where(p => p.Email != null && p.Email.Contains(email)); } var pagedEntities = await query.ToPagedResultAsync(page, pageSize); return new PagedResult { Items = pagedEntities.Items.Select(EntityMapper.MapEntity), TotalItems = pagedEntities.TotalItems, Page = pagedEntities.Page, PageSize = pagedEntities.PageSize }; } public async Task GetByIdAsync(int id) { var entity = await _context.PhSPeople .Include(p => p.Businessunits) .Include(p => p.Peoplegroups) .AsNoTracking() .FirstOrDefaultAsync(p => p.Id == id); return entity != null ? EntityMapper.MapEntity(entity) : null; } public async Task AddAsync(EPerson person) { var dbEntity = EntityMapper.MapEntity(person); _context.PhSPeople.Add(dbEntity); await _context.SaveChangesAsync(); return EntityMapper.MapEntity(dbEntity); } public async Task UpdateAsync(EPerson person) { var dbEntity = EntityMapper.MapEntity(person); _context.PhSPeople.Update(dbEntity); await _context.SaveChangesAsync(); } public async Task DeleteAsync(int id) { var entity = await _context.PhSPeople.FindAsync(id); if (entity != null) { _context.PhSPeople.Remove(entity); await _context.SaveChangesAsync(); } } #endregion } }