shoc-backend/SeaHaven.DataServices/Implementation/CommentDataService.cs
npal 6c4de10349 perf(work-orders): delete a work order's comments in one commit
DeleteWorkOrderCascadeAsync deleted comments one at a time: each call
re-fetched the row and committed separately (2N round trips, N commits).
Add DeleteAllForWorkOrderAsync, which loads the set once and removes it in
a single SaveChangesAsync.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-05 20:14:50 -05:00

129 lines
4.7 KiB
C#

using Data.SeaHavenIndustries;
using Microsoft.EntityFrameworkCore;
using SeaHaven.DataServices.Helpers;
using SeaHaven.DataServices.Interfaces;
namespace SeaHaven.DataServices.Implementation
{
public class CommentDataService : ICommentDataService
{
private readonly ApplicationDbContext _context;
public CommentDataService(ApplicationDbContext context)
{
_context = context;
}
public async Task<Comments?> GetByIdAsync(int id)
{
return await _context.Comments.FindAsync(id);
}
public async Task<IEnumerable<Comments>> GetAllAsync()
{
return await _context.Comments
.Include(c => c.ApplicationUser)
.ToListAsync();
}
public async Task<IEnumerable<Comments>> GetAllForAccountAsync(int? accountId)
{
var workOrders = WorkOrderBoardQueryFilters.ApplyBaseScope(_context.workOrders.AsNoTracking());
if (accountId.HasValue)
workOrders = WorkOrderBoardQueryFilters.ApplyAccountScope(workOrders, accountId.Value);
return await (
from c in _context.Comments.AsNoTracking().Include(c => c.ApplicationUser)
join w in workOrders on c.WorkerOrderId equals w.Id
orderby c.CreatedDate
select c
).ToListAsync();
}
public async Task<IEnumerable<Comments>> GetByWorkOrderIdAsync(int workOrderId)
{
return await _context.Comments
.Where(c => c.WorkerOrderId == workOrderId)
.Include(c => c.ApplicationUser)
.OrderBy(c => c.CreatedDate)
.ToListAsync();
}
public async Task<IEnumerable<Comments>> GetByDispatchIdAsync(int dispatchId)
{
return await _context.Comments
.Where(c => c.DispatchId == dispatchId)
.Include(c => c.ApplicationUser)
.OrderBy(c => c.CreatedDate)
.ToListAsync();
}
public async Task<Comments> AddAsync(Comments comment)
{
comment.CreatedDate = DateTime.UtcNow;
await _context.Comments.AddAsync(comment);
await _context.SaveChangesAsync();
return comment;
}
public async Task UpdateAsync(Comments comment)
{
_context.Comments.Update(comment);
await _context.SaveChangesAsync();
}
public async Task DeleteAsync(int id)
{
var entity = await GetByIdAsync(id);
if (entity != null)
{
_context.Comments.Remove(entity);
await _context.SaveChangesAsync();
}
}
public async Task DeleteAllForWorkOrderAsync(int workOrderId)
{
var entities = await _context.Comments
.Where(c => c.WorkerOrderId == workOrderId)
.ToListAsync();
if (entities.Count == 0)
return;
_context.Comments.RemoveRange(entities);
await _context.SaveChangesAsync();
}
public async Task<bool> ExistsAsync(int id)
{
return await _context.Comments.AnyAsync(c => c.Id == id);
}
public async Task StageAsync(Comments comment, CancellationToken cancellationToken)
{
await _context.Comments.AddAsync(comment, cancellationToken);
}
public async Task<IReadOnlyList<PortalCommentData>> GetVendorViewableForDispatchAsync(int dispatchId, CancellationToken cancellationToken)
{
return await (from c in _context.Comments
where c.DispatchId == dispatchId
&& c.CommentType != "internal"
&& (c.IsDeleted == null || c.IsDeleted == false)
join u in _context.Users on c.UserId equals u.Id into users
from u in users.DefaultIfEmpty()
orderby c.CreatedDate
select new PortalCommentData
{
Id = c.Id,
Commenttext = c.Commenttext,
Commenter = c.Commenter,
CommentType = c.CommentType,
CreatedDate = c.CreatedDate,
UserId = c.UserId,
UserFirstName = u != null ? u.FirstName : null,
UserLastName = u != null ? u.LastName : null
}).ToListAsync(cancellationToken);
}
}
}