reordered methods create -> find

made methods identical by renaming properties
added method to save object to DB
added method to find first by ID
This commit is contained in:
2025-01-01 10:30:34 +01:00
parent 80f35c5f98
commit 6c36974242
+183 -29
View File
@@ -57,22 +57,12 @@ type Transportation struct {
Container
}
func (stamm *LookupList) First(name string) error {
func (object *LookupList) Create() error {
gameSystem := "midgard"
err := database.DB.First(&stamm, "system=? AND name = ?", gameSystem, name).Error
if err != nil {
// zauber found
return err
}
return nil
}
func (stamm *LookupList) Create() error {
gameSystem := "midgard"
stamm.System = gameSystem
object.System = gameSystem
err := database.DB.Transaction(func(tx *gorm.DB) error {
// Save the main character record
if err := tx.Create(&stamm).Error; err != nil {
if err := tx.Create(&object).Error; err != nil {
return fmt.Errorf("failed to save Lookup: %w", err)
}
return nil
@@ -81,11 +71,30 @@ func (stamm *LookupList) Create() error {
return err
}
func (stamm *Skill) First(name string) error {
func (object *LookupList) First(value string) error {
gameSystem := "midgard"
err := database.DB.First(&stamm, "system=? AND name = ?", gameSystem, name).Error
err := database.DB.First(&object, "system=? AND name = ?", gameSystem, value).Error
if err != nil {
// Fertigkeit found
// zauber found
return err
}
return nil
}
func (object *LookupList) FirstId(value uint) error {
gameSystem := "midgard"
err := database.DB.First(&object, "system=? AND id = ?", gameSystem, value).Error
if err != nil {
// zauber found
return err
}
return nil
}
func (object *LookupList) Save() error {
err := database.DB.Save(&object).Error
if err != nil {
// zauber found
return err
}
return nil
@@ -105,7 +114,7 @@ func (stamm *Skill) Create() error {
return err
}
func (stamm *WaeponSkill) First(name string) error {
func (stamm *Skill) First(name string) error {
gameSystem := "midgard"
err := database.DB.First(&stamm, "system=? AND name = ?", gameSystem, name).Error
if err != nil {
@@ -115,6 +124,25 @@ func (stamm *WaeponSkill) First(name string) error {
return nil
}
func (object *Skill) FirstId(value uint) error {
gameSystem := "midgard"
err := database.DB.First(&object, "system=? AND id = ?", gameSystem, value).Error
if err != nil {
// zauber found
return err
}
return nil
}
func (object *Skill) Save() error {
err := database.DB.Save(&object).Error
if err != nil {
// zauber found
return err
}
return nil
}
func (stamm *WaeponSkill) Create() error {
gameSystem := "midgard"
stamm.System = gameSystem
@@ -128,9 +156,29 @@ func (stamm *WaeponSkill) Create() error {
return err
}
func (stamm *Spell) First(name string) error {
func (stamm *WaeponSkill) First(name string) error {
gameSystem := "midgard"
err := database.DB.First(&stamm, "system=? AND name = ?", gameSystem, name).Error
if err != nil {
// Fertigkeit found
return err
}
return nil
}
func (object *WaeponSkill) FirstId(value uint) error {
gameSystem := "midgard"
err := database.DB.First(&object, "system=? AND id = ?", gameSystem, value).Error
if err != nil {
// zauber found
return err
}
return nil
}
func (object *WaeponSkill) Save() error {
err := database.DB.Save(&object).Error
if err != nil {
// zauber found
return err
@@ -152,6 +200,49 @@ func (stamm *Spell) Create() error {
return err
}
func (stamm *Spell) First(name string) error {
gameSystem := "midgard"
err := database.DB.First(&stamm, "system=? AND name = ?", gameSystem, name).Error
if err != nil {
// zauber found
return err
}
return nil
}
func (object *Spell) FirstId(value uint) error {
gameSystem := "midgard"
err := database.DB.First(&object, "system=? AND id = ?", gameSystem, value).Error
if err != nil {
// zauber found
return err
}
return nil
}
func (object *Spell) Save() error {
err := database.DB.Save(&object).Error
if err != nil {
// zauber found
return err
}
return nil
}
func (stamm *Equipment) Create() error {
gameSystem := "midgard"
stamm.System = gameSystem
err := database.DB.Transaction(func(tx *gorm.DB) error {
// Save the main character record
if err := tx.Create(&stamm).Error; err != nil {
return fmt.Errorf("failed to save LookupEquipment: %w", err)
}
return nil
})
return err
}
func (stamm *Equipment) First(name string) error {
gameSystem := "midgard"
err := database.DB.First(&stamm, "system=? AND name = ?", gameSystem, name).Error
@@ -162,7 +253,26 @@ func (stamm *Equipment) First(name string) error {
return nil
}
func (stamm *Equipment) Create() error {
func (object *Equipment) FirstId(value uint) error {
gameSystem := "midgard"
err := database.DB.First(&object, "system=? AND id = ?", gameSystem, value).Error
if err != nil {
// zauber found
return err
}
return nil
}
func (object *Equipment) Save() error {
err := database.DB.Save(&object).Error
if err != nil {
// zauber found
return err
}
return nil
}
func (stamm *Waepon) Create() error {
gameSystem := "midgard"
stamm.System = gameSystem
err := database.DB.Transaction(func(tx *gorm.DB) error {
@@ -186,18 +296,23 @@ func (stamm *Waepon) First(name string) error {
return nil
}
func (stamm *Waepon) Create() error {
func (object *Waepon) FirstId(value uint) error {
gameSystem := "midgard"
stamm.System = gameSystem
err := database.DB.Transaction(func(tx *gorm.DB) error {
// Save the main character record
if err := tx.Create(&stamm).Error; err != nil {
return fmt.Errorf("failed to save LookupEquipment: %w", err)
}
return nil
})
err := database.DB.First(&object, "system=? AND id = ?", gameSystem, value).Error
if err != nil {
// zauber found
return err
}
return nil
}
return err
func (object *Waepon) Save() error {
err := database.DB.Save(&object).Error
if err != nil {
// zauber found
return err
}
return nil
}
func (stamm *Container) Create() error {
@@ -213,6 +328,7 @@ func (stamm *Container) Create() error {
return err
}
func (stamm *Container) First(name string) error {
gameSystem := "midgard"
err := database.DB.First(&stamm, "system=? AND name = ?", gameSystem, name).Error
@@ -223,6 +339,25 @@ func (stamm *Container) First(name string) error {
return nil
}
func (object *Container) FirstId(value uint) error {
gameSystem := "midgard"
err := database.DB.First(&object, "system=? AND id = ?", gameSystem, value).Error
if err != nil {
// zauber found
return err
}
return nil
}
func (object *Container) Save() error {
err := database.DB.Save(&object).Error
if err != nil {
// zauber found
return err
}
return nil
}
func (stamm *Transportation) Create() error {
gameSystem := "midgard"
stamm.System = gameSystem
@@ -237,6 +372,16 @@ func (stamm *Transportation) Create() error {
return err
}
func (object *Transportation) FirstId(value uint) error {
gameSystem := "midgard"
err := database.DB.First(&object, "system=? AND id = ?", gameSystem, value).Error
if err != nil {
// zauber found
return err
}
return nil
}
func (stamm *Transportation) First(name string) error {
gameSystem := "midgard"
err := database.DB.First(&stamm, "system=? AND name = ?", gameSystem, name).Error
@@ -246,3 +391,12 @@ func (stamm *Transportation) First(name string) error {
}
return nil
}
func (object *Transportation) Save() error {
err := database.DB.Save(&object).Error
if err != nil {
// zauber found
return err
}
return nil
}