mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 14:13:12 +00:00
108 lines
3.4 KiB
C#
108 lines
3.4 KiB
C#
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<Employee?> GetByIdAsync(int id)
|
|
{
|
|
return await _context.Employees.FindAsync(id);
|
|
}
|
|
|
|
public async Task<Employee?> 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<IEnumerable<Employee>> GetAllAsync()
|
|
{
|
|
return await _context.Employees.ToListAsync();
|
|
}
|
|
|
|
public async Task<(IEnumerable<Employee> 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<Employee> AddAsync(Employee employee)
|
|
{
|
|
employee.CreatedDate = DateTime.Now;
|
|
await _context.Employees.AddAsync(employee);
|
|
await _context.SaveChangesAsync();
|
|
return employee;
|
|
}
|
|
|
|
public async Task UpdateAsync(Employee employee)
|
|
{
|
|
employee.LastModificationTime = DateTime.Now;
|
|
_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<bool> ExistsAsync(int id)
|
|
{
|
|
return await _context.Employees.AnyAsync(e => e.Id == id);
|
|
}
|
|
|
|
public async Task<int> CountAsync()
|
|
{
|
|
return await _context.Employees.CountAsync();
|
|
}
|
|
}
|
|
}
|