feat(modpacks): implement simple zip importer and API
This commit is contained in:
49
internal/database/modpack_repository.go
Normal file
49
internal/database/modpack_repository.go
Normal file
@@ -0,0 +1,49 @@
|
||||
// File: backend/internal/database/modpack_repository.go
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitea.mrixs.me/minecraft-platform/backend/internal/models"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type ModpackRepository struct {
|
||||
DB *pgxpool.Pool // <--- ИЗМЕНЕНИЕ
|
||||
}
|
||||
|
||||
// CreateModpackTx создает модпак и все его файлы в одной транзакции.
|
||||
func (r *ModpackRepository) CreateModpackTx(ctx context.Context, modpack *models.Modpack, files []models.ModpackFile) error {
|
||||
tx, err := r.DB.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
var modpackID int
|
||||
err = tx.QueryRow(ctx,
|
||||
"INSERT INTO modpacks (name, display_name, minecraft_version) VALUES ($1, $2, $3) RETURNING id",
|
||||
modpack.Name, modpack.DisplayName, modpack.MinecraftVersion,
|
||||
).Scan(&modpackID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rows := make([][]interface{}, len(files))
|
||||
for i, f := range files {
|
||||
rows[i] = []interface{}{modpackID, f.RelativePath, f.FileHash, f.FileSize, f.DownloadURL}
|
||||
}
|
||||
|
||||
_, err = tx.CopyFrom(
|
||||
ctx,
|
||||
pgx.Identifier{"modpack_files"},
|
||||
[]string{"modpack_id", "relative_path", "file_hash", "file_size", "download_url"},
|
||||
pgx.CopyFromRows(rows),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
Reference in New Issue
Block a user