add db get queries

This commit is contained in:
lbenedar
2026-04-07 13:54:45 +03:00
parent 095dac5db7
commit e48854c55b
10 changed files with 623 additions and 29 deletions

View File

@@ -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
}