generated from schreifuchs/wails-template
78 lines
1.6 KiB
Go
78 lines
1.6 KiB
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").Preload("Participants").First(&t, id).Error
|
|
return
|
|
}
|
|
func (a *App) SaveTournament(t model.Tournament) error {
|
|
err := a.db.Save(&t).Error
|
|
return err
|
|
}
|
|
func (a *App) FillRandom(t model.Tournament) {
|
|
for range t.Size - len(t.Participants) {
|
|
t.Participants = append(t.Participants, &model.Participant{
|
|
Name: model.RandomName(),
|
|
IsTemporary: true,
|
|
IsTeam: true,
|
|
})
|
|
}
|
|
a.db.Save(&t.Participants)
|
|
a.db.Save(&t)
|
|
|
|
}
|
|
|
|
func (a *App) GetParticipants() (ps []model.Participant, err error) {
|
|
err = a.db.Find(&ps).Error
|
|
return
|
|
}
|
|
func (a *App) SaveParticipant(p model.Participant) error {
|
|
return a.db.Save(&p).Error
|
|
}
|
|
func (a *App) DeleteParticipat(p model.Participant) {
|
|
a.db.Delete(&p)
|
|
}
|
|
func (a *App) RemoveParticipantFromTournament(p model.Participant, t model.Tournament) {
|
|
if p.IsTemporary {
|
|
a.db.Delete(&p)
|
|
return
|
|
}
|
|
a.db.Model(&t).Association("Participants").Delete(&p)
|
|
}
|