shoc-backend/SeaHaven.DataServices/Implementation/TaskListTemplateDataService.cs
Alexandre Brandizzi 7d245eb717
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

114 lines
4 KiB
C#

using Data.SeaHavenIndustries;
using Microsoft.EntityFrameworkCore;
using SeaHaven.DataServices.Interfaces;
namespace SeaHaven.DataServices.Implementation
{
public class TaskListTemplateDataService : ITaskListTemplateDataService
{
private readonly ApplicationDbContext _context;
public TaskListTemplateDataService(ApplicationDbContext context)
{
_context = context;
}
public async Task<TaskListTemplate?> GetByIdAsync(int id)
{
return await _context.TaskListTemplates.FindAsync(id);
}
public async Task<TaskListTemplate?> GetByIdWithItemsAsync(int id)
{
return await _context.TaskListTemplates
.Include(t => t.Items)
.FirstOrDefaultAsync(t => t.Id == id);
}
public async Task<IEnumerable<TaskListTemplate>> GetAllAsync()
{
return await _context.TaskListTemplates.ToListAsync();
}
public async Task<TaskListTemplate> AddAsync(TaskListTemplate template)
{
await _context.TaskListTemplates.AddAsync(template);
await _context.SaveChangesAsync();
return template;
}
public async Task UpdateAsync(TaskListTemplate template)
{
_context.TaskListTemplates.Update(template);
await _context.SaveChangesAsync();
}
public async Task DeleteAsync(int id)
{
var entity = await GetByIdAsync(id);
if (entity != null)
{
_context.TaskListTemplates.Remove(entity);
await _context.SaveChangesAsync();
}
}
public async Task<bool> ExistsAsync(int id)
{
return await _context.TaskListTemplates.AnyAsync(t => t.Id == id);
}
public async Task<IEnumerable<TaskListTemplate>> GetActiveTemplatesWithItemsAsync(CancellationToken cancellationToken)
{
return await _context.TaskListTemplates
.Include(t => t.Items)
.Where(t => t.IsActive)
.OrderBy(t => t.Name)
.AsNoTracking()
.ToListAsync(cancellationToken);
}
public async Task<TaskListTemplate?> GetTemplateWithItemsReadOnlyAsync(int id, CancellationToken cancellationToken)
{
return await _context.TaskListTemplates
.Include(t => t.Items)
.Where(t => t.Id == id)
.AsNoTracking()
.FirstOrDefaultAsync(cancellationToken);
}
public async Task<TaskListTemplate?> GetTemplateWithItemsForUpdateAsync(int id, CancellationToken cancellationToken)
{
return await _context.TaskListTemplates
.Include(t => t.Items)
.FirstOrDefaultAsync(t => t.Id == id, cancellationToken);
}
public async Task<TaskListTemplate> AddTemplateAsync(TaskListTemplate template, CancellationToken cancellationToken)
{
await _context.TaskListTemplates.AddAsync(template, cancellationToken);
await _context.SaveChangesAsync(cancellationToken);
return template;
}
public async Task ReplaceTemplateItemsAsync(TaskListTemplate template, IEnumerable<TaskListTemplateItem> newItems, CancellationToken cancellationToken)
{
if (template.Items != null)
_context.TaskListTemplateItems.RemoveRange(template.Items);
template.Items = newItems.ToList();
await _context.SaveChangesAsync(cancellationToken);
}
public async Task<bool> DeactivateTemplateAsync(int id, CancellationToken cancellationToken)
{
var template = await _context.TaskListTemplates.FindAsync(new object[] { id }, cancellationToken);
if (template == null)
return false;
template.IsActive = false;
await _context.SaveChangesAsync(cancellationToken);
return true;
}
}
}