mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 09:33:13 +00:00
49 lines
1.6 KiB
C#
49 lines
1.6 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 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));
|
||
|
|
}
|
||
|
|
}
|