fix: migrate to IDbContextFactory and remove direct AppDbContext from DI (#11)
Reviewed-on: #11 Co-authored-by: Marek Jasiński <jasins.marek@gmail.com> Co-committed-by: Marek Jasiński <jasins.marek@gmail.com>
This commit was merged in pull request #11.
This commit is contained in:
@@ -5,24 +5,24 @@ using Microsoft.Extensions.Options;
|
||||
using NexusReader.Application.Abstractions.Services;
|
||||
using NexusReader.Domain.Entities;
|
||||
using NexusReader.Infrastructure.Configuration;
|
||||
using NexusReader.Infrastructure.Persistence;
|
||||
using NexusReader.Data.Persistence;
|
||||
|
||||
namespace NexusReader.Infrastructure.Services;
|
||||
|
||||
public class BillingService : IBillingService
|
||||
{
|
||||
private readonly AppDbContext _dbContext;
|
||||
private readonly IDbContextFactory<AppDbContext> _dbContextFactory;
|
||||
private readonly UserManager<NexusUser> _userManager;
|
||||
private readonly StripeSettings _stripeSettings;
|
||||
private readonly ILogger<BillingService> _logger;
|
||||
|
||||
public BillingService(
|
||||
AppDbContext dbContext,
|
||||
IDbContextFactory<AppDbContext> dbContextFactory,
|
||||
UserManager<NexusUser> userManager,
|
||||
IOptions<StripeSettings> stripeSettings,
|
||||
ILogger<BillingService> logger)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_dbContextFactory = dbContextFactory;
|
||||
_userManager = userManager;
|
||||
_stripeSettings = stripeSettings.Value;
|
||||
_logger = logger;
|
||||
@@ -55,7 +55,8 @@ public class BillingService : IBillingService
|
||||
_logger.LogWarning("Unrecognized Stripe Product ID: {ProductId} for user {Email}. Falling back to Free tier.", stripeProductId, customerEmail);
|
||||
}
|
||||
|
||||
var plan = await _dbContext.SubscriptionPlans.FirstOrDefaultAsync(p => p.PlanName == targetPlanName);
|
||||
using var dbContext = await _dbContextFactory.CreateDbContextAsync();
|
||||
var plan = await dbContext.SubscriptionPlans.FirstOrDefaultAsync(p => p.PlanName == targetPlanName);
|
||||
if (plan != null)
|
||||
{
|
||||
user.SubscriptionPlanId = plan.Id;
|
||||
@@ -82,7 +83,8 @@ public class BillingService : IBillingService
|
||||
return false;
|
||||
}
|
||||
|
||||
var freePlan = await _dbContext.SubscriptionPlans.FirstOrDefaultAsync(p => p.PlanName == SubscriptionPlan.FreeName);
|
||||
using var dbContext = await _dbContextFactory.CreateDbContextAsync();
|
||||
var freePlan = await dbContext.SubscriptionPlans.FirstOrDefaultAsync(p => p.PlanName == SubscriptionPlan.FreeName);
|
||||
if (freePlan != null)
|
||||
{
|
||||
user.SubscriptionPlanId = freePlan.Id;
|
||||
|
||||
@@ -7,7 +7,7 @@ using NexusReader.Application.Abstractions.Services;
|
||||
using NexusReader.Application.DTOs.AI;
|
||||
using NexusReader.Domain.Entities;
|
||||
using NexusReader.Infrastructure.Helpers;
|
||||
using NexusReader.Infrastructure.Persistence;
|
||||
using NexusReader.Data.Persistence;
|
||||
using Polly;
|
||||
using Polly.Registry;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
Reference in New Issue
Block a user