Skip to content

Commit 70c8f56

Browse files
committed
added user settings repository.
1 parent aa792ca commit 70c8f56

File tree

6 files changed

+88
-1
lines changed

6 files changed

+88
-1
lines changed

RedmineTelegramBot.Core/Config/BotOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ public class BotOptions
1313
public string RedmineUrl { get; set; }
1414

1515
public string RedmineSecret { get; set; }
16+
17+
public string DataDirectory { get; set; }
1618
}
1719
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace RedmineTelegramBot.Core.Data
8+
{
9+
public interface IUserSettingsRepository
10+
{
11+
void StoreSettings(string username, UserSettings userSettings);
12+
13+
UserSettings GetSettings(string username);
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace RedmineTelegramBot.Core.Data
8+
{
9+
public class UserSettings
10+
{
11+
public string Username { get; set; }
12+
13+
public string RedmineSecret { get; set; }
14+
}
15+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Text.Json;
7+
using System.Threading.Tasks;
8+
9+
namespace RedmineTelegramBot.Core.Data
10+
{
11+
public class UsersSettingsFileRepository : IUserSettingsRepository
12+
{
13+
private readonly string _dataDirectoryPath;
14+
15+
public UsersSettingsFileRepository(string dataDirectoryPath)
16+
{
17+
_dataDirectoryPath = dataDirectoryPath;
18+
}
19+
20+
public UserSettings GetSettings(string username)
21+
{
22+
var path = MakePath(username);
23+
if (File.Exists(path))
24+
{
25+
var text = File.ReadAllText(path);
26+
return JsonSerializer.Deserialize<UserSettings>(text);
27+
}
28+
29+
return null;
30+
}
31+
32+
public void StoreSettings(string username, UserSettings userSettings)
33+
{
34+
var path = MakePath(username);
35+
var json = JsonSerializer.Serialize(userSettings);
36+
File.WriteAllText(path, json);
37+
}
38+
39+
private string MakePath(string username)
40+
{
41+
if (!Directory.Exists(_dataDirectoryPath))
42+
{
43+
Directory.CreateDirectory(_dataDirectoryPath);
44+
}
45+
46+
return Path.Combine(_dataDirectoryPath, $"user_settings_{username}.json");
47+
}
48+
}
49+
}

RedmineTelegramBot.Core/Modules/RedmineBotModule.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.Extensions.DependencyInjection;
22
using RedmineTelegramBot.Core.Config;
3+
using RedmineTelegramBot.Core.Data;
34
using Telegram.Bot;
45

56
namespace RedmineTelegramBot.Core.Modules
@@ -15,6 +16,10 @@ public RedmineBotModule(BotOptions configuration)
1516

1617
public void Load(IServiceCollection services)
1718
{
19+
// repositories
20+
services.AddTransient<IUserSettingsRepository>(c => new UsersSettingsFileRepository(_options.DataDirectory));
21+
22+
// app services
1823
services.AddTransient<IRedmineBot, RedmineBot>();
1924
services.AddSingleton<ITelegramBotClient>(c => new TelegramBotClient(_options.Token));
2025
services.AddTransient<IConversationFactory, ConversationFactory>();

RedmineTelegramBot.Standalone/appsettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"TelegramBot": {
33
"Token": "",
44
"RedmineUrl": "",
5-
"RedmineSecret": ""
5+
"RedmineSecret": "",
6+
"DataDirectory": ""
67
}
78
}

0 commit comments

Comments
 (0)