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

49 lines
1.6 KiB
C#
Raw Normal View History

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);
}
2026-07-24 16:36:48 -03:00
public async Task EnqueueLockAsync(
int workOrderId,
string fieldName,
string? userId,
CancellationToken cancellationToken)
{
2026-07-24 16:36:48 -03:00
if (await IsLockedAsync(workOrderId, fieldName, cancellationToken))
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));
}
}