Stage 1 of migrating to MongoDb - fully added mongodb

This commit is contained in:
2024-12-15 17:01:48 +03:30
parent e0cdcf1198
commit fbe0d500d9
5 changed files with 13 additions and 13 deletions

View File

@@ -14,7 +14,7 @@ namespace mstdnCats.Services
try
{
// Initialize Backup DB
var store = new DataStore($"{_dbname + "_BK"}.json", minifyJson: true);
var store = new DataStore($"./data/{_dbname + "_BK"}.json", minifyJson: true);
collection = store.GetCollection<Post>();
}
catch
@@ -26,7 +26,7 @@ namespace mstdnCats.Services
return Task.FromResult(collection);
}
public static Task<IMongoDatabase> SetupDb(string mongoDbConnectionString, string dbName)
public static Task<IMongoCollection<Post>> SetupDb(string mongoDbConnectionString, string dbName)
{
if (mongoDbConnectionString == null)
{
@@ -36,7 +36,7 @@ namespace mstdnCats.Services
try
{
var client = new MongoClient(mongoDbConnectionString);
var database = client.GetDatabase(dbName);
var database = client.GetDatabase(dbName).GetCollection<Post>("posts");
return Task.FromResult(database);
}
catch (Exception ex)

View File

@@ -13,8 +13,8 @@ public class HandleDbBackup
{
logger?.LogInformation("Backup requested");
await using Stream stream = System.IO.File.OpenRead("./" + dbname+".json");
var message = await _bot.SendDocument(adminId, document: InputFile.FromStream(stream, dbname+".json"),
await using Stream stream = System.IO.File.OpenRead("./data/" + dbname+"_BK.json");
var message = await _bot.SendDocument(adminId, document: InputFile.FromStream(stream, dbname+"_BK.json"),
caption: "Backup of " + dbname + "\nCreated at " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss" + "\nCurrent post count: " + _db.AsQueryable().Count()), parseMode: ParseMode.Html);
logger?.LogInformation("Backup sent");

View File

@@ -1,5 +1,6 @@
using JsonFlatFileDataStore;
using Microsoft.Extensions.Logging;
using MongoDB.Driver;
using mstdnCats.Models;
using Telegram.Bot;
using Telegram.Bot.Types;
@@ -10,7 +11,7 @@ namespace CatsOfMastodonBot.Services
{
public class HandleStartMessage
{
public static async Task HandleStartMessageAsync(Message message, TelegramBotClient _bot, IDocumentCollection<Post> _db, ILogger<MastodonBot>? logger, CallbackQuery callbackQuery = null)
public static async Task HandleStartMessageAsync(Message message, TelegramBotClient _bot, IMongoCollection<Post> _db, ILogger<MastodonBot>? logger, CallbackQuery callbackQuery = null)
{
logger?.LogInformation("Start message received, trigger source: " + (callbackQuery != null ? "Callback" : "Start command"));