package db import ( "context" "time" ) type User struct { Id int Name string Role int Character string Color string Pronouns string CreatedAt time.Time Hotbar map[string]string Stats UserStats } type UserStats struct { Id int CoreVersion string SystemId string SystemVersion string CreatedTime int64 ModifiedTime int64 LastModifiedBy string } type Users []User func (users Users) GetById(id int) *User { return &users[id] } func (m FoundryStateModel) InsertUser(user *User, stateId int) error { query := ` INSERT INTO users (state_id, name, role, character, color, pronouns) VALUES ($1, $2, $3, $4, $5, $6) RETURNING id, created_at` args := []any{stateId, user.Name, user.Role, user.Character, user.Color, user.Pronouns} ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() err := m.DB.QueryRowContext(ctx, query, args...).Scan(&user.Id, &user.CreatedAt) if err != nil { return err } for k, v := range user.Hotbar { err = m.InsertUserHotbar(k, v, user.Id) if err != nil { return err } } return m.InsertUserStats(&user.Stats, user.Id) } func (m FoundryStateModel) InsertUserHotbar(key string, value string, userId int) error { query := ` INSERT INTO users_hotbar (user_id, key, value) VALUES ($1, $2, $3)` args := []any{userId, key, value} ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() _, err := m.DB.ExecContext(ctx, query, args...) if err != nil { return err } return nil } func (m FoundryStateModel) InsertUserStats(stats *UserStats, userId int) error { query := ` INSERT INTO users_stats (user_id, core_version, system_id, system_version, created_time, modified_time, last_modified_by) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING id` args := []any{userId, stats.CoreVersion, stats.SystemId, stats.SystemVersion, stats.CreatedTime, stats.ModifiedTime, stats.LastModifiedBy} ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() return m.DB.QueryRowContext(ctx, query, args...).Scan(&stats.Id) }