tichu-counter/model/model.go
schreifuchs b107b926a2
All checks were successful
build / windows (push) Successful in 5m37s
build / linux (push) Successful in 4m20s
simple POC
2025-03-10 11:25:38 +01:00

53 lines
776 B
Go

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
}