2020-03-23 20:08:30 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
2020-04-02 16:17:19 +00:00
|
|
|
"time"
|
|
|
|
|
2020-03-23 20:08:30 +00:00
|
|
|
"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 {
|
2020-04-02 16:17:19 +00:00
|
|
|
ID uint `gorm:"primary_key"`
|
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
|
|
|
DeletedAt *time.Time `sql:"index"`
|
|
|
|
|
2020-03-23 20:08:30 +00:00
|
|
|
Code string `gorm:"unique_index"`
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
2020-04-02 16:17:19 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-03-23 20:08:30 +00:00
|
|
|
// 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
|
|
|
|
}
|
2020-04-02 16:17:19 +00:00
|
|
|
Db.AutoMigrate(&Betriebsstelle{}, &TGChat{})
|
2020-03-23 20:08:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the GORM database handle
|
|
|
|
func Close() {
|
|
|
|
Db.Close()
|
|
|
|
Db = nil
|
|
|
|
}
|