package database import ( "log" "time" "git.1750studios.com/ToddShepard/DB640/internal/config" "github.com/jinzhu/gorm" // SQLite dialect for gorm _ "github.com/jinzhu/gorm/dialects/sqlite" ) // Betriebsstelle defines the database model of the Betriebsstelle table type Betriebsstelle struct { ID uint `gorm:"primary_key"` CreatedAt time.Time `json:"-"` UpdatedAt time.Time `json:"-"` DeletedAt *time.Time `sql:"index" json:"-"` Code string `gorm:"unique_index"` Name string } // Betriebsstellen is an slice of Betriebsstelle type Betriebsstellen []Betriebsstelle // String outputs the Name of the Betriebssstelle func (b Betriebsstellen) String(i int) string { return b[i].Name } // Len outputs the length of the Betriebsstellen slice func (b Betriebsstellen) Len() int { return len(b) } // TGChat represents a telegram chat the bot is part of type TGChat struct { ID int64 `gorm:"primary_key"` CreatedAt time.Time UpdatedAt time.Time DeletedAt *time.Time `sql:"index"` FirstName string LastName string UserName string } // Db is the GORM database handle var Db *gorm.DB // Init initzialises the database func Init() { err := Open(config.C.Database.Dialect, config.C.Database.Connection) if err != nil { log.Fatalf("Could not establish database connection: %+v", err) } log.Printf("[DATABASE] Connection established\n") } // Open opens the database connection with given dialect and connection string func Open(dialect string, connection string) (err error) { Db, err = gorm.Open(dialect, connection) if err != nil { return err } Db.AutoMigrate(&Betriebsstelle{}, &TGChat{}) return nil } // Close closes the GORM database handle func Close() { Db.Close() Db = nil }