114 lines
2.0 KiB
Go
114 lines
2.0 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"time"
|
|
)
|
|
|
|
type StateType int
|
|
|
|
const (
|
|
AuthState = StateType(0)
|
|
SetupState = StateType(1)
|
|
JoinState = StateType(2)
|
|
PlayersState = StateType(3)
|
|
UpdateState = StateType(4)
|
|
LicenseState = StateType(5)
|
|
)
|
|
|
|
type FoundryState struct {
|
|
Id int
|
|
IsAdmin bool
|
|
IsSetup bool
|
|
Type StateType
|
|
CreatedAt time.Time
|
|
Options Options
|
|
Modules Modules
|
|
Systems Systems
|
|
Worlds Worlds
|
|
Users Users
|
|
}
|
|
|
|
type Compatibility struct {
|
|
Id int
|
|
Minimum string
|
|
Verified string
|
|
Maximum string
|
|
}
|
|
|
|
type Options struct {
|
|
Id int
|
|
Language string
|
|
}
|
|
|
|
type FoundryStateModel struct {
|
|
DB *sql.DB
|
|
}
|
|
|
|
func (m FoundryStateModel) Insert(state *FoundryState) error {
|
|
query := `
|
|
INSERT INTO foundry_state (is_admin, is_setup, state_type)
|
|
VALUES ($1, $2, $3)
|
|
RETURNING id, created_at`
|
|
|
|
args := []any{state.IsAdmin, state.IsSetup, state.Type}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
defer cancel()
|
|
|
|
err := m.DB.QueryRowContext(ctx, query, args...).Scan(&state.Id, &state.CreatedAt)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = m.InsertOptions(&state.Options, state.Id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for i := range state.Modules {
|
|
err = m.InsertModule(&state.Modules[i], state.Id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
for i := range state.Systems {
|
|
err = m.InsertSystem(&state.Systems[i], state.Id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
for i := range state.Worlds {
|
|
err = m.InsertWorld(&state.Worlds[i], state.Id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
for i := range state.Users {
|
|
err = m.InsertUser(&state.Users[i], state.Id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (m FoundryStateModel) InsertOptions(options *Options, stateId int) error {
|
|
query := `
|
|
INSERT INTO options (state_id, lang)
|
|
VALUES ($1, $2)
|
|
RETURNING id`
|
|
|
|
args := []any{stateId, options.Language}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
defer cancel()
|
|
|
|
return m.DB.QueryRowContext(ctx, query, args...).Scan(&options.Id)
|
|
}
|