finish world initialization, set up init on startup and on shutdown, move all models to main directory
This commit is contained in:
@@ -4,287 +4,185 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"time"
|
||||
"strings"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
type Module struct {
|
||||
Id int64
|
||||
TextId string
|
||||
Title string
|
||||
Description string
|
||||
Url string
|
||||
Version string
|
||||
Availability int
|
||||
CreatedAt time.Time
|
||||
Languages []Language
|
||||
Compatibility Compatibility
|
||||
ID string
|
||||
|
||||
Title string
|
||||
Description string
|
||||
URL string
|
||||
License string
|
||||
Readme string
|
||||
Bugs string
|
||||
Changelog string
|
||||
Version string
|
||||
Download string
|
||||
Manifest string
|
||||
Socket bool
|
||||
Protected bool
|
||||
Exclusive bool
|
||||
PersistentStorage bool
|
||||
CoreTranslation bool
|
||||
Library bool
|
||||
Locked bool
|
||||
Owned bool
|
||||
HasStorage bool
|
||||
Active bool
|
||||
Availability int
|
||||
DocumentTypes DocumentTypes
|
||||
Relationships Relationships
|
||||
Compatibility Compatibility
|
||||
Scripts []string
|
||||
Esmodules []string
|
||||
Tags []string
|
||||
Authors []*Author
|
||||
Media []*Media
|
||||
Styles []*Style
|
||||
Languages []*Language
|
||||
Packs []*Pack
|
||||
PackFolders []*Folder
|
||||
}
|
||||
|
||||
type Language struct {
|
||||
Id int64
|
||||
Lang string
|
||||
Name string
|
||||
Path string
|
||||
func (m *Module) Query(data *InsertId[uint]) {
|
||||
data.query = `
|
||||
INSERT INTO module (setup_id, id, title, description, url, license, readme, bugs,
|
||||
changelog, version, manifest, download, socket, protected, exclusive_, persistent_storage,
|
||||
core_translation, library, locked, owned, has_storage, active, availability)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17,
|
||||
$18, $19, $20, $21, $22, $23)
|
||||
ON CONFLICT(id) DO NOTHING`
|
||||
}
|
||||
|
||||
type Modules []Module
|
||||
|
||||
func (modules Modules) GetById(id int) *Module {
|
||||
return &modules[id]
|
||||
func (m *Module) ConnectGameQuery(data *InsertId[uint]) {
|
||||
data.query = `
|
||||
INSERT INTO game_to_module (game_id, module_id)
|
||||
VALUES ($1, $2)`
|
||||
}
|
||||
|
||||
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)
|
||||
RETURNING id, created_at`
|
||||
func (m *Module) InsertObjects(tx *sqlx.Tx) error {
|
||||
relId := InsertId[string]{id: m.ID, fieldName: "module_id"}
|
||||
group, ctx := errgroup.WithContext(context.Background())
|
||||
|
||||
args := []any{stateId, module.TextId, module.Title, module.Description, module.Url, module.Version, module.Availability}
|
||||
InsertWithCtxParallel(group, ctx, tx, m.DocumentTypes, relId)
|
||||
InsertWithCtxParallel(group, ctx, tx, m.Relationships, relId)
|
||||
InsertWithCtxParallel(group, ctx, tx, m.Compatibility, relId)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
scriptRelId := &InsertId[string]{id: m.ID, fieldName: "module_id", tableName: "scripts"}
|
||||
InsertSimpleSliceParallel(group, tx, m.Scripts, scriptRelId)
|
||||
esModulesRelId := &InsertId[string]{id: m.ID, fieldName: "module_id", tableName: "es_modules"}
|
||||
InsertSimpleSliceParallel(group, tx, m.Esmodules, esModulesRelId)
|
||||
tagsRelId := &InsertId[string]{id: m.ID, fieldName: "module_id", tableName: "tags"}
|
||||
InsertSimpleSliceParallel(group, tx, m.Tags, tagsRelId)
|
||||
|
||||
err := m.DB.QueryRowContext(ctx, query, args...).Scan(&module.Id, &module.CreatedAt)
|
||||
InsertSliceParallel(group, tx, m.Authors, relId)
|
||||
InsertSliceParallel(group, tx, m.Media, relId)
|
||||
InsertSliceParallel(group, tx, m.Styles, relId)
|
||||
InsertSliceParallel(group, tx, m.Languages, relId)
|
||||
InsertSliceParallel(group, tx, m.Packs, relId)
|
||||
InsertSliceParallel(group, tx, m.PackFolders, relId)
|
||||
|
||||
err := group.Wait()
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Module) ConnectGame(tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, m.ID}
|
||||
|
||||
_, err := tx.Exec(data.query, args...)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *Module) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
var dataId *uint
|
||||
if strings.EqualFold(data.fieldName, "setup_id") {
|
||||
dataId = &data.id
|
||||
}
|
||||
|
||||
args := []any{dataId, m.ID, m.Title, m.Description, m.URL, m.License, m.Readme, m.Bugs,
|
||||
m.Changelog, m.Version, m.Manifest, m.Download, m.Socket, m.Protected, m.Exclusive, m.PersistentStorage,
|
||||
m.CoreTranslation, m.Library, m.Locked, m.Owned, m.HasStorage, m.Active, m.Availability}
|
||||
|
||||
res, err := tx.Exec(data.query, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for i := range module.Languages {
|
||||
err = m.InsertModuleLanguages(&module.Languages[i], module.Id)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
|
||||
rowsAffected, err := res.RowsAffected()
|
||||
if rowsAffected != 0 {
|
||||
err = m.InsertObjects(tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return m.InsertModuleCompatibility(&module.Compatibility, module.Id)
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) InsertModuleCompatibility(compatibility *Compatibility, moduleId int64) error {
|
||||
query := `
|
||||
INSERT INTO modules_compatibility (module_id, minimum, verified, maximum)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING id`
|
||||
|
||||
args := []any{moduleId, compatibility.Minimum, compatibility.Verified, compatibility.Maximum}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
err := m.DB.QueryRowContext(ctx, query, args...).Scan(&compatibility.Id)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) InsertModuleLanguages(lang *Language, moduleId int64) error {
|
||||
query := `
|
||||
INSERT INTO modules_languages (module_id, language, name, path)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING id`
|
||||
|
||||
args := []any{moduleId, lang.Lang, lang.Name, lang.Path}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
err := m.DB.QueryRowContext(ctx, query, args...).Scan(&lang.Id)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) GetModules(idState int64) (Modules, error) {
|
||||
if idState < 1 {
|
||||
return nil, ErrorRecordNotFound
|
||||
if dataId == nil {
|
||||
dataCopy := *data
|
||||
m.ConnectGameQuery(&dataCopy)
|
||||
err = m.ConnectGame(tx, &dataCopy)
|
||||
}
|
||||
|
||||
query := `
|
||||
SELECT id, created_at, text_id, title, description, url, version, availability
|
||||
FROM modules
|
||||
WHERE state_id = $1`
|
||||
return err
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
func (m *Module) ConnectGameCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
rows, err := m.DB.QueryContext(ctx, query, idState)
|
||||
args := []any{data.id, m.ID}
|
||||
|
||||
_, err := tx.ExecContext(ctx, data.query, args...)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *Module) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
var dataId *uint
|
||||
if strings.EqualFold(data.fieldName, "setup_id") {
|
||||
dataId = &data.id
|
||||
}
|
||||
|
||||
args := []any{dataId, m.ID, m.Title, m.Description, m.URL, m.License, m.Readme, m.Bugs,
|
||||
m.Changelog, m.Version, m.Manifest, m.Download, m.Socket, m.Protected, m.Exclusive, m.PersistentStorage,
|
||||
m.CoreTranslation, m.Library, m.Locked, m.Owned, m.HasStorage, m.Active, m.Availability}
|
||||
|
||||
res, err := tx.ExecContext(ctx, data.query, args...)
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
return nil, ErrorRecordNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
return 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,
|
||||
)
|
||||
rowsAffected, err := res.RowsAffected()
|
||||
if rowsAffected != 0 {
|
||||
err = m.InsertObjects(tx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return 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
|
||||
if dataId == nil {
|
||||
dataCopy := *data
|
||||
m.ConnectGameQuery(&dataCopy)
|
||||
err = m.ConnectGameCtx(ctx, tx, &dataCopy)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) DeleteModules(idState int64) error {
|
||||
if idState < 1 {
|
||||
return ErrorRecordNotFound
|
||||
}
|
||||
|
||||
query := `
|
||||
DELETE FROM modules
|
||||
WHERE state_id = $1`
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
result, err := m.DB.ExecContext(ctx, query, idState)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if rowsAffected == 0 {
|
||||
return ErrorRecordNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m FoundryStateModel) DeleteModule(idModule int64) error {
|
||||
if idModule < 1 {
|
||||
return ErrorRecordNotFound
|
||||
}
|
||||
|
||||
query := `
|
||||
DELETE FROM modules
|
||||
WHERE id = $1`
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
result, err := m.DB.ExecContext(ctx, query, idModule)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if rowsAffected == 0 {
|
||||
return ErrorRecordNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user