regio-ue1/app.go

48 lines
860 B
Go

package main
import (
"context"
"gegio-ue1/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
}
func (a *App) GetGames() (gs []model.Game, err error) {
err = a.db.Find(&gs).Error
return
}
func (a *App) GetTournaments() (ts []model.Tournament, err error) {
err = a.db.Preload("Game").Find(&ts).Error
return
}
func (a *App) GetTournament(id int) (t model.Tournament, err error) {
err = a.db.Preload("Game").First(&t, id).Error
return
}
func (a *App) SaveTournament(t model.Tournament) error {
err := a.db.Save(&t).Error
return err
}