shoc-backend/SeaHaven.DataServices/Implementation/WorkOrderFieldLockDataService.cs
2026-07-24 11:30:22 -03:00

44 lines
1.5 KiB
C#

using Data.SeaHavenIndustries;
using Microsoft.EntityFrameworkCore;
using SeaHaven.DataServices.Interfaces;
namespace SeaHaven.DataServices.Implementation
{
public class WorkOrderFieldLockDataService : IWorkOrderFieldLockDataService
{
private readonly ApplicationDbContext _context;
public WorkOrderFieldLockDataService(ApplicationDbContext context)
{
_context = context;
}
public async Task<bool> IsLockedAsync(int workOrderId, string fieldName, CancellationToken cancellationToken)
{
if (IsTracked(workOrderId, fieldName))
return true;
return await _context.WorkOrderFieldLocks
.AnyAsync(l => l.WorkOrderId == workOrderId && l.FieldName == fieldName, cancellationToken);
}
public void EnqueueLock(int workOrderId, string fieldName, string? userId)
{
if (IsTracked(workOrderId, fieldName))
return;
_context.WorkOrderFieldLocks.Add(new WorkOrderFieldLock
{
WorkOrderId = workOrderId,
FieldName = fieldName,
LockedAt = DateTime.UtcNow,
LockedByUserId = userId
});
}
private bool IsTracked(int workOrderId, string fieldName)
=> _context.WorkOrderFieldLocks.Local.Any(l =>
l.WorkOrderId == workOrderId &&
string.Equals(l.FieldName, fieldName, StringComparison.OrdinalIgnoreCase));
}
}