From 5e665b057e34c8ebc57053d2d9501f76d6678b17 Mon Sep 17 00:00:00 2001 From: Marcel Baumgartner Date: Sat, 15 Apr 2023 22:44:40 +0200 Subject: [PATCH] Implemented default subscription --- Moonlight/App/Services/SubscriptionService.cs | 29 ++++++++++++++----- .../configs/default_subscription.json | 1 + 2 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 Moonlight/defaultstorage/configs/default_subscription.json diff --git a/Moonlight/App/Services/SubscriptionService.cs b/Moonlight/App/Services/SubscriptionService.cs index de23692a..95c190da 100644 --- a/Moonlight/App/Services/SubscriptionService.cs +++ b/Moonlight/App/Services/SubscriptionService.cs @@ -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 GetCurrent() @@ -93,10 +92,12 @@ public class SubscriptionService public async Task 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 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(defaultSubscriptionJson) ?? Array.Empty(); + } } \ No newline at end of file diff --git a/Moonlight/defaultstorage/configs/default_subscription.json b/Moonlight/defaultstorage/configs/default_subscription.json new file mode 100644 index 00000000..ad47dbb9 --- /dev/null +++ b/Moonlight/defaultstorage/configs/default_subscription.json @@ -0,0 +1 @@ +[] \ No newline at end of file