finish world initialization, set up init on startup and on shutdown, move all models to main directory
This commit is contained in:
113
internal/foundry/models/db/files.go
Normal file
113
internal/foundry/models/db/files.go
Normal file
@@ -0,0 +1,113 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
type Files struct {
|
||||
ID uint
|
||||
|
||||
Storages []FilesStorage
|
||||
}
|
||||
|
||||
func (f *Files) Query(data *InsertId[uint]) {
|
||||
data.query = fmt.Sprintf(`
|
||||
INSERT INTO files (%s)
|
||||
VALUES ($1)
|
||||
RETURNING id`, data.fieldName)
|
||||
}
|
||||
|
||||
func (f *Files) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id}
|
||||
|
||||
err := tx.QueryRowx(data.query, args...).Scan(&f.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
group, _ := errgroup.WithContext(context.Background())
|
||||
|
||||
InsertSliceParallel(group, tx, f.Storages, InsertId[uint]{id: f.ID})
|
||||
|
||||
err = group.Wait()
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *Files) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id}
|
||||
|
||||
err := tx.QueryRowxContext(ctx, data.query, args...).Scan(&f.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
group, _ := errgroup.WithContext(context.Background())
|
||||
|
||||
InsertSliceParallel(group, tx, f.Storages, InsertId[uint]{id: f.ID})
|
||||
|
||||
err = group.Wait()
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type FilesStorage struct {
|
||||
ID uint
|
||||
|
||||
Storage string
|
||||
}
|
||||
|
||||
func (f FilesStorage) Query(data *InsertId[uint]) {
|
||||
data.query = `
|
||||
INSERT INTO files_storage (files_id, storage)
|
||||
VALUES ($1, $2)
|
||||
RETURNING id`
|
||||
}
|
||||
|
||||
func (f FilesStorage) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, f.Storage}
|
||||
|
||||
err := tx.QueryRowx(data.query, args...).Scan(&f.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f FilesStorage) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[uint]) error {
|
||||
if data.query == "" {
|
||||
return ErrNoQuery
|
||||
}
|
||||
|
||||
args := []any{data.id, f.Storage}
|
||||
|
||||
err := tx.QueryRowxContext(ctx, data.query, args...).Scan(&f.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user