Add campaign manager

This commit is contained in:
2025-10-27 10:02:58 +09:00
parent 891d462af1
commit 7c35c9a7df
18 changed files with 250 additions and 41 deletions

View File

@ -3,28 +3,22 @@ using Microsoft.EntityFrameworkCore;
namespace Hcs.WebApp.Services
{
public class OperationService(IDbContextFactory<HcsDbContext> factory)
public class HeadquartersService(IDbContextFactory<HcsDbContext> factory)
{
private readonly IDbContextFactory<HcsDbContext> factory = factory;
public async Task<bool> HasActiveOperationAsync(Operation.OperationType type)
public async Task<bool> HasActiveCampaignAsync(Campaign.CampaignType type)
{
using var context = factory.CreateDbContext();
return await context.Operations.AnyAsync(x => x.Type == type && !x.EndedAt.HasValue);
return await context.Campaigns.AnyAsync(x => x.Type == type && !x.EndedAt.HasValue);
}
public async Task<Operation> InitiateOperationAsync(Operation.OperationType type, string initiatorId)
public async Task<IEnumerable<Campaign>> GetInitiatedCampaignAsync()
{
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;
return await (from campaign in context.Campaigns
where !campaign.EndedAt.HasValue
select campaign).ToListAsync();
}
public async Task<IEnumerable<Operation>> GetInitiatedOperationsAsync()
@ -35,6 +29,34 @@ namespace Hcs.WebApp.Services
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();