2023-09-01 22:33:36 +00:00
|
|
|
using BotSharp.Plugin.MongoStorage.Collections;
|
2023-08-26 04:41:01 +00:00
|
|
|
|
2023-09-01 22:33:36 +00:00
|
|
|
namespace BotSharp.Plugin.MongoStorage;
|
2023-08-26 04:41:01 +00:00
|
|
|
|
|
|
|
|
public class MongoDbContext
|
|
|
|
|
{
|
|
|
|
|
private readonly MongoClient _mongoClient;
|
|
|
|
|
private readonly string _mongoDbDatabaseName;
|
2023-09-12 22:34:50 +00:00
|
|
|
private readonly string _collectionPrefix;
|
2023-08-26 04:41:01 +00:00
|
|
|
|
2023-09-12 22:34:50 +00:00
|
|
|
public MongoDbContext(BotSharpDatabaseSettings dbSettings)
|
2023-08-26 04:41:01 +00:00
|
|
|
{
|
2023-10-16 22:52:48 +00:00
|
|
|
var mongoDbConnectionString = dbSettings.BotSharpMongoDb;
|
2023-08-26 04:41:01 +00:00
|
|
|
_mongoClient = new MongoClient(mongoDbConnectionString);
|
|
|
|
|
_mongoDbDatabaseName = GetDatabaseName(mongoDbConnectionString);
|
2023-10-16 22:52:48 +00:00
|
|
|
_collectionPrefix = dbSettings.TablePrefix.IfNullOrEmptyAs("BotSharp");
|
2023-08-26 04:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetDatabaseName(string mongoDbConnectionString)
|
|
|
|
|
{
|
|
|
|
|
var databaseName = mongoDbConnectionString.Substring(mongoDbConnectionString.LastIndexOf("/", StringComparison.InvariantCultureIgnoreCase) + 1);
|
|
|
|
|
if (databaseName.Contains("?"))
|
|
|
|
|
{
|
|
|
|
|
databaseName = databaseName.Substring(0, databaseName.IndexOf("?", StringComparison.InvariantCultureIgnoreCase));
|
|
|
|
|
}
|
|
|
|
|
return databaseName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IMongoDatabase Database { get { return _mongoClient.GetDatabase(_mongoDbDatabaseName); } }
|
|
|
|
|
|
|
|
|
|
public IMongoCollection<AgentCollection> Agents
|
2023-09-12 22:34:50 +00:00
|
|
|
=> Database.GetCollection<AgentCollection>($"{_collectionPrefix}_Agents");
|
2023-08-26 04:41:01 +00:00
|
|
|
|
|
|
|
|
public IMongoCollection<ConversationCollection> Conversations
|
2023-09-12 22:34:50 +00:00
|
|
|
=> Database.GetCollection<ConversationCollection>($"{_collectionPrefix}_Conversations");
|
2023-08-26 04:41:01 +00:00
|
|
|
|
2023-09-06 04:41:49 +00:00
|
|
|
public IMongoCollection<ConversationDialogCollection> ConversationDialogs
|
2023-09-12 22:34:50 +00:00
|
|
|
=> Database.GetCollection<ConversationDialogCollection>($"{_collectionPrefix}_ConversationDialogs");
|
2023-09-06 04:41:49 +00:00
|
|
|
|
2023-10-23 20:33:24 +00:00
|
|
|
public IMongoCollection<ExectionLogCollection> ExectionLogs
|
|
|
|
|
=> Database.GetCollection<ExectionLogCollection>($"{_collectionPrefix}_ExectionLogs");
|
|
|
|
|
|
2023-08-26 04:41:01 +00:00
|
|
|
public IMongoCollection<UserCollection> Users
|
2023-09-12 22:34:50 +00:00
|
|
|
=> Database.GetCollection<UserCollection>($"{_collectionPrefix}_Users");
|
2023-08-26 04:41:01 +00:00
|
|
|
|
|
|
|
|
public IMongoCollection<UserAgentCollection> UserAgents
|
2023-09-12 22:34:50 +00:00
|
|
|
=> Database.GetCollection<UserAgentCollection>($"{_collectionPrefix}_UserAgents");
|
2023-08-26 04:41:01 +00:00
|
|
|
}
|