DB640/internal/database/database.go

66 lines
1.3 KiB
Go

package database
import (
"time"
"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
UpdatedAt time.Time
DeletedAt *time.Time `sql:"index"`
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
// 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
}