Skip to content

Commit

Permalink
Add sync handle to sync via sending telegram command
Browse files Browse the repository at this point in the history
Signed-off-by: Masudur Rahman <[email protected]>
  • Loading branch information
masudur-rahman committed Feb 23, 2024
1 parent 9756a52 commit 6b05105
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
14 changes: 14 additions & 0 deletions api/handlers/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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")
}
2 changes: 2 additions & 0 deletions api/tele.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ func TeleBotRoutes() (*telebot.Bot, error) {

bot.Handle("/cat", handlers.TransactionCategoryCallback)

bot.Handle("/sync", handlers.SyncSQLiteDatabase)

return bot, nil
}

Expand Down
6 changes: 3 additions & 3 deletions configs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ 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

const (
DatabaseArangoDB DatabaseType = "arangodb"
DatabasePostgres DatabaseType = "postgres"
DatabaseSqlite DatabaseType = "sqlite"
DatabaseSQLite DatabaseType = "sqlite"
DatabaseSupabase DatabaseType = "supabase"
)

Expand All @@ -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"`
Expand Down
14 changes: 7 additions & 7 deletions configs/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions modules/google/drive.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
}
}
}

0 comments on commit 6b05105

Please sign in to comment.