174 lines
3.6 KiB
Go
174 lines
3.6 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
)
|
|
|
|
type Setup struct {
|
|
ID uint
|
|
CreatedAt time.Time
|
|
|
|
IsAdmin bool
|
|
IsSetup bool
|
|
CoreUpdate CoreUpdate
|
|
FeaturedContent FeaturedContent
|
|
Files Files
|
|
Options *SetupOptions
|
|
Release Release
|
|
Languages []*SetupLanguage
|
|
Modules []*Module
|
|
News []*News
|
|
PackageWarnings []*PackageWarning
|
|
Systems []*System
|
|
Worlds []*World
|
|
}
|
|
|
|
func (s *Setup) InsertObjects(tx *sqlx.Tx) error {
|
|
relData := &InsertId[uint]{id: s.ID, fieldName: "setup_id"}
|
|
syncDB := NewSyncDB()
|
|
defer close(syncDB.errChan)
|
|
|
|
InsertWithCtxParallel(syncDB, tx, &s.CoreUpdate, relData)
|
|
InsertWithCtxParallel(syncDB, tx, &s.FeaturedContent, relData)
|
|
InsertWithCtxParallel(syncDB, tx, &s.Files, relData)
|
|
InsertWithCtxParallel(syncDB, tx, s.Options, relData)
|
|
InsertWithCtxParallel(syncDB, tx, &s.Release, relData)
|
|
|
|
InsertSliceParallel(syncDB, tx, s.Languages, relData)
|
|
InsertSliceParallel(syncDB, tx, s.Modules, relData)
|
|
InsertSliceParallel(syncDB, tx, s.News, relData)
|
|
InsertSliceParallel(syncDB, tx, s.PackageWarnings, relData)
|
|
|
|
relDataString := &InsertId[string]{
|
|
id: strconv.FormatUint(uint64(s.ID), 10),
|
|
fieldName: "setup_id",
|
|
}
|
|
InsertSliceParallel(syncDB, tx, s.Systems, relDataString)
|
|
InsertSliceParallel(syncDB, tx, s.Worlds, relDataString)
|
|
|
|
return syncDB.Wait()
|
|
}
|
|
|
|
func (s *Setup) Insert(db *sqlx.DB) error {
|
|
tx := db.MustBegin()
|
|
defer tx.Rollback()
|
|
|
|
const query = `
|
|
INSERT INTO setup (is_admin, is_setup)
|
|
VALUES ($1, $2)
|
|
RETURNING id, created_at`
|
|
|
|
args := []any{s.IsAdmin, s.IsSetup}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
defer cancel()
|
|
|
|
err := tx.QueryRowContext(ctx, query, args...).Scan(&s.ID, &s.CreatedAt)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = s.InsertObjects(tx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return tx.Commit()
|
|
}
|
|
|
|
type FeaturedContent struct {
|
|
ID uint
|
|
|
|
Title string
|
|
Caption string
|
|
URL string
|
|
Image string
|
|
}
|
|
|
|
func (f *FeaturedContent) Query(data *InsertId[uint]) {
|
|
data.query = `
|
|
INSERT INTO featured_content (setup_id, title, caption, url, image)
|
|
VALUES ($1, $2, $3, $4, $5)
|
|
RETURNING id`
|
|
}
|
|
|
|
func (f *FeaturedContent) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
|
|
if data.query == "" {
|
|
return ErrNoQuery
|
|
}
|
|
|
|
args := []any{data.id, f.Title, f.Caption, f.URL, f.Image}
|
|
|
|
err := tx.QueryRowx(data.query, args...).Scan(&f.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (f *FeaturedContent) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[uint]) error {
|
|
if data.query == "" {
|
|
return ErrNoQuery
|
|
}
|
|
|
|
args := []any{data.id, f.Title, f.Caption, f.URL, f.Image}
|
|
|
|
err := tx.QueryRowxContext(ctx, data.query, args...).Scan(&f.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type News struct {
|
|
ID uint
|
|
|
|
Title string
|
|
Caption string
|
|
URL string
|
|
Image string
|
|
}
|
|
|
|
func (n *News) Query(data *InsertId[uint]) {
|
|
data.query = `
|
|
INSERT INTO news (setup_id, title, caption, url, image)
|
|
VALUES ($1, $2, $3, $4, $5)
|
|
RETURNING id`
|
|
}
|
|
|
|
func (n *News) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
|
|
if data.query == "" {
|
|
return ErrNoQuery
|
|
}
|
|
|
|
args := []any{data.id, n.Title, n.Caption, n.URL, n.Image}
|
|
|
|
err := tx.QueryRow(data.query, args...).Scan(&n.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (n *News) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[uint]) error {
|
|
if data.query == "" {
|
|
return ErrNoQuery
|
|
}
|
|
|
|
args := []any{data.id, n.Title, n.Caption, n.URL, n.Image}
|
|
|
|
err := tx.QueryRowContext(ctx, data.query, args...).Scan(&n.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|