package model import ( "log" "os" "path" "gorm.io/driver/sqlite" "gorm.io/gorm" ) type Game struct { gorm.Model TeamA int `gorm:"-"` TeamB int `gorm:"-"` Steps []Step `gorm:"foreignKey:GameID"` } func (g *Game) SumPoints() { g.TeamA = 0 g.TeamB = 0 for _, s := range g.Steps { g.TeamA += s.PointsTeamA + s.AdderTeamA g.TeamB += s.PointsTeamB + s.AdderTeamB } } type Step struct { gorm.Model GameID uint Game Game `gorm:"foreignKey:GameID"` PointsTeamA int AdderTeamA int PointsTeamB int AdderTeamB int } func InitDB() *gorm.DB { home, err := os.UserHomeDir() if err != nil { panic(err) } db, err := gorm.Open(sqlite.Open(path.Join(home, "tichu.db"))) if err != nil { log.Panic(err) } db.AutoMigrate(Game{}, Step{}) return db }