move out db logic into separate file

This commit is contained in:
Akulij 2025-03-25 11:34:19 +08:00
parent 3b329ebdd8
commit 7bba99c007
2 changed files with 47 additions and 38 deletions

47
cmd/app/db.go Normal file
View File

@ -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)
}

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"errors"
"fmt" "fmt"
"io" "io"
"log" "log"
@ -14,7 +13,6 @@ import (
"github.com/akulij/ticketbot/config" "github.com/akulij/ticketbot/config"
"gorm.io/driver/sqlite"
"gorm.io/gorm" "gorm.io/gorm"
) )
@ -34,12 +32,6 @@ func (u User) IsEffectiveAdmin() bool {
return u.RoleBitmask&0b10 == 0b10 return u.RoleBitmask&0b10 == 0b10
} }
type BotContent struct {
gorm.Model
Literal string
Content string
}
type BotController struct { type BotController struct {
cfg config.Config cfg config.Config
bot *tgbotapi.BotAPI bot *tgbotapi.BotAPI
@ -47,18 +39,6 @@ type BotController struct {
updates tgbotapi.UpdatesChannel 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 { func GetBotController() BotController {
cfg := config.GetConfig() cfg := config.GetConfig()
fmt.Printf("Token value: '%v'\n", cfg.BotToken) 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} 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() { func main() {
var bc = GetBotController() var bc = GetBotController()