add db models, refactor json models, add references in migrations
This commit is contained in:
81
internal/foundry/models/db/world.go
Normal file
81
internal/foundry/models/db/world.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
type World struct {
|
||||
Id int
|
||||
TextId string
|
||||
Title string
|
||||
Description string
|
||||
System string
|
||||
CoreVersion string
|
||||
SystemVersion string
|
||||
LastPlayed string
|
||||
PlayTime int64
|
||||
NextSession time.Time
|
||||
CreatedAt time.Time
|
||||
Compatibility Compatibility
|
||||
}
|
||||
|
||||
type Worlds []World
|
||||
|
||||
func (worlds Worlds) GetById(id int) *World {
|
||||
return &worlds[id]
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) InsertWorld(world *World, stateId int) error {
|
||||
query := `
|
||||
INSERT INTO worlds (state_id, text_id, title, description, system, core_version, system_version, playtime, next_session)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||
RETURNING id, created_at`
|
||||
|
||||
args := []any{stateId, world.TextId, world.Title, world.Description, world.System, world.System, world.CoreVersion, world.SystemVersion, world.PlayTime, world.NextSession}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
err := m.DB.QueryRowContext(ctx, query, args...).Scan(&world.Id, &world.CreatedAt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return m.InsertWorldCompatibility(&world.Compatibility, world.Id)
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) InsertWorldCompatibility(compatibility *Compatibility, worldId int) error {
|
||||
query := `
|
||||
INSERT INTO worlds_compatibility (world_id, minumum, verified, maximum)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING id`
|
||||
|
||||
args := []any{worldId, compatibility.Minimum, compatibility.Verified, compatibility.Maximum}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
return m.DB.QueryRowContext(ctx, query, args...).Scan(&compatibility.Id)
|
||||
}
|
||||
|
||||
// func (worlds Worlds) GetSessionTime(worldName string) (*time.Time, error) {
|
||||
// if worldName == "" && len(worlds) != 1 {
|
||||
// return nil, ErrorSetupNotFound
|
||||
// }
|
||||
|
||||
// if worldName == "" {
|
||||
// return &worlds.GetById(0).NextSession, nil
|
||||
// } else {
|
||||
// for i := range worlds {
|
||||
// if worlds[i].Id == worldName {
|
||||
// return &worlds.GetById(0).NextSession, nil
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// return nil, ErrorSetupNotFound
|
||||
// }
|
||||
|
||||
// func (world World) GetSessionTime() *time.Time {
|
||||
// return &world.NextSession
|
||||
// }
|
||||
Reference in New Issue
Block a user