Complete campaign flow
This commit is contained in:
@ -11,6 +11,12 @@ namespace Hcs.WebApp.Services
|
||||
return await context.Campaigns.AnyAsync(x => x.Type == type && !x.EndedAt.HasValue);
|
||||
}
|
||||
|
||||
public async Task<bool> HasActiveOperationsAsync(int campaignId)
|
||||
{
|
||||
using var context = GetNewContext();
|
||||
return await context.Operations.CountAsync(x => x.CampaignId == campaignId && !x.EndedAt.HasValue) > 0;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Campaign>> GetInitiatedCampaignAsync()
|
||||
{
|
||||
using var context = GetNewContext();
|
||||
@ -41,21 +47,59 @@ namespace Hcs.WebApp.Services
|
||||
return campaign;
|
||||
}
|
||||
|
||||
public async Task<Operation> InitiateOperationAsync(int campaignId, Operation.OperationType type)
|
||||
public async Task<IEnumerable<Operation>> InitiateOperationsAsync(HcsDbContext context, int count, int campaignId, Operation.OperationType type)
|
||||
{
|
||||
using var context = GetNewContext();
|
||||
var operation = new Operation()
|
||||
var operations = new List<Operation>();
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
CampaignId = campaignId,
|
||||
Type = type,
|
||||
CreatedAt = DateTime.UtcNow
|
||||
};
|
||||
await context.Operations.AddAsync(operation);
|
||||
operations.Add(new Operation()
|
||||
{
|
||||
CampaignId = campaignId,
|
||||
Type = type,
|
||||
CreatedAt = DateTime.UtcNow
|
||||
});
|
||||
}
|
||||
await context.Operations.AddRangeAsync(operations);
|
||||
await context.SaveChangesAsync();
|
||||
return operation;
|
||||
return operations;
|
||||
}
|
||||
|
||||
public async Task SetCampaignEndedWithFail(int campaignId, string failureReason)
|
||||
public async Task SetCampaignStartedAsync(HcsDbContext context, int campaignId)
|
||||
{
|
||||
var campaign = await context.Campaigns.FirstOrDefaultAsync(x => x.Id == campaignId);
|
||||
if (campaign != null)
|
||||
{
|
||||
campaign.StartedAt = DateTime.UtcNow;
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SetOperationStartedAsync(int operationId)
|
||||
{
|
||||
using var context = GetNewContext();
|
||||
var operation = await context.Operations.FirstOrDefaultAsync(x => x.Id == operationId);
|
||||
if (operation != null)
|
||||
{
|
||||
operation.StartedAt = DateTime.UtcNow;
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SetCampaignEndedAsync(int campaignId)
|
||||
{
|
||||
using var context = GetNewContext();
|
||||
var campaign = await context.Campaigns.FirstOrDefaultAsync(x => x.Id == campaignId);
|
||||
if (campaign != null)
|
||||
{
|
||||
campaign.EndedAt = DateTime.UtcNow;
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SetCampaignEndedWithFailAsync(int campaignId, string failureReason)
|
||||
{
|
||||
using var context = GetNewContext();
|
||||
var campaign = await context.Campaigns.FirstOrDefaultAsync(x => x.Id == campaignId);
|
||||
@ -68,19 +112,7 @@ namespace Hcs.WebApp.Services
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SetOperationStarted(int operationId)
|
||||
{
|
||||
using var context = GetNewContext();
|
||||
var operation = await context.Operations.FirstOrDefaultAsync(x => x.Id == operationId);
|
||||
if (operation != null)
|
||||
{
|
||||
operation.StartedAt = DateTime.UtcNow;
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SetOperationEndedWithFail(int operationId, string failureReason)
|
||||
public async Task SetOperationEndedWithFailAsync(int operationId, string failureReason)
|
||||
{
|
||||
using var context = GetNewContext();
|
||||
var operation = await context.Operations.FirstOrDefaultAsync(x => x.Id == operationId);
|
||||
@ -104,5 +136,16 @@ namespace Hcs.WebApp.Services
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SetCampaignStepAsync(HcsDbContext context, int campaignId, int step)
|
||||
{
|
||||
var campaign = await context.Campaigns.FirstOrDefaultAsync(x => x.Id == campaignId);
|
||||
if (campaign != null)
|
||||
{
|
||||
campaign.Step = step;
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,6 +5,11 @@ namespace Hcs.WebApp.Services
|
||||
{
|
||||
public class RegistryService(IDbContextFactory<HcsDbContext> factory) : HcsServiceBase(factory)
|
||||
{
|
||||
public async Task<int> GetRegistryCountAsync(HcsDbContext context)
|
||||
{
|
||||
return await context.Registries.CountAsync();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Registry>> GetAllRegistriesAsync(bool isCommon)
|
||||
{
|
||||
using var context = GetNewContext();
|
||||
@ -19,7 +24,7 @@ namespace Hcs.WebApp.Services
|
||||
return await context.Registries.SingleAsync(x => x.LastSyncOperationId == operationId);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Registry>> GetRegistriesByOperationId(int operationId)
|
||||
public async Task<IEnumerable<Registry>> GetRegistriesByOperationIdAsync(int operationId)
|
||||
{
|
||||
using var context = GetNewContext();
|
||||
return await (from registry in context.Registries
|
||||
@ -27,9 +32,10 @@ namespace Hcs.WebApp.Services
|
||||
select registry).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task SetOperationIdToAllRegistries(HcsDbContext context, int operationId)
|
||||
public async Task SetOperationsToRegistriesAsync(HcsDbContext context, IEnumerable<Operation> operations)
|
||||
{
|
||||
await context.Registries.ForEachAsync(x => x.LastSyncOperationId = operationId);
|
||||
var queue = new Queue<Operation>(operations);
|
||||
await context.Registries.ForEachAsync(x => x.LastSyncOperationId = queue.Dequeue().Id);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user