save Spezialisierung as StringArray
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
@@ -158,3 +162,26 @@ func saveCharacterToDB(character *Character) error {
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// StringArray is a custom type for []string
|
||||
type StringArray []string
|
||||
|
||||
// Value implements the driver.Valuer interface for database storage
|
||||
func (s StringArray) Value() (driver.Value, error) {
|
||||
return json.Marshal(s) // Serialize []string to JSON
|
||||
}
|
||||
|
||||
// Scan implements the sql.Scanner interface for database retrieval
|
||||
func (s *StringArray) Scan(value interface{}) error {
|
||||
if value == nil {
|
||||
*s = []string{}
|
||||
return nil
|
||||
}
|
||||
|
||||
bytes, ok := value.([]byte)
|
||||
if !ok {
|
||||
return errors.New("failed to convert database value to []byte")
|
||||
}
|
||||
|
||||
return json.Unmarshal(bytes, s) // Deserialize JSON to []string
|
||||
}
|
||||
|
||||
+31
-27
@@ -8,34 +8,34 @@ type User struct {
|
||||
}
|
||||
|
||||
type Character struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
Name string `json:"name"`
|
||||
Rasse string `json:"rasse"`
|
||||
Typ string `json:"typ"`
|
||||
Alter int `json:"alter"`
|
||||
Anrede string `json:"anrede"`
|
||||
Grad int `json:"grad"`
|
||||
Groesse int `json:"groesse"`
|
||||
Gewicht int `json:"gewicht"`
|
||||
Glaube string `json:"glaube"`
|
||||
Hand string `json:"hand"`
|
||||
Fertigkeiten []Fertigkeit `gorm:"foreignKey:CharacterID" json:"fertigkeiten"`
|
||||
Zauber []Zauber `gorm:"foreignKey:CharacterID" json:"zauber"`
|
||||
Lp Lp `gorm:"foreignKey:CharacterID" json:"lp"`
|
||||
Eigenschaften []Eigenschaft `gorm:"foreignKey:CharacterID" json:"eigenschaften"`
|
||||
Merkmale Merkmale `gorm:"foreignKey:CharacterID" json:"merkmale"`
|
||||
Bennies Bennies `gorm:"foreignKey:CharacterID" json:"bennies"`
|
||||
Gestalt Gestalt `gorm:"foreignKey:CharacterID" json:"gestalt"`
|
||||
Ap Ap `gorm:"foreignKey:CharacterID" json:"ap"`
|
||||
B B `gorm:"foreignKey:CharacterID" json:"b"`
|
||||
Erfahrungsschatz Erfahrungsschatz `gorm:"foreignKey:CharacterID" json:"erfahrungsschatz"`
|
||||
Transportmittel []Transportation `gorm:"foreignKey:CharacterID" json:"transportmittel"`
|
||||
Ausruestung []Ausruestung `gorm:"foreignKey:CharacterID" json:"ausruestung"`
|
||||
Behaeltnisse []Behaeltniss `gorm:"foreignKey:CharacterID" json:"behaeltnisse"`
|
||||
Waffen []Waffe `gorm:"foreignKey:CharacterID" json:"waffen"`
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
Name string `json:"name"`
|
||||
Rasse string `json:"rasse"`
|
||||
Typ string `json:"typ"`
|
||||
Alter int `json:"alter"`
|
||||
Anrede string `json:"anrede"`
|
||||
Grad int `json:"grad"`
|
||||
Groesse int `json:"groesse"`
|
||||
Gewicht int `json:"gewicht"`
|
||||
Glaube string `json:"glaube"`
|
||||
Hand string `json:"hand"`
|
||||
Fertigkeiten []Fertigkeit `gorm:"foreignKey:CharacterID" json:"fertigkeiten"`
|
||||
Zauber []Zauber `gorm:"foreignKey:CharacterID" json:"zauber"`
|
||||
Lp Lp `gorm:"foreignKey:CharacterID" json:"lp"`
|
||||
Eigenschaften []Eigenschaft `gorm:"foreignKey:CharacterID" json:"eigenschaften"`
|
||||
Merkmale Merkmale `gorm:"foreignKey:CharacterID" json:"merkmale"`
|
||||
Bennies Bennies `gorm:"foreignKey:CharacterID" json:"bennies"`
|
||||
Gestalt Gestalt `gorm:"foreignKey:CharacterID" json:"gestalt"`
|
||||
Ap Ap `gorm:"foreignKey:CharacterID" json:"ap"`
|
||||
B B `gorm:"foreignKey:CharacterID" json:"b"`
|
||||
Erfahrungsschatz Erfahrungsschatz `gorm:"foreignKey:CharacterID" json:"erfahrungsschatz"`
|
||||
Transportmittel []Transportation `gorm:"foreignKey:CharacterID" json:"transportmittel"`
|
||||
Ausruestung []Ausruestung `gorm:"foreignKey:CharacterID" json:"ausruestung"`
|
||||
Behaeltnisse []Behaeltniss `gorm:"foreignKey:CharacterID" json:"behaeltnisse"`
|
||||
Waffen []Waffe `gorm:"foreignKey:CharacterID" json:"waffen"`
|
||||
Waffenfertigkeiten []Waffenfertigkeit `gorm:"foreignKey:CharacterID" json:"waffenfertigkeiten"`
|
||||
Spezialisierung StringArray `gorm:"type:TEXT" json:"spezialisierung"`
|
||||
/*
|
||||
Waffenfertigkeiten []Waffenfertigkeit `gorm:"foreignKey:CharacterID" json:"waffenfertigkeiten"`
|
||||
Spezialisierung []string `json:"spezialisierung"`
|
||||
Image string `json:"image,omitempty"`
|
||||
*/
|
||||
}
|
||||
@@ -198,6 +198,7 @@ type Transportation struct {
|
||||
Magisch MagischTransport `gorm:"foreignKey:TransportationID" json:"magisch"`
|
||||
//Magisch Magisch `gorm:"polymorphic:Item;polymorphicValue:Transportmittel" json:"magisch"`
|
||||
}
|
||||
|
||||
type MagischAusruestung struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
//ItemType string `gorm:"index" json:"item_type"` // Type of the referenced item (e.g., "Ausruestung")
|
||||
@@ -207,6 +208,7 @@ type MagischAusruestung struct {
|
||||
Ausgebrannt bool `json:"ausgebrannt"`
|
||||
IstMagisch bool `json:"ist_magisch"`
|
||||
}
|
||||
|
||||
type MagischWaffe struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
//ItemType string `gorm:"index" json:"item_type"` // Type of the referenced item (e.g., "Ausruestung")
|
||||
@@ -216,6 +218,7 @@ type MagischWaffe struct {
|
||||
Ausgebrannt bool `json:"ausgebrannt"`
|
||||
IstMagisch bool `json:"ist_magisch"`
|
||||
}
|
||||
|
||||
type MagischBehaelter struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
//ItemType string `gorm:"index" json:"item_type"` // Type of the referenced item (e.g., "Ausruestung")
|
||||
@@ -225,6 +228,7 @@ type MagischBehaelter struct {
|
||||
Ausgebrannt bool `json:"ausgebrannt"`
|
||||
IstMagisch bool `json:"ist_magisch"`
|
||||
}
|
||||
|
||||
type MagischTransport struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
//ItemType string `gorm:"index" json:"item_type"` // Type of the referenced item (e.g., "Ausruestung")
|
||||
|
||||
@@ -32,7 +32,7 @@ func SetupTestDB() *gorm.DB {
|
||||
&Behaeltniss{},
|
||||
&MagischWaffe{},
|
||||
&Waffe{},
|
||||
// &Waffenfertigkeit{},
|
||||
&Waffenfertigkeit{},
|
||||
)
|
||||
return db
|
||||
}
|
||||
@@ -118,6 +118,13 @@ func TestSaveCharacterToDB(t *testing.T) {
|
||||
{Name: "Schwert", Beschreibung: "Ein schwert", Abwb: 0, Anb: 0, Gewicht: 1.5, NameFuerSpezialisierung: "Schwert", Schb: 0, Wert: 3,
|
||||
Magisch: MagischWaffe{IstMagisch: false}},
|
||||
},
|
||||
Waffenfertigkeiten: []Waffenfertigkeit{
|
||||
{Name: "Einhandschlagwaffe", Beschreibung: "z.B. für Kurzschwerter", Bonus: 0,
|
||||
Fertigkeitswert: 12, Pp: 1, Quelle: "Kod 256"},
|
||||
},
|
||||
Spezialisierung: []string{
|
||||
"Bogen", "Streitaxt",
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Println(character)
|
||||
@@ -125,7 +132,7 @@ func TestSaveCharacterToDB(t *testing.T) {
|
||||
// Call the function being tested
|
||||
err := saveCharacterToDB(character)
|
||||
assert.NoError(t, err, "Expected no error when saving character to DB")
|
||||
fmt.Println(character)
|
||||
//fmt.Println(character)
|
||||
|
||||
// Verify that the character was saved
|
||||
var savedCharacter Character
|
||||
@@ -147,6 +154,7 @@ func TestSaveCharacterToDB(t *testing.T) {
|
||||
Preload("Ausruestung").
|
||||
Preload("Behaeltnisse").
|
||||
Preload("Waffen").
|
||||
Preload("Waffenfertigkeiten").
|
||||
First(&savedCharacter, "name = ?", "Test Character").Error
|
||||
assert.NoError(t, err, "Expected to find the character in the database")
|
||||
assert.Equal(t, "Test Character", savedCharacter.Name)
|
||||
@@ -163,7 +171,12 @@ func TestSaveCharacterToDB(t *testing.T) {
|
||||
assert.Equal(t, "Blau", savedCharacter.Merkmale.Augenfarbe)
|
||||
assert.Equal(t, "Backpack", savedCharacter.Behaeltnisse[0].Name)
|
||||
assert.Equal(t, "Schwert", savedCharacter.Waffen[0].Name)
|
||||
assert.Equal(t, 40, savedCharacter.Ap.Value)
|
||||
assert.Equal(t, "Einhandschlagwaffe", savedCharacter.Waffenfertigkeiten[0].Name)
|
||||
assert.Equal(t, 2, len(savedCharacter.Spezialisierung))
|
||||
assert.Equal(t, "Bogen", savedCharacter.Spezialisierung[0])
|
||||
assert.Equal(t, "Streitaxt", savedCharacter.Spezialisierung[1])
|
||||
|
||||
/*
|
||||
assert.Equal(t, 40, savedCharacter.Ap.Value)
|
||||
*/
|
||||
*/
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user