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 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 async Task EnqueueLockAsync( int workOrderId, string fieldName, string? userId, CancellationToken cancellationToken) { 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)); } }