mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 17:43:12 +00:00
WorkOrderDataService.AddAsync overwrote the UTC CreatedDate set by WorkOrderService with local server time, offsetting the SLA response clock on any host not running in UTC. Data services now stamp CreatedDate, LastModificationTime and DeletionTime with DateTime.UtcNow, and a work order keeps the creation time its caller set.
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.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<bool> ExistsAsync(int id)
|
|
{
|
|
return await _context.Employees.AnyAsync(e => e.Id == id);
|
|
}
|
|
|
|
public async Task<int> CountAsync()
|
|
{
|
|
return await _context.Employees.CountAsync();
|
|
}
|
|
}
|
|
}
|