From a1baa320fe665e043839264a9d50047d59e73279 Mon Sep 17 00:00:00 2001 From: GoldenBIOS Date: Sun, 3 May 2026 21:12:26 +0200 Subject: [PATCH] implement ChatService with stateful architecture and caching --- Services/ChatMethods.cs | 38 ------------------------- Services/ChatService.cs | 62 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 38 deletions(-) delete mode 100644 Services/ChatMethods.cs create mode 100644 Services/ChatService.cs diff --git a/Services/ChatMethods.cs b/Services/ChatMethods.cs deleted file mode 100644 index 5d78671..0000000 --- a/Services/ChatMethods.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using Chtn.CSharp.SDK.Core; -using Chtn.CSharp.SDK.Models.Chat; - -namespace Chtn.CSharp.SDK.Services -{ - public interface IChatsService - { - Task> Get(GetChatsReq req); - Task GetUserAvailability(GetAvailabilityReq req); - Task ToggleMute(ToggleChatMuteReq req); - Task StartNew(StartNewReq req); - } - - public class ChatsServiceProvider : IChatsService - { - private readonly ApiClient _apiClient; - - public ChatsServiceProvider(ApiClient apiClient) - { - _apiClient = apiClient ?? throw new ArgumentNullException(nameof(apiClient)); - } - - public async Task> Get(GetChatsReq req) => - await _apiClient.PostAsync>("chat/get", req); - - public async Task GetUserAvailability(GetAvailabilityReq req) => - await _apiClient.PostAsync("chat/availability", req); - - public async Task ToggleMute(ToggleChatMuteReq req) => - await _apiClient.PostAsync("v2/chat/toggleMute", req); - - public async Task StartNew(StartNewReq req) => - await _apiClient.PostAsync("chat/startNew", req); - } -} diff --git a/Services/ChatService.cs b/Services/ChatService.cs new file mode 100644 index 0000000..43e7649 --- /dev/null +++ b/Services/ChatService.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Chtn.CSharp.SDK.Core; +using Chtn.CSharp.SDK.Models.Chat; +using Chtn.CSharp.SDK.Models.Common; +using Chtn.CSharpSDK.Interfaces; +using Newtonsoft.Json; + +namespace Chtn.CSharp.SDK.Services +{ + public interface IChatsService + { + Task> Get(); + List GetFast(); + Task GetUserAvailability(GetAvailabilityReq req); + Task ToggleMute(ToggleChatMuteReq req); + Task StartNew(StartNewReq req); + } + + public class ChatsService : IChatsService + { + private readonly ApiClient _apiClient; + private readonly string _userId; + private readonly IDatabaseAPI _database; + + public ChatsService(ApiClient apiClient, string userId, IDatabaseAPI database) + { + _apiClient = apiClient ?? throw new ArgumentNullException(nameof(apiClient)); + _userId = userId; + _database = database ?? throw new ArgumentNullException(nameof(database)); + } + + public async Task> Get() + { + var body = new { userid = _userId }; + var chats = await _apiClient.PostAsync>("chat/get", body); + + if (chats != null) + { + _database.Set("chats", JsonConvert.SerializeObject(chats), chats); + } + return chats; + } + + public List GetFast() + { + var cachedData = _database.Get("chats", ""); + if (string.IsNullOrEmpty(cachedData)) return new List(); + + return JsonConvert.DeserializeObject>(cachedData); + } + public async Task GetUserAvailability(GetAvailabilityReq req) => + await _apiClient.PostAsync("chat/availability", req); + + public async Task ToggleMute(ToggleChatMuteReq req) => + await _apiClient.PostAsync("v2/chat/toggleMute", req); + + public async Task StartNew(StartNewReq req) => + await _apiClient.PostAsync("chat/startNew", req); + } +}