add db get queries
This commit is contained in:
@@ -5,4 +5,5 @@ import "errors"
|
||||
var (
|
||||
ErrorSetupMoreThanOne = errors.New("Passed more than one setupData")
|
||||
ErrorSetupNotFound = errors.New("Not found setup data from your request")
|
||||
ErrorRecordNotFound = errors.New("Record not found")
|
||||
)
|
||||
|
||||
@@ -3,6 +3,7 @@ package db
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -18,7 +19,7 @@ const (
|
||||
)
|
||||
|
||||
type FoundryState struct {
|
||||
Id int
|
||||
Id int64
|
||||
IsAdmin bool
|
||||
IsSetup bool
|
||||
Type StateType
|
||||
@@ -31,14 +32,14 @@ type FoundryState struct {
|
||||
}
|
||||
|
||||
type Compatibility struct {
|
||||
Id int
|
||||
Id int64
|
||||
Minimum string
|
||||
Verified string
|
||||
Maximum string
|
||||
}
|
||||
|
||||
type Options struct {
|
||||
Id int
|
||||
Id int64
|
||||
Language string
|
||||
}
|
||||
|
||||
@@ -98,7 +99,7 @@ func (m FoundryStateModel) Insert(state *FoundryState) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) InsertOptions(options *Options, stateId int) error {
|
||||
func (m FoundryStateModel) InsertOptions(options *Options, stateId int64) error {
|
||||
query := `
|
||||
INSERT INTO options (state_id, lang)
|
||||
VALUES ($1, $2)
|
||||
@@ -111,3 +112,121 @@ func (m FoundryStateModel) InsertOptions(options *Options, stateId int) error {
|
||||
|
||||
return m.DB.QueryRowContext(ctx, query, args...).Scan(&options.Id)
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) Get(id int64) (*FoundryState, error) {
|
||||
if id < 1 {
|
||||
return nil, ErrorRecordNotFound
|
||||
}
|
||||
|
||||
query := `
|
||||
SELECT id, created_at, is_admin, is_setup, state_type
|
||||
FROM foundry_state
|
||||
WHERE id = $1`
|
||||
|
||||
var state FoundryState
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
err := m.DB.QueryRowContext(ctx, query, id).Scan(
|
||||
&state.Id,
|
||||
&state.CreatedAt,
|
||||
&state.IsAdmin,
|
||||
&state.IsSetup,
|
||||
&state.Type,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
return nil, ErrorRecordNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
state.Modules, err = m.GetModules(state.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
state.Systems, err = m.GetSystems(state.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
state.Worlds, err = m.GetWorlds(state.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
state.Users, err = m.GetUsers(state.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
options, err := m.GetOptions(state.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
state.Options = *options
|
||||
|
||||
return &state, nil
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) GetOptions(idState int64) (*Options, error) {
|
||||
if idState < 1 {
|
||||
return nil, ErrorRecordNotFound
|
||||
}
|
||||
|
||||
query := `
|
||||
SELECT id, lang
|
||||
FROM options
|
||||
WHERE state_id = $1`
|
||||
|
||||
var options Options
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
err := m.DB.QueryRowContext(ctx, query, idState).Scan(
|
||||
&options.Id,
|
||||
&options.Language,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
return nil, ErrorRecordNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &options, nil
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) GetIdByType(stateType StateType) (int64, error) {
|
||||
if stateType < 0 {
|
||||
return -1, ErrorRecordNotFound
|
||||
}
|
||||
|
||||
query := `
|
||||
SELECT id
|
||||
FROM foundry_state
|
||||
WHERE state_type = $1`
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
var id int64
|
||||
|
||||
err := m.DB.QueryRowContext(ctx, query, stateType).Scan(&id)
|
||||
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
return -1, ErrorRecordNotFound
|
||||
default:
|
||||
return -1, err
|
||||
}
|
||||
}
|
||||
|
||||
return id, nil
|
||||
}
|
||||
|
||||
@@ -2,11 +2,13 @@ package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Module struct {
|
||||
Id int
|
||||
Id int64
|
||||
TextId string
|
||||
Title string
|
||||
Description string
|
||||
@@ -19,7 +21,7 @@ type Module struct {
|
||||
}
|
||||
|
||||
type Language struct {
|
||||
Id string
|
||||
Id int64
|
||||
Lang string
|
||||
Name string
|
||||
Path string
|
||||
@@ -31,7 +33,7 @@ func (modules Modules) GetById(id int) *Module {
|
||||
return &modules[id]
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) InsertModule(module *Module, stateId int) error {
|
||||
func (m FoundryStateModel) InsertModule(module *Module, stateId int64) error {
|
||||
query := `
|
||||
INSERT INTO modules (state_id, text_id, title, description, url, version, availability)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
@@ -55,7 +57,7 @@ func (m FoundryStateModel) InsertModule(module *Module, stateId int) error {
|
||||
return m.InsertModuleCompatibility(&module.Compatibility, module.Id)
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) InsertModuleCompatibility(compatibility *Compatibility, moduleId int) error {
|
||||
func (m FoundryStateModel) InsertModuleCompatibility(compatibility *Compatibility, moduleId int64) error {
|
||||
query := `
|
||||
INSERT INTO modules_compatibility (model_id, minumum, verified, maximum)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
@@ -69,7 +71,7 @@ func (m FoundryStateModel) InsertModuleCompatibility(compatibility *Compatibilit
|
||||
return m.DB.QueryRowContext(ctx, query, args...).Scan(&compatibility.Id)
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) InsertModuleLanguages(lang *Language, moduleId int) error {
|
||||
func (m FoundryStateModel) InsertModuleLanguages(lang *Language, moduleId int64) error {
|
||||
query := `
|
||||
INSERT INTO modules_compatibility (model_id, language, name, path)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
@@ -82,3 +84,143 @@ func (m FoundryStateModel) InsertModuleLanguages(lang *Language, moduleId int) e
|
||||
|
||||
return m.DB.QueryRowContext(ctx, query, args...).Scan(&lang.Id)
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) GetModules(idState int64) (Modules, error) {
|
||||
if idState < 1 {
|
||||
return nil, ErrorRecordNotFound
|
||||
}
|
||||
|
||||
query := `
|
||||
SELECT id, created_at, text_id, title, description, url, version, availability
|
||||
FROM modules
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
modules := make(Modules, 0, 1)
|
||||
|
||||
for rows.Next() {
|
||||
var module Module
|
||||
err := rows.Scan(
|
||||
&module.Id,
|
||||
&module.CreatedAt,
|
||||
&module.TextId,
|
||||
&module.Title,
|
||||
&module.Description,
|
||||
&module.Url,
|
||||
&module.Version,
|
||||
&module.Availability,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
module.Languages, err = m.GetModuleLanguages(module.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
compatibility, err := m.GetModuleCompatibility(module.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
module.Compatibility = *compatibility
|
||||
modules = append(modules, module)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return modules, nil
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) GetModuleCompatibility(idModule int64) (*Compatibility, error) {
|
||||
if idModule < 1 {
|
||||
return nil, ErrorRecordNotFound
|
||||
}
|
||||
|
||||
query := `
|
||||
SELECT id, minimum, verified, maximum
|
||||
FROM modules_compatibility
|
||||
WHERE module_id = $1`
|
||||
|
||||
var compatibility Compatibility
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
err := m.DB.QueryRowContext(ctx, query, idModule).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 (m FoundryStateModel) GetModuleLanguages(idModule int64) ([]Language, error) {
|
||||
if idModule < 1 {
|
||||
return nil, ErrorRecordNotFound
|
||||
}
|
||||
|
||||
query := `
|
||||
SELECT id, language, name, path
|
||||
FROM modules_languages
|
||||
WHERE module_id = $1`
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
rows, err := m.DB.QueryContext(ctx, query, idModule)
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
return nil, ErrorRecordNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
languages := make([]Language, 0, 1)
|
||||
|
||||
for rows.Next() {
|
||||
var lang Language
|
||||
err := rows.Scan(
|
||||
&lang.Id,
|
||||
&lang.Lang,
|
||||
&lang.Name,
|
||||
&lang.Path,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
languages = append(languages, lang)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return languages, nil
|
||||
}
|
||||
|
||||
@@ -2,11 +2,13 @@ package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
type System struct {
|
||||
Id int
|
||||
Id int64
|
||||
TextId string
|
||||
Title string
|
||||
Description string
|
||||
@@ -22,7 +24,7 @@ func (systems Systems) GetById(id int) *System {
|
||||
return &systems[id]
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) InsertSystem(system *System, stateId int) error {
|
||||
func (m FoundryStateModel) InsertSystem(system *System, stateId int64) error {
|
||||
query := `
|
||||
INSERT INTO systems (state_id, text_id, title, description, url, download)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)`
|
||||
@@ -39,7 +41,7 @@ func (m FoundryStateModel) InsertSystem(system *System, stateId int) error {
|
||||
return m.InsertModuleCompatibility(&system.Compatibility, system.Id)
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) InsertSystenCompatibility(compatibility *Compatibility, systemId int) error {
|
||||
func (m FoundryStateModel) InsertSystenCompatibility(compatibility *Compatibility, systemId int64) error {
|
||||
query := `
|
||||
INSERT INTO systems_compatibility (system_id, minumum, verified, maximum)
|
||||
VALUES ($1, $2, $3, $4)`
|
||||
@@ -51,3 +53,91 @@ func (m FoundryStateModel) InsertSystenCompatibility(compatibility *Compatibilit
|
||||
|
||||
return m.DB.QueryRowContext(ctx, query, args...).Scan(&compatibility.Id)
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) GetSystems(idState int64) (Systems, error) {
|
||||
if idState < 1 {
|
||||
return nil, ErrorRecordNotFound
|
||||
}
|
||||
|
||||
query := `
|
||||
SELECT id, created_at, text_id, title, description, url, download
|
||||
FROM systems
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
systems := make(Systems, 0, 1)
|
||||
|
||||
for rows.Next() {
|
||||
var system System
|
||||
err := rows.Scan(
|
||||
&system.Id,
|
||||
&system.CreatedAt,
|
||||
&system.TextId,
|
||||
&system.Title,
|
||||
&system.Description,
|
||||
&system.Url,
|
||||
&system.Download,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
compatibility, err := m.GetModuleCompatibility(system.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
system.Compatibility = *compatibility
|
||||
systems = append(systems, system)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return systems, nil
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) GetSystemCompatibility(idSystem int64) (*Compatibility, error) {
|
||||
if idSystem < 1 {
|
||||
return nil, ErrorRecordNotFound
|
||||
}
|
||||
|
||||
query := `
|
||||
SELECT id, minimum, verified, maximum
|
||||
FROM systems_compatibility
|
||||
WHERE system_id = $1`
|
||||
|
||||
var compatibility Compatibility
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
err := m.DB.QueryRowContext(ctx, query, idSystem).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
|
||||
}
|
||||
|
||||
@@ -2,11 +2,13 @@ package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
Id int
|
||||
Id int64
|
||||
Name string
|
||||
Role int
|
||||
Character string
|
||||
@@ -18,7 +20,7 @@ type User struct {
|
||||
}
|
||||
|
||||
type UserStats struct {
|
||||
Id int
|
||||
Id int64
|
||||
CoreVersion string
|
||||
SystemId string
|
||||
SystemVersion string
|
||||
@@ -33,7 +35,7 @@ func (users Users) GetById(id int) *User {
|
||||
return &users[id]
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) InsertUser(user *User, stateId int) error {
|
||||
func (m FoundryStateModel) InsertUser(user *User, stateId int64) error {
|
||||
query := `
|
||||
INSERT INTO users (state_id, name, role, character, color, pronouns)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
@@ -59,7 +61,7 @@ func (m FoundryStateModel) InsertUser(user *User, stateId int) error {
|
||||
return m.InsertUserStats(&user.Stats, user.Id)
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) InsertUserHotbar(key string, value string, userId int) error {
|
||||
func (m FoundryStateModel) InsertUserHotbar(key string, value string, userId int64) error {
|
||||
query := `
|
||||
INSERT INTO users_hotbar (user_id, key, value)
|
||||
VALUES ($1, $2, $3)`
|
||||
@@ -76,7 +78,7 @@ func (m FoundryStateModel) InsertUserHotbar(key string, value string, userId int
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) InsertUserStats(stats *UserStats, userId int) error {
|
||||
func (m FoundryStateModel) InsertUserStats(stats *UserStats, userId int64) 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)
|
||||
@@ -89,3 +91,150 @@ func (m FoundryStateModel) InsertUserStats(stats *UserStats, userId int) error {
|
||||
|
||||
return m.DB.QueryRowContext(ctx, query, args...).Scan(&stats.Id)
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) GetUsers(idState int64) (Users, error) {
|
||||
if idState < 1 {
|
||||
return nil, ErrorRecordNotFound
|
||||
}
|
||||
|
||||
query := `
|
||||
SELECT id, created_at, name, role, character, color, pronouns
|
||||
FROM users
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
users := make(Users, 0, 1)
|
||||
|
||||
for rows.Next() {
|
||||
var user User
|
||||
err := rows.Scan(
|
||||
&user.Id,
|
||||
&user.CreatedAt,
|
||||
&user.Name,
|
||||
&user.Role,
|
||||
&user.Character,
|
||||
&user.Color,
|
||||
&user.Pronouns,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
user.Hotbar, err = m.GetUserHotbar(user.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
userState, err := m.GetUserStats(user.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
user.Stats = *userState
|
||||
users = append(users, user)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) GetUserHotbar(idUser int64) (map[string]string, error) {
|
||||
if idUser < 1 {
|
||||
return nil, ErrorRecordNotFound
|
||||
}
|
||||
|
||||
query := `
|
||||
SELECT key, value
|
||||
FROM users_hotbar
|
||||
WHERE user_id = $1`
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
rows, err := m.DB.QueryContext(ctx, query, idUser)
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
return nil, ErrorRecordNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
hotbar := make(map[string]string)
|
||||
for rows.Next() {
|
||||
var key string
|
||||
var val string
|
||||
|
||||
err := rows.Scan(
|
||||
&key,
|
||||
&val,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
hotbar[key] = val
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
return nil, ErrorRecordNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return hotbar, nil
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) GetUserStats(idSystem int64) (*UserStats, error) {
|
||||
if idSystem < 1 {
|
||||
return nil, ErrorRecordNotFound
|
||||
}
|
||||
|
||||
query := `
|
||||
SELECT id, core_version, system_id, system_version, created_time, modified_time, last_modified_by
|
||||
FROM users_stats
|
||||
WHERE user_id = $1`
|
||||
|
||||
var userStats UserStats
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
err := m.DB.QueryRowContext(ctx, query, idSystem).Scan(
|
||||
&userStats.Id,
|
||||
&userStats.CoreVersion,
|
||||
&userStats.SystemId,
|
||||
&userStats.SystemVersion,
|
||||
&userStats.CreatedTime,
|
||||
&userStats.ModifiedTime,
|
||||
&userStats.LastModifiedBy,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
return nil, ErrorRecordNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &userStats, nil
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -139,6 +139,10 @@ func (state *FoundryState) GetWorlds() db.Worlds {
|
||||
}
|
||||
worldsCopy = append(worldsCopy, worldCopy)
|
||||
}
|
||||
|
||||
if state.World != nil {
|
||||
worldsCopy = append(worldsCopy, *state.GetWorld())
|
||||
}
|
||||
return worldsCopy
|
||||
}
|
||||
|
||||
|
||||
@@ -39,7 +39,3 @@ type Users []User
|
||||
func (users Users) GetById(id int) *User {
|
||||
return &users[id]
|
||||
}
|
||||
|
||||
func (state *FoundryState) GetUsers() *Users {
|
||||
return &state.Users
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user