implement ChatService with stateful architecture and caching
This commit is contained in:
@@ -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<List<Chat>> Get(GetChatsReq req);
|
|
||||||
Task<GetAvailabilityResp> GetUserAvailability(GetAvailabilityReq req);
|
|
||||||
Task ToggleMute(ToggleChatMuteReq req);
|
|
||||||
Task<Chat> 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<List<Chat>> Get(GetChatsReq req) =>
|
|
||||||
await _apiClient.PostAsync<GetChatsReq, List<Chat>>("chat/get", req);
|
|
||||||
|
|
||||||
public async Task<GetAvailabilityResp> GetUserAvailability(GetAvailabilityReq req) =>
|
|
||||||
await _apiClient.PostAsync<GetAvailabilityReq, GetAvailabilityResp>("chat/availability", req);
|
|
||||||
|
|
||||||
public async Task ToggleMute(ToggleChatMuteReq req) =>
|
|
||||||
await _apiClient.PostAsync<ToggleChatMuteReq, object>("v2/chat/toggleMute", req);
|
|
||||||
|
|
||||||
public async Task<Chat> StartNew(StartNewReq req) =>
|
|
||||||
await _apiClient.PostAsync<StartNewReq, Chat>("chat/startNew", req);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
62
Services/ChatService.cs
Normal file
62
Services/ChatService.cs
Normal file
@@ -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<List<Chat>> Get();
|
||||||
|
List<Chat> GetFast();
|
||||||
|
Task<GetAvailabilityResp> GetUserAvailability(GetAvailabilityReq req);
|
||||||
|
Task ToggleMute(ToggleChatMuteReq req);
|
||||||
|
Task<Chat> 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<List<Chat>> Get()
|
||||||
|
{
|
||||||
|
var body = new { userid = _userId };
|
||||||
|
var chats = await _apiClient.PostAsync<object, List<Chat>>("chat/get", body);
|
||||||
|
|
||||||
|
if (chats != null)
|
||||||
|
{
|
||||||
|
_database.Set("chats", JsonConvert.SerializeObject(chats), chats);
|
||||||
|
}
|
||||||
|
return chats;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Chat> GetFast()
|
||||||
|
{
|
||||||
|
var cachedData = _database.Get("chats", "");
|
||||||
|
if (string.IsNullOrEmpty(cachedData)) return new List<Chat>();
|
||||||
|
|
||||||
|
return JsonConvert.DeserializeObject<List<Chat>>(cachedData);
|
||||||
|
}
|
||||||
|
public async Task<GetAvailabilityResp> GetUserAvailability(GetAvailabilityReq req) =>
|
||||||
|
await _apiClient.PostAsync<GetAvailabilityReq, GetAvailabilityResp>("chat/availability", req);
|
||||||
|
|
||||||
|
public async Task ToggleMute(ToggleChatMuteReq req) =>
|
||||||
|
await _apiClient.PostAsync<ToggleChatMuteReq, object>("v2/chat/toggleMute", req);
|
||||||
|
|
||||||
|
public async Task<Chat> StartNew(StartNewReq req) =>
|
||||||
|
await _apiClient.PostAsync<StartNewReq, Chat>("chat/startNew", req);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user