Remove database dependency. #100
This commit is contained in:
parent
62f8800652
commit
904939af71
|
|
@ -26,6 +26,7 @@ public partial class AgentService
|
|||
|
||||
var userAgentRecord = new UserAgentRecord
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
UserId = _user.Id,
|
||||
AgentId = record.Id,
|
||||
CreatedTime = DateTime.UtcNow,
|
||||
|
|
|
|||
|
|
@ -23,14 +23,6 @@ public static class BotSharpServiceCollectionExtensions
|
|||
services.AddScoped<IConversationService, ConversationService>();
|
||||
services.AddScoped<IConversationStateService, ConversationStateService>();
|
||||
|
||||
RegisterPlugins(services, config);
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection ConfigureBotSharpRepository<Tdb>(this IServiceCollection services, IConfiguration config)
|
||||
where Tdb : DataContext
|
||||
{
|
||||
var databaseSettings = new DatabaseSettings();
|
||||
config.Bind("Database", databaseSettings);
|
||||
services.AddSingleton((IServiceProvider x) => databaseSettings);
|
||||
|
|
@ -39,14 +31,28 @@ public static class BotSharpServiceCollectionExtensions
|
|||
config.Bind("Database", myDatabaseSettings);
|
||||
services.AddSingleton((IServiceProvider x) => myDatabaseSettings);
|
||||
|
||||
services.AddScoped((IServiceProvider x)
|
||||
=> DataContextHelper.GetDbContext<MongoDbContext, Tdb>(myDatabaseSettings, x));
|
||||
RegisterPlugins(services, config);
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection UsingSqlServer(this IServiceCollection services, IConfiguration config)
|
||||
{
|
||||
services.AddScoped<IBotSharpRepository>(sp =>
|
||||
{
|
||||
return myDatabaseSettings.Default == "FileRepository" ?
|
||||
new FileRepository(myDatabaseSettings, sp) :
|
||||
DataContextHelper.GetDbContext<BotSharpDbContext, Tdb>(myDatabaseSettings, sp);
|
||||
var myDatabaseSettings = sp.GetRequiredService<MyDatabaseSettings>();
|
||||
return DataContextHelper.GetDbContext<BotSharpDbContext, DbContext4SqlServer>(myDatabaseSettings, sp);
|
||||
});
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection UsingFileRepository(this IServiceCollection services, IConfiguration config)
|
||||
{
|
||||
services.AddScoped<IBotSharpRepository>(sp =>
|
||||
{
|
||||
var myDatabaseSettings = sp.GetRequiredService<MyDatabaseSettings>();
|
||||
return new FileRepository(myDatabaseSettings, sp);
|
||||
});
|
||||
|
||||
return services;
|
||||
|
|
|
|||
|
|
@ -70,7 +70,7 @@ public class FileRepository : IBotSharpRepository
|
|||
{
|
||||
get
|
||||
{
|
||||
if (_userAgents != null)
|
||||
if (_userAgents != null && _userAgents.Count > 0)
|
||||
{
|
||||
return _userAgents.AsQueryable();
|
||||
}
|
||||
|
|
@ -79,8 +79,12 @@ public class FileRepository : IBotSharpRepository
|
|||
_userAgents = new List<UserAgentRecord>();
|
||||
foreach (var d in Directory.GetDirectories(dir))
|
||||
{
|
||||
var json = File.ReadAllText(Path.Combine(d, "agents.json"));
|
||||
_userAgents.AddRange(JsonSerializer.Deserialize<List<UserAgentRecord>>(json, _options));
|
||||
var file = Path.Combine(d, "agents.json");
|
||||
if (Directory.Exists(d) && File.Exists(file))
|
||||
{
|
||||
var json = File.ReadAllText(Path.Combine(d, "agents.json"));
|
||||
_userAgents.AddRange(JsonSerializer.Deserialize<List<UserAgentRecord>>(json, _options));
|
||||
}
|
||||
}
|
||||
return _userAgents.AsQueryable();
|
||||
}
|
||||
|
|
@ -125,6 +129,16 @@ public class FileRepository : IBotSharpRepository
|
|||
_agents.Add(agent);
|
||||
_changedTableNames.Add(nameof(AgentRecord));
|
||||
}
|
||||
else if (entity is UserRecord user)
|
||||
{
|
||||
_users.Add(user);
|
||||
_changedTableNames.Add(nameof(UserRecord));
|
||||
}
|
||||
else if (entity is UserAgentRecord userAgent)
|
||||
{
|
||||
_userAgents.Add(userAgent);
|
||||
_changedTableNames.Add(nameof(UserAgentRecord));
|
||||
}
|
||||
}
|
||||
|
||||
List<string> _changedTableNames = new List<string>();
|
||||
|
|
@ -139,7 +153,7 @@ public class FileRepository : IBotSharpRepository
|
|||
if (table == nameof(ConversationRecord))
|
||||
{
|
||||
var convSettings = _services.GetService<ConversationSetting>();
|
||||
|
||||
|
||||
foreach (var conversation in _conversations)
|
||||
{
|
||||
var dir = Path.Combine(_dbSettings.FileRepository,
|
||||
|
|
@ -170,6 +184,34 @@ public class FileRepository : IBotSharpRepository
|
|||
File.WriteAllText(path, JsonSerializer.Serialize(agent, _options));
|
||||
}
|
||||
}
|
||||
else if (table == nameof(UserRecord))
|
||||
{
|
||||
foreach (var user in _users)
|
||||
{
|
||||
var dir = Path.Combine(_dbSettings.FileRepository,
|
||||
"users",
|
||||
user.Id);
|
||||
if (!Directory.Exists(dir))
|
||||
{
|
||||
Directory.CreateDirectory(dir);
|
||||
}
|
||||
var path = Path.Combine(dir, "user.json");
|
||||
File.WriteAllText(path, JsonSerializer.Serialize(user, _options));
|
||||
}
|
||||
}
|
||||
else if (table == nameof(UserAgentRecord))
|
||||
{
|
||||
_userAgents.GroupBy(x => x.UserId)
|
||||
.Select(x => x.Key).ToList()
|
||||
.ForEach(uid =>
|
||||
{
|
||||
var dir = Path.Combine(_dbSettings.FileRepository,
|
||||
"users",
|
||||
uid);
|
||||
var path = Path.Combine(dir, "agents.json");
|
||||
File.WriteAllText(path, JsonSerializer.Serialize(_userAgents.Where(x => x.UserId == uid), _options));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return _changedTableNames.Count;
|
||||
|
|
|
|||
|
|
@ -40,8 +40,12 @@ builder.Services.AddAuthentication(options =>
|
|||
builder.Services.AddScoped<IUserIdentity, UserIdentity>();
|
||||
|
||||
// Add BotSharp
|
||||
builder.Services.AddBotSharp(builder.Configuration)
|
||||
.ConfigureBotSharpRepository<DbContext4SqlServer>(builder.Configuration);
|
||||
builder.Services.AddBotSharp(builder.Configuration);
|
||||
|
||||
// Change below if you want to use other data storage.
|
||||
// builder.Services.UsingSqlServer(builder.Configuration);
|
||||
// Default is using File Storage
|
||||
builder.Services.UsingFileRepository(builder.Configuration);
|
||||
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
|
|
|
|||
|
|
@ -61,6 +61,7 @@
|
|||
"Master": "Data Source=(localdb)\\ProjectModels;Initial Catalog=BotSharp;Integrated Security=True;Connect Timeout=30;Encrypt=False;Trust Server Certificate=False;Application Intent=ReadWrite;Multi Subnet Failover=False",
|
||||
"Slavers": []
|
||||
},
|
||||
"FileRepository": "data",
|
||||
"UseCamelCase": true,
|
||||
"Assemblies": [ "BotSharp.Core" ]
|
||||
},
|
||||
|
|
|
|||
Loading…
Reference in a new issue