From cd8b25bcb84949472a24af074e716dfdc7a1b06e Mon Sep 17 00:00:00 2001 From: West14 <30056636+West14@users.noreply.github.com> Date: Wed, 16 Feb 2022 18:08:02 +0200 Subject: [PATCH] [store] Reduce amount of hardcoded values. --- modules/ContentStore/ContentStore.cs | 10 ++--- modules/ContentStore/Handler/Store.cs | 41 +++++-------------- modules/ContentStore/Model/StoreItem.cs | 2 +- .../ContentStore/Service/AnimBoobsService.cs | 4 +- .../ContentStore/Service/AnimalAsAService.cs | 2 + modules/ContentStore/Service/CatApiService.cs | 5 +-- modules/ContentStore/Service/DogApiService.cs | 5 ++- .../Service/IRandomMediaService.cs | 5 ++- modules/ContentStore/Service/OBoobsService.cs | 4 +- 9 files changed, 32 insertions(+), 46 deletions(-) diff --git a/modules/ContentStore/ContentStore.cs b/modules/ContentStore/ContentStore.cs index 1c3474f..9522d0b 100644 --- a/modules/ContentStore/ContentStore.cs +++ b/modules/ContentStore/ContentStore.cs @@ -8,25 +8,25 @@ namespace West.TelegramBot.ContentStore; public class ContentStore : Module { - private readonly List _mediaServiceTypes = new(); + public readonly List MediaServiceTypes = new(); public ContentStore(Core core) : base(core) { foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { foreach (var exportedType in assembly.GetExportedTypes() - .Where(t => !t.IsAbstract && t.IsSubclassOf(typeof(IRandomMediaService)))) + .Where(t => !t.IsAbstract && t.GetInterface(nameof(IRandomMediaService)) != null)) { - _mediaServiceTypes.Add(exportedType); + MediaServiceTypes.Add(exportedType); } } } public override void ConfigureServices(IServiceCollection services) { - foreach (var serviceType in _mediaServiceTypes) + foreach (var serviceType in MediaServiceTypes) { - services.AddSingleton(serviceType); + services.AddSingleton(typeof(IRandomMediaService), serviceType); } } } \ No newline at end of file diff --git a/modules/ContentStore/Handler/Store.cs b/modules/ContentStore/Handler/Store.cs index fd13cde..0d84bb2 100644 --- a/modules/ContentStore/Handler/Store.cs +++ b/modules/ContentStore/Handler/Store.cs @@ -19,19 +19,13 @@ public class Store : BotEventHandler { private readonly IReputation? _reputation; private readonly IServiceProvider _provider; - - private readonly Dictionary _storeItemMap = new() - { - { "cat", new StoreItem("cat", "Котэ", 40) }, - { "dog", new StoreItem("dog", "Пёсель", 40) }, - { "boobs", new StoreItem("boobs", "Сиськи", 60) }, - { "anim_boobs", new StoreItem("anim_boobs", "Сиськи движущиеся", 120) } - }; - - public Store(IServiceProvider provider) + private readonly ContentStore _store; + + public Store(IServiceProvider provider, ContentStore store) { _reputation = provider.GetService(); _provider = provider; + _store = store; } [Command(InChat.Public, "store", CommandParseMode.Both)] @@ -47,9 +41,9 @@ public class Store : BotEventHandler var counter = 0; var list = new List(); - foreach (var item in _storeItemMap) + foreach (var item in _provider.GetServices()) { - var btn = item.Value.ToButton(From.Id); + var btn = item.StoreItem.ToButton(From.Id); if ((counter & 1) == 0) // 0 - even, 1 - odd { list = new List @@ -97,13 +91,13 @@ public class Store : BotEventHandler var purchaseType = paramList[3]; answer = "Спасибо за покупку"; - if (!_storeItemMap.TryGetValue(purchaseType, out var storeItem) - || !TryGetMediaService(purchaseType, out var mediaService)) + if (!TryGetMediaService(purchaseType, out var mediaService)) { await AnswerQuery(query.Id, "Unknown purchasable."); return; } + var storeItem = mediaService.StoreItem; var messageId = query.Message!.MessageId; if (await _reputation.GetUserReputationAsync(Chat, From) < storeItem.Price) { @@ -140,22 +134,9 @@ public class Store : BotEventHandler private bool TryGetMediaService(string name, out IRandomMediaService? mediaService) { - var serviceType = name switch - { - "boobs" => typeof(OBoobsService), - "cat" => typeof(CatApiService), - "dog" => typeof(DogApiService), - "anim_boobs" => typeof(AnimBoobsService), - _ => null - }; - - if (serviceType == null) - { - mediaService = null; - return false; - } - - mediaService = _provider.GetService(serviceType) as IRandomMediaService; + mediaService = _provider.GetServices() + .FirstOrDefault(s => s.StoreItem.Id == name); + return mediaService != null; } diff --git a/modules/ContentStore/Model/StoreItem.cs b/modules/ContentStore/Model/StoreItem.cs index b3250c4..604d1af 100644 --- a/modules/ContentStore/Model/StoreItem.cs +++ b/modules/ContentStore/Model/StoreItem.cs @@ -2,7 +2,7 @@ namespace West.TelegramBot.ContentStore.Model; -class StoreItem +public class StoreItem { public string Id; public readonly string Name; diff --git a/modules/ContentStore/Service/AnimBoobsService.cs b/modules/ContentStore/Service/AnimBoobsService.cs index d2afda8..7fab5bc 100644 --- a/modules/ContentStore/Service/AnimBoobsService.cs +++ b/modules/ContentStore/Service/AnimBoobsService.cs @@ -1,5 +1,4 @@ -using Telegram.Bot.Types; -using Telegram.Bot.Types.Enums; +using Telegram.Bot.Types.Enums; using West.TelegramBot.ContentStore.Model; namespace West.TelegramBot.ContentStore.Service; @@ -7,6 +6,7 @@ namespace West.TelegramBot.ContentStore.Service; public class AnimBoobsService : IRandomMediaService { private readonly HttpClient _httpClient; + public StoreItem StoreItem { get; } = new("boobs", "Сиськи движущиеся", 120); public AnimBoobsService() { diff --git a/modules/ContentStore/Service/AnimalAsAService.cs b/modules/ContentStore/Service/AnimalAsAService.cs index e70e50c..eb6933c 100644 --- a/modules/ContentStore/Service/AnimalAsAService.cs +++ b/modules/ContentStore/Service/AnimalAsAService.cs @@ -6,6 +6,7 @@ namespace West.TelegramBot.ContentStore.Service; public abstract class AnimalAsAService : IRandomMediaService { private readonly HttpClient _httpClient; + public abstract StoreItem StoreItem { get; } protected AnimalAsAService(Uri baseAddress) { @@ -15,6 +16,7 @@ public abstract class AnimalAsAService : IRandomMediaService }; } + public async Task GetRandomMediaAsync() { var resp = await _httpClient.GetAsync("images/search"); diff --git a/modules/ContentStore/Service/CatApiService.cs b/modules/ContentStore/Service/CatApiService.cs index e340357..88fa8b7 100644 --- a/modules/ContentStore/Service/CatApiService.cs +++ b/modules/ContentStore/Service/CatApiService.cs @@ -1,10 +1,9 @@ -using Newtonsoft.Json; -using Telegram.Bot.Types; -using West.TelegramBot.ContentStore.Model; +using West.TelegramBot.ContentStore.Model; namespace West.TelegramBot.ContentStore.Service; public class CatApiService : AnimalAsAService { + public override StoreItem StoreItem { get; } = new("cat", "Котэ", 40); public CatApiService() : base(new Uri("https://api.thecatapi.com/v1/")) {} } \ No newline at end of file diff --git a/modules/ContentStore/Service/DogApiService.cs b/modules/ContentStore/Service/DogApiService.cs index 6bd9fb1..cf1ab1e 100644 --- a/modules/ContentStore/Service/DogApiService.cs +++ b/modules/ContentStore/Service/DogApiService.cs @@ -1,6 +1,9 @@ -namespace West.TelegramBot.ContentStore.Service; +using West.TelegramBot.ContentStore.Model; + +namespace West.TelegramBot.ContentStore.Service; public class DogApiService : AnimalAsAService { + public override StoreItem StoreItem { get; } = new("dog", "Пёсель", 40); public DogApiService() : base(new Uri("https://api.thedogapi.com/v1/")) {} } \ No newline at end of file diff --git a/modules/ContentStore/Service/IRandomMediaService.cs b/modules/ContentStore/Service/IRandomMediaService.cs index 07c7077..4d63126 100644 --- a/modules/ContentStore/Service/IRandomMediaService.cs +++ b/modules/ContentStore/Service/IRandomMediaService.cs @@ -1,9 +1,10 @@ -using Telegram.Bot.Types; -using West.TelegramBot.ContentStore.Model; +using West.TelegramBot.ContentStore.Model; namespace West.TelegramBot.ContentStore.Service; public interface IRandomMediaService { + public StoreItem StoreItem { get; } + public Task GetRandomMediaAsync(); } \ No newline at end of file diff --git a/modules/ContentStore/Service/OBoobsService.cs b/modules/ContentStore/Service/OBoobsService.cs index d88727e..4048a72 100644 --- a/modules/ContentStore/Service/OBoobsService.cs +++ b/modules/ContentStore/Service/OBoobsService.cs @@ -1,5 +1,4 @@ using Newtonsoft.Json; -using Telegram.Bot.Types; using West.TelegramBot.ContentStore.Model; namespace West.TelegramBot.ContentStore.Service; @@ -7,7 +6,8 @@ namespace West.TelegramBot.ContentStore.Service; public class OBoobsService : IRandomMediaService { private readonly HttpClient _httpClient; - + public StoreItem StoreItem { get; } = new("boobs", "Сиськи", 60); + public OBoobsService() { _httpClient = new HttpClient