shoc-backend/SeaHaven.DataServices/Implementation/FollowUpDataService.cs

258 lines
9 KiB
C#
Raw Normal View History

2026-04-28 18:55:14 -05:00
using Data.SeaHavenIndustries;
using Microsoft.EntityFrameworkCore;
using SeaHaven.DataServices.Interfaces;
namespace SeaHaven.DataServices.Implementation
{
public class FollowUpDataService : IFollowUpDataService
{
private readonly ApplicationDbContext _context;
public FollowUpDataService(ApplicationDbContext context)
{
_context = context;
}
public async Task<FollowUps?> GetByIdAsync(int id)
{
return await _context.FollowUps.FindAsync(id);
}
public async Task<FollowUps?> GetByIdWithDetailsAsync(int id)
{
return await _context.FollowUps
2026-05-06 10:49:33 -05:00
.Include(x => x.Employee)
.Include(x => x.Account)
.Include(x => x.Location)
// Removed .Include(x => x.WorkOrder) due to schema mismatch - WorkOrder has columns not in DB
2026-04-28 18:55:14 -05:00
.FirstOrDefaultAsync(f => f.Id == id);
}
public async Task<IEnumerable<FollowUps>> GetAllAsync()
{
return await _context.FollowUps.ToListAsync();
}
public async Task<IEnumerable<FollowUps>> GetByWorkOrderIdAsync(int workOrderId)
{
return await _context.FollowUps
.Where(f => f.WorkOrderId == workOrderId)
.ToListAsync();
}
public async Task<IEnumerable<FollowUps>> GetByUserIdAsync(string userId)
{
return await _context.FollowUps
.Where(f => f.createdby == userId)
.ToListAsync();
}
public async Task<(IEnumerable<FollowUps> Items, int TotalCount)> GetPagedAsync(
2026-05-06 10:49:33 -05:00
int page,
int pageSize,
string? search = null,
2026-04-28 18:55:14 -05:00
string? status = null)
{
2026-05-06 10:49:33 -05:00
search ??= "";
status = string.IsNullOrWhiteSpace(status) ? "Open" : status;
2026-04-28 18:55:14 -05:00
2026-05-06 10:49:33 -05:00
var query = _context.FollowUps
.Include(x => x.Employee)
.Include(x => x.Account)
.Include(x => x.Location)
// Removed .Include(x => x.WorkOrder) due to schema mismatch - WorkOrder has columns not in DB
.Where(x => x.Status == status);
if (!string.IsNullOrWhiteSpace(search))
2026-04-28 18:55:14 -05:00
{
2026-05-06 10:49:33 -05:00
var s = search.ToLower();
query = query.Where(x =>
(x.Reason ?? "").ToLower().Contains(s) ||
(x.Account != null && (x.Account.Name ?? "").ToLower().Contains(s)) ||
(x.Employee != null &&
(((x.Employee.FirstName ?? "") + " " + (x.Employee.LastName ?? ""))
.ToLower().Contains(s))
)
);
2026-04-28 18:55:14 -05:00
}
var totalCount = await query.CountAsync();
var items = await query
2026-05-06 10:49:33 -05:00
.OrderByDescending(f => f.Id)
2026-04-28 18:55:14 -05:00
.Skip((page - 1) * pageSize)
.Take(pageSize)
.ToListAsync();
return (items, totalCount);
}
public async Task<FollowUps> AddAsync(FollowUps followUp)
{
followUp.CreatedDate = DateTime.Now;
await _context.FollowUps.AddAsync(followUp);
await _context.SaveChangesAsync();
return followUp;
}
public async Task UpdateAsync(FollowUps followUp)
{
followUp.LastModificationTime = DateTime.Now;
_context.FollowUps.Update(followUp);
await _context.SaveChangesAsync();
}
public async Task DeleteAsync(int id)
{
var entity = await GetByIdAsync(id);
if (entity != null)
{
_context.FollowUps.Remove(entity);
await _context.SaveChangesAsync();
}
}
public async Task<bool> ExistsAsync(int id)
{
return await _context.FollowUps.AnyAsync(f => f.Id == id);
}
public async Task<int> CountAsync()
{
return await _context.FollowUps.CountAsync();
}
refactor: enforce backend boundaries and optimize dispatch (#30) * refactor(api): enforce service and data-service boundaries * refactor(api): complete feature service boundaries * refactor(identity): enforce service and data boundaries * refactor(vendors): enforce service and data boundaries * refactor(workorders): enforce service and data boundaries * refactor(backend): enforce architecture and optimize dispatch * style(backend): format changed architecture files * fix(architecture): address backend review follow-ups * fix(backend): sanitize exception disclosure in changed API endpoints Replace raw exception-message disclosure (ex.Message) returned to API callers with a stable sanitized public message plus correlated structured internal logging, across the endpoints changed in this PR. - Add SanitizedErrors helper: logs the original exception at Error with a generated correlation id and returns a stable public message referencing it so support can trace without exposing internals. - Inject ILogger<T> into the 14 changed controllers and route every ex.Message/dbex.Message disclosure through the helper, preserving status codes, response shapes, and business data (e.g. OpenWorkOrders). - Leave FluentValidation (vex.Errors) and existing fixed-message catches untouched; out-of-scope controllers (Account/Contact/Employee/Asset/ PMSchedule) are unchanged. - Add focused tests proving internal exception text is not returned and that Error logging carrying the original exception is invoked. * fix(architecture): abstract job run state access * style: format board update service * test: use collection assertion idiom
2026-07-24 17:35:34 -03:00
public async Task<List<FollowUps>> GetListPagedAsync(
int page,
int pageSize,
string? search,
string? status,
CancellationToken cancellationToken)
{
search ??= "";
status = string.IsNullOrWhiteSpace(status) ? "Open" : status;
var query = _context.FollowUps
.Include(x => x.Employee)
.Include(x => x.Account)
.Include(x => x.Location)
.AsNoTracking()
.Where(x => x.Status == status);
if (!string.IsNullOrWhiteSpace(search))
{
var s = search.ToLower();
query = query.Where(x =>
(x.Reason ?? "").ToLower().Contains(s) ||
(x.Account != null && (x.Account.Name ?? "").ToLower().Contains(s)) ||
(x.Employee != null &&
(((x.Employee.FirstName ?? "") + " " + (x.Employee.LastName ?? ""))
.ToLower().Contains(s))
)
);
}
return await query
.OrderByDescending(f => f.Id)
.Skip((page - 1) * pageSize)
.Take(pageSize)
.ToListAsync(cancellationToken);
}
public async Task<int> GetListTotalAsync(string? search, string? status, CancellationToken cancellationToken)
{
search ??= "";
status = string.IsNullOrWhiteSpace(status) ? "Open" : status;
var query = _context.FollowUps
.AsNoTracking()
.Where(x => x.Status == status);
if (!string.IsNullOrWhiteSpace(search))
{
var s = search.ToLower();
query = query.Where(x =>
(x.Reason ?? "").ToLower().Contains(s) ||
(x.Account != null && (x.Account.Name ?? "").ToLower().Contains(s)) ||
(x.Employee != null &&
(((x.Employee.FirstName ?? "") + " " + (x.Employee.LastName ?? ""))
.ToLower().Contains(s))
)
);
}
return await query.CountAsync(cancellationToken);
}
public async Task<FollowUps?> GetDetailByIdAsync(int id, CancellationToken cancellationToken)
{
return await _context.FollowUps
.Include(x => x.Employee)
.Include(x => x.Account)
.Include(x => x.Location)
.AsNoTracking()
.FirstOrDefaultAsync(f => f.Id == id, cancellationToken);
}
public async Task<FollowUps?> GetByIdForUpdateAsync(int id, CancellationToken cancellationToken)
{
return await _context.FollowUps.FindAsync(new object[] { id }, cancellationToken);
}
public async Task<string> GetUserNameAsync(string? userId, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(userId))
return "";
var u = await _context.Users
.AsNoTracking()
.Where(x => x.Id == userId)
.Select(x => new { x.FirstName, x.LastName, x.UserName })
.FirstOrDefaultAsync(cancellationToken);
if (u == null)
return "";
var n = ((u.FirstName ?? "") + " " + (u.LastName ?? "")).Trim();
return string.IsNullOrWhiteSpace(n) ? (u.UserName ?? "") : n;
}
public async Task<FollowUps> AddAsync(FollowUps followUp, CancellationToken cancellationToken)
{
followUp.CreatedDate = DateTime.Now;
await _context.FollowUps.AddAsync(followUp, cancellationToken);
await _context.SaveChangesAsync(cancellationToken);
return followUp;
}
public async Task UpdateAsync(FollowUps followUp, CancellationToken cancellationToken)
{
followUp.LastModificationTime = DateTime.Now;
_context.FollowUps.Update(followUp);
await _context.SaveChangesAsync(cancellationToken);
}
public async Task<bool> SetStatusAsync(int id, string status, CancellationToken cancellationToken)
{
var entity = await _context.FollowUps.FindAsync(new object[] { id }, cancellationToken);
if (entity == null)
return false;
entity.Status = status;
entity.LastModificationTime = DateTime.Now;
await _context.SaveChangesAsync(cancellationToken);
return true;
}
public async Task<bool> RemoveByIdAsync(int id, CancellationToken cancellationToken)
{
var entity = await _context.FollowUps.FindAsync(new object[] { id }, cancellationToken);
if (entity == null)
return false;
_context.FollowUps.Remove(entity);
await _context.SaveChangesAsync(cancellationToken);
return true;
}
2026-04-28 18:55:14 -05:00
}
}