generated from schreifuchs/wails-template
70 lines
1.2 KiB
Go
70 lines
1.2 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) DeleteGame(g *model.Game) {
|
|
a.db.Delete(g)
|
|
}
|
|
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)
|
|
}
|
|
func (a *App) UndoFor(game *model.Game) {
|
|
var step model.Step
|
|
a.db.Where("game_id = ?", game.ID).Order("created_at DESC").First(&step)
|
|
a.db.Delete(&step)
|
|
|
|
}
|