Add transaction support between services
This commit is contained in:
@ -3,19 +3,17 @@ using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Hcs.WebApp.Services
|
||||
{
|
||||
public class HeadquartersService(IDbContextFactory<HcsDbContext> factory)
|
||||
public class HeadquartersService(IDbContextFactory<HcsDbContext> factory) : HcsServiceBase(factory)
|
||||
{
|
||||
private readonly IDbContextFactory<HcsDbContext> factory = factory;
|
||||
|
||||
public async Task<bool> HasActiveCampaignAsync(Campaign.CampaignType type)
|
||||
{
|
||||
using var context = factory.CreateDbContext();
|
||||
using var context = GetNewContext();
|
||||
return await context.Campaigns.AnyAsync(x => x.Type == type && !x.EndedAt.HasValue);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Campaign>> GetInitiatedCampaignAsync()
|
||||
{
|
||||
using var context = factory.CreateDbContext();
|
||||
using var context = GetNewContext();
|
||||
return await (from campaign in context.Campaigns
|
||||
where !campaign.EndedAt.HasValue
|
||||
select campaign).ToListAsync();
|
||||
@ -23,7 +21,7 @@ namespace Hcs.WebApp.Services
|
||||
|
||||
public async Task<IEnumerable<Operation>> GetInitiatedOperationsAsync()
|
||||
{
|
||||
using var context = factory.CreateDbContext();
|
||||
using var context = GetNewContext();
|
||||
return await (from operation in context.Operations
|
||||
where !operation.EndedAt.HasValue && string.IsNullOrEmpty(operation.MessageGuid)
|
||||
select operation).ToListAsync();
|
||||
@ -31,7 +29,7 @@ namespace Hcs.WebApp.Services
|
||||
|
||||
public async Task<Campaign> InitiateCampaignAsync(Campaign.CampaignType type, string initiatorId)
|
||||
{
|
||||
using var context = factory.CreateDbContext();
|
||||
using var context = GetNewContext();
|
||||
var campaign = new Campaign()
|
||||
{
|
||||
Type = type,
|
||||
@ -45,7 +43,7 @@ namespace Hcs.WebApp.Services
|
||||
|
||||
public async Task<Operation> InitiateOperationAsync(int campaignId, Operation.OperationType type)
|
||||
{
|
||||
using var context = factory.CreateDbContext();
|
||||
using var context = GetNewContext();
|
||||
var operation = new Operation()
|
||||
{
|
||||
CampaignId = campaignId,
|
||||
@ -59,7 +57,7 @@ namespace Hcs.WebApp.Services
|
||||
|
||||
public async Task SetCampaignEndedWithFail(int campaignId, string failureReason)
|
||||
{
|
||||
using var context = factory.CreateDbContext();
|
||||
using var context = GetNewContext();
|
||||
var campaign = await context.Campaigns.FirstOrDefaultAsync(x => x.Id == campaignId);
|
||||
if (campaign != null)
|
||||
{
|
||||
@ -72,7 +70,7 @@ namespace Hcs.WebApp.Services
|
||||
|
||||
public async Task SetOperationStarted(int operationId)
|
||||
{
|
||||
using var context = factory.CreateDbContext();
|
||||
using var context = GetNewContext();
|
||||
var operation = await context.Operations.FirstOrDefaultAsync(x => x.Id == operationId);
|
||||
if (operation != null)
|
||||
{
|
||||
@ -84,7 +82,7 @@ namespace Hcs.WebApp.Services
|
||||
|
||||
public async Task SetOperationEndedWithFail(int operationId, string failureReason)
|
||||
{
|
||||
using var context = factory.CreateDbContext();
|
||||
using var context = GetNewContext();
|
||||
var operation = await context.Operations.FirstOrDefaultAsync(x => x.Id == operationId);
|
||||
if (operation != null)
|
||||
{
|
||||
@ -97,7 +95,7 @@ namespace Hcs.WebApp.Services
|
||||
|
||||
public async Task SetOperationMessageGuidAsync(int operationId, string messageGuid)
|
||||
{
|
||||
using var context = factory.CreateDbContext();
|
||||
using var context = GetNewContext();
|
||||
var operation = await context.Operations.FirstOrDefaultAsync(x => x.Id == operationId);
|
||||
if (operation != null)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user