From 7bba99c007901d12c70729673974242706e2315e Mon Sep 17 00:00:00 2001 From: Akulij Date: Tue, 25 Mar 2025 11:34:19 +0800 Subject: [PATCH] move out db logic into separate file --- cmd/app/db.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ cmd/app/main.go | 38 -------------------------------------- 2 files changed, 47 insertions(+), 38 deletions(-) create mode 100644 cmd/app/db.go diff --git a/cmd/app/db.go b/cmd/app/db.go new file mode 100644 index 0000000..0ee1584 --- /dev/null +++ b/cmd/app/db.go @@ -0,0 +1,47 @@ +package main + +import ( + "errors" + + "gorm.io/driver/sqlite" + "gorm.io/gorm" +) + +type BotContent struct { + gorm.Model + Literal string + Content string +} + +func DBMigrate() (*gorm.DB, error) { + db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{}) + if err != nil { + return db, err + } + + db.AutoMigrate(&User{}) + db.AutoMigrate(&BotContent{}) + + return db, err +} + +func (bc BotController) GetBotContentVerbose(Literal string) (string, error) { + var c BotContent + bc.db.First(&c, "Literal", Literal) + if c == (BotContent{}) { + return "[Unitialized] Init in Admin panel! Literal: " + Literal, errors.New("No content") + } + return c.Content, nil +} + +func (bc BotController) GetBotContent(Literal string) string { + content, _ := bc.GetBotContentVerbose(Literal) + return content +} + +func (bc BotController) SetBotContent(Literal string, Content string) { + c := BotContent{Literal: Literal, Content: Content} + bc.db.FirstOrCreate(&c, "Literal", Literal) + bc.db.Model(&c).Update("Content", Content) +} + diff --git a/cmd/app/main.go b/cmd/app/main.go index 8508285..ba10d51 100644 --- a/cmd/app/main.go +++ b/cmd/app/main.go @@ -1,7 +1,6 @@ package main import ( - "errors" "fmt" "io" "log" @@ -14,7 +13,6 @@ import ( "github.com/akulij/ticketbot/config" - "gorm.io/driver/sqlite" "gorm.io/gorm" ) @@ -34,12 +32,6 @@ func (u User) IsEffectiveAdmin() bool { return u.RoleBitmask&0b10 == 0b10 } -type BotContent struct { - gorm.Model - Literal string - Content string -} - type BotController struct { cfg config.Config bot *tgbotapi.BotAPI @@ -47,18 +39,6 @@ type BotController struct { updates tgbotapi.UpdatesChannel } -func DBMigrate() (*gorm.DB, error) { - db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{}) - if err != nil { - return db, err - } - - db.AutoMigrate(&User{}) - db.AutoMigrate(&BotContent{}) - - return db, err -} - func GetBotController() BotController { cfg := config.GetConfig() fmt.Printf("Token value: '%v'\n", cfg.BotToken) @@ -86,24 +66,6 @@ func GetBotController() BotController { return BotController{cfg: cfg, bot: bot, db: db, updates: updates} } -func (bc BotController) GetBotContentVerbose(Literal string) (string, error) { - var c BotContent - bc.db.First(&c, "Literal", Literal) - if c == (BotContent{}) { - return "[Unitialized] Init in Admin panel! Literal: " + Literal, errors.New("No content") - } - return c.Content, nil -} -func (bc BotController) GetBotContent(Literal string) string { - content, _ := bc.GetBotContentVerbose(Literal) - return content -} -func (bc BotController) SetBotContent(Literal string, Content string) { - c := BotContent{Literal: Literal, Content: Content} - bc.db.FirstOrCreate(&c, "Literal", Literal) - bc.db.Model(&c).Update("Content", Content) -} - func main() { var bc = GetBotController()