refactored for less code duplication

This commit is contained in:
2026-01-05 09:56:12 +01:00
parent 24f3f4c090
commit 9411d1af33
3 changed files with 273 additions and 338 deletions
+33 -172
View File
@@ -233,13 +233,7 @@ func ExportSkills(outputDir string) error {
return fmt.Errorf("failed to fetch skills: %w", err)
}
// Get all sources for mapping
var sources []models.Source
database.DB.Find(&sources)
sourceMap := make(map[uint]string)
for _, s := range sources {
sourceMap[s.ID] = s.Code
}
sourceMap := buildSourceMap()
// Get all skill category difficulties
var scds []models.SkillCategoryDifficulty
@@ -285,35 +279,9 @@ func ImportSkills(inputDir string) error {
return err
}
// Get all sources for mapping
var sources []models.Source
database.DB.Find(&sources)
sourceMap := make(map[string]uint)
for _, s := range sources {
sourceMap[s.Code] = s.ID
}
// Get all categories for mapping
var categories []models.SkillCategory
database.DB.Find(&categories)
categoryMap := make(map[string]map[string]uint) // game_system -> name -> id
for _, c := range categories {
if categoryMap[c.GameSystem] == nil {
categoryMap[c.GameSystem] = make(map[string]uint)
}
categoryMap[c.GameSystem][c.Name] = c.ID
}
// Get all difficulties for mapping
var difficulties []models.SkillDifficulty
database.DB.Find(&difficulties)
difficultyMap := make(map[string]map[string]uint) // game_system -> name -> id
for _, d := range difficulties {
if difficultyMap[d.GameSystem] == nil {
difficultyMap[d.GameSystem] = make(map[string]uint)
}
difficultyMap[d.GameSystem][d.Name] = d.ID
}
sourceMap := buildSourceMapReverse()
categoryMap := buildCategoryMap()
difficultyMap := buildDifficultyMap()
for _, exp := range exportable {
var skill models.Skill
@@ -426,12 +394,7 @@ func ImportSources(inputDir string) error {
}
for _, exp := range exportable {
var source models.Source
result := database.DB.Where("code = ?", exp.Code).First(&source)
if result.Error == gorm.ErrRecordNotFound {
// Create new source
source = models.Source{
source := models.Source{
Code: exp.Code,
Name: exp.Name,
FullName: exp.FullName,
@@ -443,26 +406,9 @@ func ImportSources(inputDir string) error {
IsActive: exp.IsActive,
GameSystem: exp.GameSystem,
}
if err := database.DB.Create(&source).Error; err != nil {
return fmt.Errorf("failed to create source %s: %w", exp.Code, err)
}
} else if result.Error != nil {
return fmt.Errorf("failed to query source %s: %w", exp.Code, result.Error)
} else {
// Update existing source
source.Name = exp.Name
source.FullName = exp.FullName
source.Edition = exp.Edition
source.Publisher = exp.Publisher
source.PublishYear = exp.PublishYear
source.Description = exp.Description
source.IsCore = exp.IsCore
source.IsActive = exp.IsActive
source.GameSystem = exp.GameSystem
if err := database.DB.Save(&source).Error; err != nil {
return fmt.Errorf("failed to update source %s: %w", exp.Code, err)
}
if err := findOrCreateByCode(exp.Code, &source, "source"); err != nil {
return err
}
}
@@ -600,13 +546,7 @@ func ExportSkillCategories(outputDir string) error {
return fmt.Errorf("failed to fetch skill categories: %w", err)
}
// Get source map
var sources []models.Source
database.DB.Find(&sources)
sourceMap := make(map[uint]string)
for _, s := range sources {
sourceMap[s.ID] = s.Code
}
sourceMap := buildSourceMap()
exportable := make([]ExportableSkillCategory, len(categories))
for i, cat := range categories {
@@ -627,36 +567,17 @@ func ImportSkillCategories(inputDir string) error {
return err
}
// Get source map
var sources []models.Source
database.DB.Find(&sources)
sourceMap := make(map[string]uint)
for _, s := range sources {
sourceMap[s.Code] = s.ID
}
sourceMap := buildSourceMapReverse()
for _, exp := range exportable {
var category models.SkillCategory
result := database.DB.Where("name = ? AND game_system = ?", exp.Name, exp.GameSystem).First(&category)
sourceID := sourceMap[exp.SourceCode]
if result.Error == gorm.ErrRecordNotFound {
category = models.SkillCategory{
category := models.SkillCategory{
Name: exp.Name,
GameSystem: exp.GameSystem,
SourceID: sourceID,
}
if err := database.DB.Create(&category).Error; err != nil {
return fmt.Errorf("failed to create category %s: %w", exp.Name, err)
}
} else if result.Error != nil {
return fmt.Errorf("failed to query category %s: %w", exp.Name, result.Error)
} else {
category.SourceID = sourceID
if err := database.DB.Save(&category).Error; err != nil {
return fmt.Errorf("failed to update category %s: %w", exp.Name, err)
SourceID: sourceMap[exp.SourceCode],
}
if err := findOrCreateByNameAndSystem(exp.Name, exp.GameSystem, &category, "skill category"); err != nil {
return err
}
}
@@ -689,21 +610,14 @@ func ImportSkillDifficulties(inputDir string) error {
}
for _, exp := range exportable {
var difficulty models.SkillDifficulty
result := database.DB.Where("name = ? AND game_system = ?", exp.Name, exp.GameSystem).First(&difficulty)
if result.Error == gorm.ErrRecordNotFound {
difficulty = models.SkillDifficulty{
difficulty := models.SkillDifficulty{
Name: exp.Name,
GameSystem: exp.GameSystem,
}
if err := database.DB.Create(&difficulty).Error; err != nil {
return fmt.Errorf("failed to create difficulty %s: %w", exp.Name, err)
if err := findOrCreateByNameAndSystem(exp.Name, exp.GameSystem, &difficulty, "skill difficulty"); err != nil {
return err
}
} else if result.Error != nil {
return fmt.Errorf("failed to query difficulty %s: %w", exp.Name, result.Error)
}
// No update needed for difficulties (only name and game_system)
}
return nil
@@ -716,13 +630,7 @@ func ExportSpells(outputDir string) error {
return fmt.Errorf("failed to fetch spells: %w", err)
}
// Get source map
var sources []models.Source
database.DB.Find(&sources)
sourceMap := make(map[uint]string)
for _, s := range sources {
sourceMap[s.ID] = s.Code
}
sourceMap := buildSourceMap()
exportable := make([]ExportableSpell, len(spells))
for i, spell := range spells {
@@ -757,13 +665,7 @@ func ImportSpells(inputDir string) error {
return err
}
// Get source map
var sources []models.Source
database.DB.Find(&sources)
sourceMap := make(map[string]uint)
for _, s := range sources {
sourceMap[s.Code] = s.ID
}
sourceMap := buildSourceMapReverse()
for _, exp := range exportable {
var spell models.Spell
@@ -830,13 +732,7 @@ func ExportCharacterClasses(outputDir string) error {
return fmt.Errorf("failed to fetch character classes: %w", err)
}
// Get source map
var sources []models.Source
database.DB.Find(&sources)
sourceMap := make(map[uint]string)
for _, s := range sources {
sourceMap[s.ID] = s.Code
}
sourceMap := buildSourceMap()
exportable := make([]ExportableCharacterClass, len(classes))
for i, class := range classes {
@@ -859,42 +755,19 @@ func ImportCharacterClasses(inputDir string) error {
return err
}
// Get source map
var sources []models.Source
database.DB.Find(&sources)
sourceMap := make(map[string]uint)
for _, s := range sources {
sourceMap[s.Code] = s.ID
}
sourceMap := buildSourceMapReverse()
for _, exp := range exportable {
var class models.CharacterClass
result := database.DB.Where("code = ?", exp.Code).First(&class)
sourceID := sourceMap[exp.SourceCode]
if result.Error == gorm.ErrRecordNotFound {
class = models.CharacterClass{
class := models.CharacterClass{
Code: exp.Code,
Name: exp.Name,
Description: exp.Description,
SourceID: sourceID,
SourceID: sourceMap[exp.SourceCode],
GameSystem: exp.GameSystem,
}
if err := database.DB.Create(&class).Error; err != nil {
return fmt.Errorf("failed to create character class %s: %w", exp.Code, err)
}
} else if result.Error != nil {
return fmt.Errorf("failed to query character class %s: %w", exp.Code, result.Error)
} else {
class.Name = exp.Name
class.Description = exp.Description
class.SourceID = sourceID
class.GameSystem = exp.GameSystem
if err := database.DB.Save(&class).Error; err != nil {
return fmt.Errorf("failed to update character class %s: %w", exp.Code, err)
}
if err := findOrCreateByCode(exp.Code, &class, "character class"); err != nil {
return err
}
}
@@ -908,13 +781,7 @@ func ExportSpellSchools(outputDir string) error {
return fmt.Errorf("failed to fetch spell schools: %w", err)
}
// Get source map
var sources []models.Source
database.DB.Find(&sources)
sourceMap := make(map[uint]string)
for _, s := range sources {
sourceMap[s.ID] = s.Code
}
sourceMap := buildSourceMap()
exportable := make([]ExportableSpellSchool, len(schools))
for i, school := range schools {
@@ -986,6 +853,7 @@ func ExportClassCategoryEPCosts(outputDir string) error {
return fmt.Errorf("failed to fetch class category EP costs: %w", err)
}
// Build reverse maps for export (ID -> Code/Name)
var classes []models.CharacterClass
database.DB.Find(&classes)
classMap := make(map[uint]string)
@@ -1019,13 +887,9 @@ func ImportClassCategoryEPCosts(inputDir string) error {
return err
}
var classes []models.CharacterClass
database.DB.Find(&classes)
classMap := make(map[string]uint)
for _, c := range classes {
classMap[c.Code] = c.ID
}
classMap := buildCharacterClassMap()
// Build category map (name -> id) - need to aggregate by name since we don't have game_system in the exportable
var categories []models.SkillCategory
database.DB.Find(&categories)
categoryMap := make(map[string]uint)
@@ -1073,6 +937,7 @@ func ExportClassSpellSchoolEPCosts(outputDir string) error {
return fmt.Errorf("failed to fetch class spell school EP costs: %w", err)
}
// Build reverse maps for export
var classes []models.CharacterClass
database.DB.Find(&classes)
classMap := make(map[uint]string)
@@ -1106,13 +971,9 @@ func ImportClassSpellSchoolEPCosts(inputDir string) error {
return err
}
var classes []models.CharacterClass
database.DB.Find(&classes)
classMap := make(map[string]uint)
for _, c := range classes {
classMap[c.Code] = c.ID
}
classMap := buildCharacterClassMap()
// Build spell school map (name -> id)
var schools []models.SpellSchool
database.DB.Find(&schools)
schoolMap := make(map[string]uint)
+51 -144
View File
@@ -12,59 +12,32 @@ import (
// ExportSkillImprovementCosts exports all skill improvement costs to a JSON file
func ExportSkillImprovementCosts(outputDir string) error {
var costs []models.SkillImprovementCost
if err := database.DB.Find(&costs).Error; err != nil {
if err := database.DB.Preload("SkillCategoryDifficulty.Skill").
Preload("SkillCategoryDifficulty.SkillCategory").
Preload("SkillCategoryDifficulty.SkillDifficulty").
Find(&costs).Error; err != nil {
return fmt.Errorf("failed to fetch skill improvement costs: %w", err)
}
// Build maps for skill category difficulties
var scds []models.SkillCategoryDifficulty
database.DB.Find(&scds)
scdMap := make(map[uint]models.SkillCategoryDifficulty)
for _, scd := range scds {
scdMap[scd.ID] = scd
exportable := make([]ExportableSkillImprovementCost, 0, len(costs))
for _, cost := range costs {
// Skip records with incomplete relationships
if cost.SkillCategoryDifficulty.Skill.Name == "" ||
cost.SkillCategoryDifficulty.SkillCategory.Name == "" ||
cost.SkillCategoryDifficulty.SkillDifficulty.Name == "" {
continue
}
// Get skills
var skills []models.Skill
database.DB.Find(&skills)
skillMap := make(map[uint]models.Skill)
for _, s := range skills {
skillMap[s.ID] = s
}
// Get categories
var categories []models.SkillCategory
database.DB.Find(&categories)
categoryMap := make(map[uint]models.SkillCategory)
for _, c := range categories {
categoryMap[c.ID] = c
}
// Get difficulties
var difficulties []models.SkillDifficulty
database.DB.Find(&difficulties)
difficultyMap := make(map[uint]models.SkillDifficulty)
for _, d := range difficulties {
difficultyMap[d.ID] = d
}
exportable := make([]ExportableSkillImprovementCost, len(costs))
for i, cost := range costs {
scd := scdMap[cost.SkillCategoryDifficultyID]
skill := skillMap[scd.SkillID]
category := categoryMap[scd.SkillCategoryID]
difficulty := difficultyMap[scd.SkillDifficultyID]
exportable[i] = ExportableSkillImprovementCost{
SkillName: skill.Name,
SkillSystem: skill.GameSystem,
CategoryName: category.Name,
CategorySystem: category.GameSystem,
DifficultyName: difficulty.Name,
DifficultySystem: difficulty.GameSystem,
exportable = append(exportable, ExportableSkillImprovementCost{
SkillName: cost.SkillCategoryDifficulty.Skill.Name,
SkillSystem: cost.SkillCategoryDifficulty.Skill.GameSystem,
CategoryName: cost.SkillCategoryDifficulty.SkillCategory.Name,
CategorySystem: cost.SkillCategoryDifficulty.SkillCategory.GameSystem,
DifficultyName: cost.SkillCategoryDifficulty.SkillDifficulty.Name,
DifficultySystem: cost.SkillCategoryDifficulty.SkillDifficulty.GameSystem,
CurrentLevel: cost.CurrentLevel,
TERequired: cost.TERequired,
}
})
}
return writeJSON(filepath.Join(outputDir, "skill_improvement_costs.json"), exportable)
@@ -77,30 +50,36 @@ func ImportSkillImprovementCosts(inputDir string) error {
return err
}
// Build lookup maps using helpers
skillMap := buildSkillMap()
categoryMap := buildCategoryMap()
difficultyMap := buildDifficultyMap()
for _, exp := range exportable {
// Find skill
var skill models.Skill
if err := database.DB.Where("name = ? AND game_system = ?", exp.SkillName, exp.SkillSystem).First(&skill).Error; err != nil {
return fmt.Errorf("skill not found: %s: %w", exp.SkillName, err)
// Find skill ID
skillID, ok := skillMap[exp.SkillSystem][exp.SkillName]
if !ok {
return fmt.Errorf("skill not found: %s (%s)", exp.SkillName, exp.SkillSystem)
}
// Find category
var category models.SkillCategory
if err := database.DB.Where("name = ? AND game_system = ?", exp.CategoryName, exp.CategorySystem).First(&category).Error; err != nil {
return fmt.Errorf("category not found: %s: %w", exp.CategoryName, err)
// Find category ID
categoryID, ok := categoryMap[exp.CategorySystem][exp.CategoryName]
if !ok {
return fmt.Errorf("category not found: %s (%s)", exp.CategoryName, exp.CategorySystem)
}
// Find difficulty
var difficulty models.SkillDifficulty
if err := database.DB.Where("name = ? AND game_system = ?", exp.DifficultyName, exp.DifficultySystem).First(&difficulty).Error; err != nil {
return fmt.Errorf("difficulty not found: %s: %w", exp.DifficultyName, err)
// Find difficulty ID
difficultyID, ok := difficultyMap[exp.DifficultySystem][exp.DifficultyName]
if !ok {
return fmt.Errorf("difficulty not found: %s (%s)", exp.DifficultyName, exp.DifficultySystem)
}
// Find SkillCategoryDifficulty
var scd models.SkillCategoryDifficulty
if err := database.DB.Where("skill_id = ? AND skill_category_id = ? AND skill_difficulty_id = ?",
skill.ID, category.ID, difficulty.ID).First(&scd).Error; err != nil {
return fmt.Errorf("skill category difficulty not found: %w", err)
skillID, categoryID, difficultyID).First(&scd).Error; err != nil {
return fmt.Errorf("skill category difficulty not found for %s/%s/%s: %w",
exp.SkillName, exp.CategoryName, exp.DifficultyName, err)
}
// Find or create SkillImprovementCost
@@ -137,13 +116,7 @@ func ExportWeaponSkills(outputDir string) error {
return fmt.Errorf("failed to fetch weapon skills: %w", err)
}
// Get source map
var sources []models.Source
database.DB.Find(&sources)
sourceMap := make(map[uint]string)
for _, s := range sources {
sourceMap[s.ID] = s.Code
}
sourceMap := buildSourceMap()
exportable := make([]ExportableWeaponSkill, len(skills))
for i, skill := range skills {
@@ -173,13 +146,7 @@ func ImportWeaponSkills(inputDir string) error {
return err
}
// Get source map
var sources []models.Source
database.DB.Find(&sources)
sourceMap := make(map[string]uint)
for _, s := range sources {
sourceMap[s.Code] = s.ID
}
sourceMap := buildSourceMapReverse()
for _, exp := range exportable {
var skill models.WeaponSkill
@@ -237,13 +204,7 @@ func ExportEquipment(outputDir string) error {
return fmt.Errorf("failed to fetch equipment: %w", err)
}
// Get source map
var sources []models.Source
database.DB.Find(&sources)
sourceMap := make(map[uint]string)
for _, s := range sources {
sourceMap[s.ID] = s.Code
}
sourceMap := buildSourceMap()
exportable := make([]ExportableEquipment, len(equipment))
for i, eq := range equipment {
@@ -269,13 +230,7 @@ func ImportEquipment(inputDir string) error {
return err
}
// Get source map
var sources []models.Source
database.DB.Find(&sources)
sourceMap := make(map[string]uint)
for _, s := range sources {
sourceMap[s.Code] = s.ID
}
sourceMap := buildSourceMapReverse()
for _, exp := range exportable {
var eq models.Equipment
@@ -323,13 +278,7 @@ func ExportWeapons(outputDir string) error {
return fmt.Errorf("failed to fetch weapons: %w", err)
}
// Get source map
var sources []models.Source
database.DB.Find(&sources)
sourceMap := make(map[uint]string)
for _, s := range sources {
sourceMap[s.ID] = s.Code
}
sourceMap := buildSourceMap()
exportable := make([]ExportableWeapon, len(weapons))
for i, weapon := range weapons {
@@ -360,13 +309,7 @@ func ImportWeapons(inputDir string) error {
return err
}
// Get source map
var sources []models.Source
database.DB.Find(&sources)
sourceMap := make(map[string]uint)
for _, s := range sources {
sourceMap[s.Code] = s.ID
}
sourceMap := buildSourceMapReverse()
for _, exp := range exportable {
var weapon models.Weapon
@@ -426,13 +369,7 @@ func ExportContainers(outputDir string) error {
return fmt.Errorf("failed to fetch containers: %w", err)
}
// Get source map
var sources []models.Source
database.DB.Find(&sources)
sourceMap := make(map[uint]string)
for _, s := range sources {
sourceMap[s.ID] = s.Code
}
sourceMap := buildSourceMap()
exportable := make([]ExportableContainer, len(containers))
for i, container := range containers {
@@ -460,13 +397,7 @@ func ImportContainers(inputDir string) error {
return err
}
// Get source map
var sources []models.Source
database.DB.Find(&sources)
sourceMap := make(map[string]uint)
for _, s := range sources {
sourceMap[s.Code] = s.ID
}
sourceMap := buildSourceMapReverse()
for _, exp := range exportable {
var container models.Container
@@ -520,13 +451,7 @@ func ExportTransportation(outputDir string) error {
return fmt.Errorf("failed to fetch transportation: %w", err)
}
// Get source map
var sources []models.Source
database.DB.Find(&sources)
sourceMap := make(map[uint]string)
for _, s := range sources {
sourceMap[s.ID] = s.Code
}
sourceMap := buildSourceMap()
exportable := make([]ExportableTransportation, len(transportation))
for i, trans := range transportation {
@@ -554,13 +479,7 @@ func ImportTransportation(inputDir string) error {
return err
}
// Get source map
var sources []models.Source
database.DB.Find(&sources)
sourceMap := make(map[string]uint)
for _, s := range sources {
sourceMap[s.Code] = s.ID
}
sourceMap := buildSourceMapReverse()
for _, exp := range exportable {
var trans models.Transportation
@@ -616,13 +535,7 @@ func ExportBelieves(outputDir string) error {
return fmt.Errorf("failed to fetch believes: %w", err)
}
// Get source map
var sources []models.Source
database.DB.Find(&sources)
sourceMap := make(map[uint]string)
for _, s := range sources {
sourceMap[s.ID] = s.Code
}
sourceMap := buildSourceMap()
exportable := make([]ExportableBelieve, len(believes))
for i, believe := range believes {
@@ -645,13 +558,7 @@ func ImportBelieves(inputDir string) error {
return err
}
// Get source map
var sources []models.Source
database.DB.Find(&sources)
sourceMap := make(map[string]uint)
for _, s := range sources {
sourceMap[s.Code] = s.ID
}
sourceMap := buildSourceMapReverse()
for _, exp := range exportable {
var believe models.Believe
+167
View File
@@ -0,0 +1,167 @@
package gsmaster
import (
"bamort/database"
"bamort/models"
"fmt"
"gorm.io/gorm"
)
// LookupMap builders - reusable functions to build ID<->Code/Name maps
// buildSourceMap creates a map from source ID to source code
func buildSourceMap() map[uint]string {
var sources []models.Source
database.DB.Find(&sources)
sourceMap := make(map[uint]string)
for _, s := range sources {
sourceMap[s.ID] = s.Code
}
return sourceMap
}
// buildSourceMapReverse creates a map from source code to source ID
func buildSourceMapReverse() map[string]uint {
var sources []models.Source
database.DB.Find(&sources)
sourceMap := make(map[string]uint)
for _, s := range sources {
sourceMap[s.Code] = s.ID
}
return sourceMap
}
// buildCategoryMap creates a nested map: game_system -> name -> id
func buildCategoryMap() map[string]map[string]uint {
var categories []models.SkillCategory
database.DB.Find(&categories)
categoryMap := make(map[string]map[string]uint)
for _, c := range categories {
if categoryMap[c.GameSystem] == nil {
categoryMap[c.GameSystem] = make(map[string]uint)
}
categoryMap[c.GameSystem][c.Name] = c.ID
}
return categoryMap
}
// buildDifficultyMap creates a nested map: game_system -> name -> id
func buildDifficultyMap() map[string]map[string]uint {
var difficulties []models.SkillDifficulty
database.DB.Find(&difficulties)
difficultyMap := make(map[string]map[string]uint)
for _, d := range difficulties {
if difficultyMap[d.GameSystem] == nil {
difficultyMap[d.GameSystem] = make(map[string]uint)
}
difficultyMap[d.GameSystem][d.Name] = d.ID
}
return difficultyMap
}
// buildCharacterClassMap creates a map from character class code to ID
func buildCharacterClassMap() map[string]uint {
var classes []models.CharacterClass
database.DB.Find(&classes)
classMap := make(map[string]uint)
for _, c := range classes {
classMap[c.Code] = c.ID
}
return classMap
}
// buildSpellSchoolMap creates a nested map: game_system -> name -> id
func buildSpellSchoolMap() map[string]map[string]uint {
var schools []models.SpellSchool
database.DB.Find(&schools)
schoolMap := make(map[string]map[string]uint)
for _, s := range schools {
if schoolMap[s.GameSystem] == nil {
schoolMap[s.GameSystem] = make(map[string]uint)
}
schoolMap[s.GameSystem][s.Name] = s.ID
}
return schoolMap
}
// buildSkillMap creates a nested map: game_system -> name -> id
func buildSkillMap() map[string]map[string]uint {
var skills []models.Skill
database.DB.Find(&skills)
skillMap := make(map[string]map[string]uint)
for _, s := range skills {
if skillMap[s.GameSystem] == nil {
skillMap[s.GameSystem] = make(map[string]uint)
}
skillMap[s.GameSystem][s.Name] = s.ID
}
return skillMap
}
// buildWeaponSkillMap creates a nested map: game_system -> name -> id
func buildWeaponSkillMap() map[string]map[string]uint {
var weaponSkills []models.WeaponSkill
database.DB.Find(&weaponSkills)
weaponSkillMap := make(map[string]map[string]uint)
for _, ws := range weaponSkills {
if weaponSkillMap[ws.GameSystem] == nil {
weaponSkillMap[ws.GameSystem] = make(map[string]uint)
}
weaponSkillMap[ws.GameSystem][ws.Name] = ws.ID
}
return weaponSkillMap
}
// Generic import helper for entities with name + game_system natural key
type ImportConfig struct {
EntityName string // For error messages, e.g., "skill category"
}
// findOrCreateByNameAndSystem is a helper for import operations
// It looks up an entity by name and game_system, creates if not found
func findOrCreateByNameAndSystem(
name string,
gameSystem string,
model interface{},
entityName string,
) error {
result := database.DB.Where("name = ? AND game_system = ?", name, gameSystem).First(model)
if result.Error == gorm.ErrRecordNotFound {
if err := database.DB.Create(model).Error; err != nil {
return fmt.Errorf("failed to create %s %s: %w", entityName, name, err)
}
} else if result.Error != nil {
return fmt.Errorf("failed to query %s %s: %w", entityName, name, result.Error)
} else {
if err := database.DB.Save(model).Error; err != nil {
return fmt.Errorf("failed to update %s %s: %w", entityName, name, err)
}
}
return nil
}
// findOrCreateByCode is a helper for import operations with code as natural key
func findOrCreateByCode(
code string,
model interface{},
entityName string,
) error {
result := database.DB.Where("code = ?", code).First(model)
if result.Error == gorm.ErrRecordNotFound {
if err := database.DB.Create(model).Error; err != nil {
return fmt.Errorf("failed to create %s %s: %w", entityName, code, err)
}
} else if result.Error != nil {
return fmt.Errorf("failed to query %s %s: %w", entityName, code, result.Error)
} else {
if err := database.DB.Save(model).Error; err != nil {
return fmt.Errorf("failed to update %s %s: %w", entityName, code, err)
}
}
return nil
}