51 lines
1.8 KiB
C#
51 lines
1.8 KiB
C#
using Hcs.WebApp.Data.Hcs;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Hcs.WebApp.Services
|
|
{
|
|
public class OperationService(IDbContextFactory<HcsDbContext> factory)
|
|
{
|
|
private readonly IDbContextFactory<HcsDbContext> factory = factory;
|
|
|
|
public async Task<bool> HasActiveOperationAsync(Operation.OperationType type)
|
|
{
|
|
using var context = factory.CreateDbContext();
|
|
return await context.Operations.AnyAsync(x => x.Type == type && !x.EndedAt.HasValue);
|
|
}
|
|
|
|
public async Task<Operation> InitiateOperationAsync(Operation.OperationType type, string initiatorId)
|
|
{
|
|
using var context = factory.CreateDbContext();
|
|
var operation = new Operation()
|
|
{
|
|
Type = type,
|
|
InitiatorId = initiatorId,
|
|
StartedAt = DateTime.UtcNow
|
|
};
|
|
await context.Operations.AddAsync(operation);
|
|
await context.SaveChangesAsync();
|
|
return operation;
|
|
}
|
|
|
|
public async Task<IEnumerable<Operation>> GetInitiatedOperationsAsync()
|
|
{
|
|
using var context = factory.CreateDbContext();
|
|
return await (from operation in context.Operations
|
|
where !operation.EndedAt.HasValue && string.IsNullOrEmpty(operation.MessageGuid)
|
|
select operation).ToListAsync();
|
|
}
|
|
|
|
public async Task SetOperationMessageGuidAsync(int operationId, string messageGuid)
|
|
{
|
|
using var context = factory.CreateDbContext();
|
|
var operation = await context.Operations.FirstOrDefaultAsync(x => x.Id == operationId);
|
|
if (operation != null)
|
|
{
|
|
operation.MessageGuid = messageGuid;
|
|
|
|
await context.SaveChangesAsync();
|
|
}
|
|
}
|
|
}
|
|
}
|