diff --git a/api/handlers/bot.go b/api/handlers/bot.go index c0ac89b..6d56a34 100644 --- a/api/handlers/bot.go +++ b/api/handlers/bot.go @@ -10,6 +10,7 @@ import ( "github.com/masudur-rahman/expense-tracker-bot/configs" "github.com/masudur-rahman/expense-tracker-bot/models" + "github.com/masudur-rahman/expense-tracker-bot/modules/google" "github.com/masudur-rahman/expense-tracker-bot/pkg" "github.com/masudur-rahman/expense-tracker-bot/services/all" @@ -234,3 +235,16 @@ func ListExpenses(ctx telebot.Context) error { return ctx.Send(pkg.FormatDocuments(txns, "Timestamp", "Amount", "Type")) } + +func SyncSQLiteDatabase(ctx telebot.Context) error { + db := configs.TrackerConfig.Database + if !(db.Type == configs.DatabaseSQLite && db.SQLite.SyncToDrive) { + return ctx.Send("Database needs to be SQLite and sync needs to be enabled") + } + + if err := google.SyncDatabaseToDrive(); err != nil { + return ctx.Send(fmt.Sprintf("Database sync failed, reason: %v", err)) + } + + return ctx.Send("Database synced to google drive successfully") +} diff --git a/api/tele.go b/api/tele.go index aa4ca66..556bf3d 100644 --- a/api/tele.go +++ b/api/tele.go @@ -42,6 +42,8 @@ func TeleBotRoutes() (*telebot.Bot, error) { bot.Handle("/cat", handlers.TransactionCategoryCallback) + bot.Handle("/sync", handlers.SyncSQLiteDatabase) + return bot, nil } diff --git a/configs/config.go b/configs/config.go index 4a860a6..9a07bec 100644 --- a/configs/config.go +++ b/configs/config.go @@ -19,7 +19,7 @@ type DatabaseConfig struct { //ArangoDB DBConfigArangoDB `json:"arangodb" yaml:"arangodb"` Postgres lib.PostgresConfig `json:"postgres" yaml:"postgres"` - Sqlite DBConfigSqlite `json:"sqlite" yaml:"sqlite"` + SQLite DBConfigSQLite `json:"sqlite" yaml:"sqlite"` } type DatabaseType string @@ -27,7 +27,7 @@ type DatabaseType string const ( DatabaseArangoDB DatabaseType = "arangodb" DatabasePostgres DatabaseType = "postgres" - DatabaseSqlite DatabaseType = "sqlite" + DatabaseSQLite DatabaseType = "sqlite" DatabaseSupabase DatabaseType = "supabase" ) @@ -48,7 +48,7 @@ type DBConfigPostgres struct { SSLMode string `json:"sslmode" yaml:"sslmode"` } -type DBConfigSqlite struct { +type DBConfigSQLite struct { SyncToDrive bool `json:"syncToDrive" yaml:"syncToDrive"` DisableSyncFromDrive bool `json:"disableSyncFromDrive" yaml:"disableSyncFromDrive"` SyncInterval time.Duration `json:"syncInterval" yaml:"syncInterval"` diff --git a/configs/database.go b/configs/database.go index eb9e021..fa137da 100644 --- a/configs/database.go +++ b/configs/database.go @@ -28,18 +28,18 @@ func InitiateDatabaseConnection(ctx context.Context) error { return err } return initializeSQLServices(db) - case DatabaseSqlite, "": - if cfg.Sqlite.SyncToDrive { - if !cfg.Sqlite.DisableSyncFromDrive { + case DatabaseSQLite, "": + if cfg.SQLite.SyncToDrive { + if !cfg.SQLite.DisableSyncFromDrive { if err := google.SyncDatabaseFromDrive(); err != nil { return err } - logr.DefaultLogger.Infof("Sqlite database synced from google drive") + logr.DefaultLogger.Infof("SQLite database synced from google drive") } - go google.SyncDatabaseToDrivePeriodically(TrackerConfig.Database.Sqlite.SyncInterval) + go google.SyncDatabaseToDrivePeriodically(TrackerConfig.Database.SQLite.SyncInterval) } - db, err := getSqliteDatabase(ctx) + db, err := getSQLiteDatabase(ctx) if err != nil { return err } @@ -49,7 +49,7 @@ func InitiateDatabaseConnection(ctx context.Context) error { } } -func getSqliteDatabase(ctx context.Context) (isql.Database, error) { +func getSQLiteDatabase(ctx context.Context) (isql.Database, error) { conn, err := lib.GetSQLiteConnection("expense-tracker.db") if err != nil { return nil, err diff --git a/modules/google/drive.go b/modules/google/drive.go index b7ea14d..6dc87ad 100644 --- a/modules/google/drive.go +++ b/modules/google/drive.go @@ -138,7 +138,7 @@ func SyncDatabaseToDrivePeriodically(interval time.Duration) { logr.DefaultLogger.Errorw("Sync database to drive failed", "error", err.Error()) return } - logr.DefaultLogger.Infof("Sqlite database synced to google drive") + logr.DefaultLogger.Infof("SQLite database synced to google drive") ticker := time.NewTicker(interval) for { @@ -148,7 +148,7 @@ func SyncDatabaseToDrivePeriodically(interval time.Duration) { logr.DefaultLogger.Errorw("Sync database to drive failed", "error", err.Error()) return } - logr.DefaultLogger.Infof("Sqlite database synced to google drive") + logr.DefaultLogger.Infof("SQLite database synced to google drive") } } }