finish world initialization, set up init on startup and on shutdown, move all models to main directory
This commit is contained in:
88
internal/foundry/models/db/index.go
Normal file
88
internal/foundry/models/db/index.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
type Index struct {
|
||||
ID string
|
||||
|
||||
Folder string
|
||||
Img string
|
||||
Name string
|
||||
Type string
|
||||
}
|
||||
|
||||
func (i *Index) Query(data *InsertId[string]) {
|
||||
data.query = `
|
||||
INSERT INTO index_ (id, folder, img, name, type)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
ON CONFLICT(id) DO NOTHING`
|
||||
}
|
||||
|
||||
func (i *Index) ConnectGameQuery(data *InsertId[string]) {
|
||||
data.query = `
|
||||
INSERT INTO pack_to_index (pack_id, index_id)
|
||||
VALUES ($1, $2)
|
||||
ON CONFLICT(pack_id, index_id) DO NOTHING`
|
||||
}
|
||||
|
||||
func (i *Index) ConnectGame(tx *sqlx.Tx, data *InsertId[string]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, i.ID}
|
||||
|
||||
_, err := tx.Exec(data.query, args...)
|
||||
return err
|
||||
}
|
||||
|
||||
func (i *Index) Insert(tx *sqlx.Tx, data *InsertId[string]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{i.ID, i.Folder, i.Img, i.Name, i.Type}
|
||||
|
||||
_, err := tx.Exec(data.query, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dataCopy := *data
|
||||
i.ConnectGameQuery(&dataCopy)
|
||||
|
||||
return i.ConnectGame(tx, &dataCopy)
|
||||
}
|
||||
|
||||
func (i *Index) ConnectGameCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[string]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, i.ID}
|
||||
|
||||
_, err := tx.ExecContext(ctx, data.query, args...)
|
||||
return err
|
||||
}
|
||||
|
||||
func (i *Index) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[string]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{i.ID, i.Folder, i.Img, i.Name, i.Type}
|
||||
|
||||
_, err := tx.ExecContext(ctx, data.query, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dataCopy := *data
|
||||
i.ConnectGameQuery(&dataCopy)
|
||||
|
||||
return i.ConnectGameCtx(ctx, tx, &dataCopy)
|
||||
}
|
||||
Reference in New Issue
Block a user