Files
Foundry-Scrapping-API/internal/foundry/temp_models/db/setup.go
2026-04-16 18:30:00 +03:00

153 lines
3.2 KiB
Go

package db
import (
"context"
"sync"
"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) 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
}
id := InsertId[uint]{id: s.ID, fieldName: "setup_id"}
dataSync := SyncDbOperations{
wg: sync.WaitGroup{},
errChan: make(chan error),
}
defer close(dataSync.errChan)
go InsertSliceParallel(&dataSync, tx, s.News, &id)
go InsertSliceParallel(&dataSync, tx, s.Languages, &id)
InsertWithCtx(tx, &s.FeaturedContent, &id)
InsertWithCtx(tx, &s.Files, &id)
InsertWithCtx(tx, &s.Release, &id)
InsertWithCtx(tx, &s.CoreUpdate, &id)
err = dataSync.Wait()
if err != nil {
return err
}
return tx.Commit()
}
type FeaturedContent struct {
ID uint
Title string
Caption string
URL string
Image string
}
func (f *FeaturedContent) Insert(tx *sqlx.Tx, setup *InsertId[uint]) error {
const query = `
INSERT INTO featured_content (setup_id, title, caption, url, image)
VALUES ($1, $2, $3, $4, $5)
RETURNING id`
args := []any{setup.id, f.Title, f.Caption, f.URL, f.Image}
err := tx.QueryRowx(query, args...).Scan(&f.ID)
if err != nil {
return err
}
return nil
}
func (f *FeaturedContent) InsertCtx(ctx context.Context, tx *sqlx.Tx, setup *InsertId[uint]) error {
const query = `
INSERT INTO featured_content (setup_id, title, caption, url, image)
VALUES ($1, $2, $3, $4, $5)
RETURNING id`
args := []any{setup.id, f.Title, f.Caption, f.URL, f.Image}
err := tx.QueryRowxContext(ctx, 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) Insert(tx *sqlx.Tx, setup *InsertId[uint]) error {
const insertNews = `
INSERT INTO news (setup_id, title, caption, url, image)
VALUES ($1, $2, $3, $4, $5)
RETURNING id`
args := []any{setup.id, n.Title, n.Caption, n.URL, n.Image}
err := tx.QueryRow(insertNews, args...).Scan(&n.ID)
if err != nil {
return err
}
return nil
}
func (n News) InsertCtx(ctx context.Context, tx *sqlx.Tx, setup *InsertId[uint]) error {
const insertNews = `
INSERT INTO news (setup_id, title, caption, url, image)
VALUES ($1, $2, $3, $4, $5)
RETURNING id`
args := []any{setup.id, n.Title, n.Caption, n.URL, n.Image}
err := tx.QueryRowContext(ctx, insertNews, args...).Scan(&n.ID)
if err != nil {
return err
}
return nil
}