Implemented default subscription

This commit is contained in:
Marcel Baumgartner 2023-04-15 22:44:40 +02:00
parent 9e30748665
commit 5e665b057e
2 changed files with 23 additions and 7 deletions

View file

@ -1,6 +1,7 @@
using Microsoft.EntityFrameworkCore;
using Moonlight.App.Database.Entities;
using Moonlight.App.Exceptions;
using Moonlight.App.Helpers;
using Moonlight.App.Models.Misc;
using Moonlight.App.Repositories;
using Moonlight.App.Services.Sessions;
@ -14,20 +15,18 @@ public class SubscriptionService
private readonly OneTimeJwtService OneTimeJwtService;
private readonly IdentityService IdentityService;
private readonly UserRepository UserRepository;
private readonly ConfigService ConfigService;
public SubscriptionService(
SubscriptionRepository subscriptionRepository,
OneTimeJwtService oneTimeJwtService,
IdentityService identityService,
UserRepository userRepository,
ConfigService configService)
UserRepository userRepository
)
{
SubscriptionRepository = subscriptionRepository;
OneTimeJwtService = oneTimeJwtService;
IdentityService = identityService;
UserRepository = userRepository;
ConfigService = configService;
}
public async Task<Subscription?> GetCurrent()
@ -93,10 +92,12 @@ public class SubscriptionService
public async Task<SubscriptionLimit> GetLimit(string identifier)
{
var subscription = await GetCurrent();
var defaultLimits = await GetDefaultLimits();
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,
Amount = 0
@ -111,8 +112,9 @@ public class SubscriptionService
if (foundLimit != null)
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,
Amount = 0
@ -133,4 +135,17 @@ public class SubscriptionService
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>();
}
}

View file

@ -0,0 +1 @@
[]