tichu-counter/app.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

61 lines
1.0 KiB
Go

package main
import (
"context"
"fmt"
"tichu-counter/model"
"gorm.io/gorm"
)
// App struct
type App struct {
ctx context.Context
db *gorm.DB
}
// NewApp creates a new App application struct
func NewApp(db *gorm.DB) *App {
return &App{db: db}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
// Greet returns a greeting for the given name
func (a *App) Greet(name string) string {
return fmt.Sprintf("Hello %s, It's show time!", name)
}
func (a *App) GetLastGameID() uint {
var g model.Game
a.db.Order("updated_at DESC").First(&g)
return g.ID
}
func (a *App) GetGame(id uint) (game model.Game) {
a.db.Preload("Steps").First(&game, id)
game.SumPoints()
return
}
func (a *App) GetGames() (games []model.Game) {
a.db.Find(&games)
return
}
func (a *App) NewGame() (id uint) {
game := model.Game{
Steps: []model.Step{},
}
a.db.Save(&game)
return game.ID
}
func (a *App) SaveStep(s *model.Step) {
a.db.Save(s)
}