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

100 lines
1.8 KiB
Go

package db
import (
"context"
"fmt"
"sync"
"github.com/jmoiron/sqlx"
)
type Files struct {
ID uint
Storages []FilesStorage
}
func (f *Files) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
query := fmt.Sprintf(`
INSERT INTO files (%s)
VALUES ($1)
RETURNING id`, data.fieldName)
args := []any{data.id}
err := tx.QueryRowx(query, args...).Scan(&f.ID)
if err != nil {
return err
}
syncDB := SyncDbOperations{
wg: sync.WaitGroup{},
errChan: make(chan error),
}
InsertSliceParallel[uint](&syncDB, tx, f.Storages, &InsertId[uint]{id: f.ID})
return syncDB.Wait()
}
func (f *Files) InsertCtx(ctx context.Context, tx *sqlx.Tx, data *InsertId[uint]) error {
query := fmt.Sprintf(`
INSERT INTO files (%s)
VALUES ($1)
RETURNING id`, data.fieldName)
args := []any{data.id}
err := tx.QueryRowxContext(ctx, query, args...).Scan(&f.ID)
if err != nil {
return err
}
syncDB := SyncDbOperations{
wg: sync.WaitGroup{},
errChan: make(chan error),
}
InsertSliceParallel(&syncDB, tx, f.Storages, &InsertId[uint]{id: f.ID})
return syncDB.Wait()
}
type FilesStorage struct {
ID uint
Storage string
}
func (f FilesStorage) Insert(tx *sqlx.Tx, data *InsertId[uint]) error {
query := `
INSERT INTO files (files_id, storage)
VALUES ($1, $2)
RETURNING id`
args := []any{data.id, f.Storage}
err := tx.QueryRowx(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 {
const query = `
INSERT INTO featured_content (files_id, storage)
VALUES ($1, $2)
RETURNING id`
args := []any{data.id, f.Storage}
err := tx.QueryRowxContext(ctx, query, args...).Scan(&f.ID)
if err != nil {
return err
}
return nil
}