Merge pull request #73 from Moonlight-Panel/DefaultSubscription
Implemented default subscription
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Moonlight.App.Database.Entities;
|
using Moonlight.App.Database.Entities;
|
||||||
using Moonlight.App.Exceptions;
|
using Moonlight.App.Exceptions;
|
||||||
|
using Moonlight.App.Helpers;
|
||||||
using Moonlight.App.Models.Misc;
|
using Moonlight.App.Models.Misc;
|
||||||
using Moonlight.App.Repositories;
|
using Moonlight.App.Repositories;
|
||||||
using Moonlight.App.Services.Sessions;
|
using Moonlight.App.Services.Sessions;
|
||||||
@@ -14,20 +15,18 @@ public class SubscriptionService
|
|||||||
private readonly OneTimeJwtService OneTimeJwtService;
|
private readonly OneTimeJwtService OneTimeJwtService;
|
||||||
private readonly IdentityService IdentityService;
|
private readonly IdentityService IdentityService;
|
||||||
private readonly UserRepository UserRepository;
|
private readonly UserRepository UserRepository;
|
||||||
private readonly ConfigService ConfigService;
|
|
||||||
|
|
||||||
public SubscriptionService(
|
public SubscriptionService(
|
||||||
SubscriptionRepository subscriptionRepository,
|
SubscriptionRepository subscriptionRepository,
|
||||||
OneTimeJwtService oneTimeJwtService,
|
OneTimeJwtService oneTimeJwtService,
|
||||||
IdentityService identityService,
|
IdentityService identityService,
|
||||||
UserRepository userRepository,
|
UserRepository userRepository
|
||||||
ConfigService configService)
|
)
|
||||||
{
|
{
|
||||||
SubscriptionRepository = subscriptionRepository;
|
SubscriptionRepository = subscriptionRepository;
|
||||||
OneTimeJwtService = oneTimeJwtService;
|
OneTimeJwtService = oneTimeJwtService;
|
||||||
IdentityService = identityService;
|
IdentityService = identityService;
|
||||||
UserRepository = userRepository;
|
UserRepository = userRepository;
|
||||||
ConfigService = configService;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Subscription?> GetCurrent()
|
public async Task<Subscription?> GetCurrent()
|
||||||
@@ -93,10 +92,12 @@ public class SubscriptionService
|
|||||||
public async Task<SubscriptionLimit> GetLimit(string identifier)
|
public async Task<SubscriptionLimit> GetLimit(string identifier)
|
||||||
{
|
{
|
||||||
var subscription = await GetCurrent();
|
var subscription = await GetCurrent();
|
||||||
|
var defaultLimits = await GetDefaultLimits();
|
||||||
|
|
||||||
if (subscription == null)
|
if (subscription == null)
|
||||||
{
|
{
|
||||||
return new()
|
// If the default subscription limit with identifier is found, return it. if not, return empty
|
||||||
|
return defaultLimits.FirstOrDefault(x => x.Identifier == identifier) ?? new()
|
||||||
{
|
{
|
||||||
Identifier = identifier,
|
Identifier = identifier,
|
||||||
Amount = 0
|
Amount = 0
|
||||||
@@ -111,8 +112,9 @@ public class SubscriptionService
|
|||||||
|
|
||||||
if (foundLimit != null)
|
if (foundLimit != null)
|
||||||
return foundLimit;
|
return foundLimit;
|
||||||
|
|
||||||
return new()
|
// If the default subscription limit with identifier is found, return it. if not, return empty
|
||||||
|
return defaultLimits.FirstOrDefault(x => x.Identifier == identifier) ?? new()
|
||||||
{
|
{
|
||||||
Identifier = identifier,
|
Identifier = identifier,
|
||||||
Amount = 0
|
Amount = 0
|
||||||
@@ -133,4 +135,17 @@ public class SubscriptionService
|
|||||||
|
|
||||||
return userWithData;
|
return userWithData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task<SubscriptionLimit[]> GetDefaultLimits() // Add cache and reload option
|
||||||
|
{
|
||||||
|
var defaultSubscriptionJson = "[]";
|
||||||
|
|
||||||
|
if (File.Exists(PathBuilder.File("storage", "configs", "default_subscription.json")))
|
||||||
|
{
|
||||||
|
defaultSubscriptionJson =
|
||||||
|
await File.ReadAllTextAsync(PathBuilder.File("storage", "configs", "default_subscription.json"));
|
||||||
|
}
|
||||||
|
|
||||||
|
return JsonConvert.DeserializeObject<SubscriptionLimit[]>(defaultSubscriptionJson) ?? Array.Empty<SubscriptionLimit>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
[]
|
||||||
Reference in New Issue
Block a user