73 lines
2.6 KiB
C#
73 lines
2.6 KiB
C#
using Hcs.WebApp.Data.Hcs;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Hcs.WebApp.Services
|
|
{
|
|
public class HeadquartersService(IDbContextFactory<HcsDbContext> factory)
|
|
{
|
|
private readonly IDbContextFactory<HcsDbContext> factory = factory;
|
|
|
|
public async Task<bool> HasActiveCampaignAsync(Campaign.CampaignType type)
|
|
{
|
|
using var context = factory.CreateDbContext();
|
|
return await context.Campaigns.AnyAsync(x => x.Type == type && !x.EndedAt.HasValue);
|
|
}
|
|
|
|
public async Task<IEnumerable<Campaign>> GetInitiatedCampaignAsync()
|
|
{
|
|
using var context = factory.CreateDbContext();
|
|
return await (from campaign in context.Campaigns
|
|
where !campaign.EndedAt.HasValue
|
|
select campaign).ToListAsync();
|
|
}
|
|
|
|
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<Campaign> InitiateCampaignAsync(Campaign.CampaignType type, string initiatorId)
|
|
{
|
|
using var context = factory.CreateDbContext();
|
|
var campaign = new Campaign()
|
|
{
|
|
Type = type,
|
|
InitiatorId = initiatorId,
|
|
StartedAt = DateTime.UtcNow
|
|
};
|
|
await context.Campaigns.AddAsync(campaign);
|
|
await context.SaveChangesAsync();
|
|
return campaign;
|
|
}
|
|
|
|
public async Task<Operation> InitiateOperationAsync(int campaignId, Operation.OperationType type)
|
|
{
|
|
using var context = factory.CreateDbContext();
|
|
var operation = new Operation()
|
|
{
|
|
CampaignId = campaignId,
|
|
Type = type,
|
|
StartedAt = DateTime.UtcNow
|
|
};
|
|
await context.Operations.AddAsync(operation);
|
|
await context.SaveChangesAsync();
|
|
return operation;
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
}
|