add db get queries
This commit is contained in:
@@ -2,11 +2,13 @@ package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
type World struct {
|
||||
Id int
|
||||
Id int64
|
||||
TextId string
|
||||
Title string
|
||||
Description string
|
||||
@@ -26,7 +28,7 @@ func (worlds Worlds) GetById(id int) *World {
|
||||
return &worlds[id]
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) InsertWorld(world *World, stateId int) error {
|
||||
func (m FoundryStateModel) InsertWorld(world *World, stateId int64) 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)
|
||||
@@ -44,7 +46,7 @@ func (m FoundryStateModel) InsertWorld(world *World, stateId int) error {
|
||||
return m.InsertWorldCompatibility(&world.Compatibility, world.Id)
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) InsertWorldCompatibility(compatibility *Compatibility, worldId int) error {
|
||||
func (m FoundryStateModel) InsertWorldCompatibility(compatibility *Compatibility, worldId int64) error {
|
||||
query := `
|
||||
INSERT INTO worlds_compatibility (world_id, minumum, verified, maximum)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
@@ -58,6 +60,97 @@ func (m FoundryStateModel) InsertWorldCompatibility(compatibility *Compatibility
|
||||
return m.DB.QueryRowContext(ctx, query, args...).Scan(&compatibility.Id)
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) GetWorlds(idState int64) (Worlds, error) {
|
||||
if idState < 1 {
|
||||
return nil, ErrorRecordNotFound
|
||||
}
|
||||
|
||||
query := `
|
||||
SELECT id, created_at, text_id, title, description, system, core_version, system_version, playtime, next_session
|
||||
FROM worlds
|
||||
WHERE state_id = $1`
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
rows, err := m.DB.QueryContext(ctx, query, idState)
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
return nil, ErrorRecordNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
worlds := make(Worlds, 0, 1)
|
||||
|
||||
for rows.Next() {
|
||||
var world World
|
||||
err := rows.Scan(
|
||||
&world.Id,
|
||||
&world.CreatedAt,
|
||||
&world.TextId,
|
||||
&world.Title,
|
||||
&world.Description,
|
||||
&world.System,
|
||||
&world.CoreVersion,
|
||||
&world.SystemVersion,
|
||||
&world.PlayTime,
|
||||
&world.NextSession,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
compatibility, err := m.GetWorldsCompatibility(world.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
world.Compatibility = *compatibility
|
||||
worlds = append(worlds, world)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return worlds, nil
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) GetWorldsCompatibility(idWorld int64) (*Compatibility, error) {
|
||||
if idWorld < 1 {
|
||||
return nil, ErrorRecordNotFound
|
||||
}
|
||||
|
||||
query := `
|
||||
SELECT id, minimum, verified, maximum
|
||||
FROM worlds_compatibility
|
||||
WHERE world_id = $1`
|
||||
|
||||
var compatibility Compatibility
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
err := m.DB.QueryRowContext(ctx, query, idWorld).Scan(
|
||||
&compatibility.Id,
|
||||
&compatibility.Minimum,
|
||||
&compatibility.Verified,
|
||||
&compatibility.Maximum,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
return nil, ErrorRecordNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &compatibility, nil
|
||||
}
|
||||
|
||||
// func (worlds Worlds) GetSessionTime(worldName string) (*time.Time, error) {
|
||||
// if worldName == "" && len(worlds) != 1 {
|
||||
// return nil, ErrorSetupNotFound
|
||||
|
||||
Reference in New Issue
Block a user