using Data.SeaHavenIndustries; using Microsoft.EntityFrameworkCore; using SeaHaven.DataServices.Interfaces; namespace SeaHaven.DataServices.Implementation { public class EmployeeDataService : IEmployeeDataService { private readonly ApplicationDbContext _context; public EmployeeDataService(ApplicationDbContext context) { _context = context; } public async Task GetByIdAsync(int id) { return await _context.Employees.FindAsync(id); } public async Task GetByIdWithDetailsAsync(int id) { return await _context.Employees .Include(e => e.Department) .Include(e => e.JobTitle) .Include(e => e.Region) .Include(e => e.Emails) .Include(e => e.Phones) .FirstOrDefaultAsync(e => e.Id == id); } public async Task> GetAllAsync() { return await _context.Employees.ToListAsync(); } public async Task<(IEnumerable Items, int TotalCount)> GetPagedAsync( int page, int pageSize, string? search = null) { search ??= ""; var query = _context.Employees .Include(e => e.JobTitle) .Include(e => e.Department) .Include(e => e.Region) .Include(e => e.Emails) .Include(e => e.Phones) .Where(e => !e.IsDeleted.HasValue || e.IsDeleted == false); if (!string.IsNullOrWhiteSpace(search)) { var s = search.ToLower(); query = query.Where(e => (e.FirstName != null && e.FirstName.ToLower().Contains(s)) || (e.LastName != null && e.LastName.ToLower().Contains(s)) || e.Emails.Any(x => x.Address != null && x.Address.ToLower().Contains(s)) || e.Phones.Any(x => x.Number != null && x.Number.Contains(search)) ); } var totalCount = await query.CountAsync(); var items = await query .OrderByDescending(e => e.Id) .Skip((page - 1) * pageSize) .Take(pageSize) .ToListAsync(); return (items, totalCount); } public async Task AddAsync(Employee employee) { employee.CreatedDate = DateTime.UtcNow; await _context.Employees.AddAsync(employee); await _context.SaveChangesAsync(); return employee; } public async Task UpdateAsync(Employee employee) { employee.LastModificationTime = DateTime.UtcNow; _context.Employees.Update(employee); await _context.SaveChangesAsync(); } public async Task DeleteAsync(int id) { var entity = await GetByIdAsync(id); if (entity != null) { _context.Employees.Remove(entity); await _context.SaveChangesAsync(); } } public async Task ExistsAsync(int id) { return await _context.Employees.AnyAsync(e => e.Id == id); } public async Task CountAsync() { return await _context.Employees.CountAsync(); } } }