added GameSystemID to Char
I am curious if this is enough
This commit is contained in:
@@ -103,6 +103,8 @@ func UpdateCharacter(c *gin.Context) {
|
|||||||
|
|
||||||
// Store the original ID to preserve it
|
// Store the original ID to preserve it
|
||||||
originalID := character.ID
|
originalID := character.ID
|
||||||
|
originalGameSystem := character.GameSystem
|
||||||
|
originalGameSystemId := character.GameSystemId
|
||||||
|
|
||||||
// Bind the updated data
|
// Bind the updated data
|
||||||
if err := c.ShouldBindJSON(&character); err != nil {
|
if err := c.ShouldBindJSON(&character); err != nil {
|
||||||
@@ -112,6 +114,8 @@ func UpdateCharacter(c *gin.Context) {
|
|||||||
|
|
||||||
// Restore the ID
|
// Restore the ID
|
||||||
character.ID = originalID
|
character.ID = originalID
|
||||||
|
character.GameSystem = originalGameSystem
|
||||||
|
character.GameSystemId = originalGameSystemId
|
||||||
|
|
||||||
// Update all associations
|
// Update all associations
|
||||||
if err := database.DB.Session(&gorm.Session{FullSaveAssociations: true}).Save(&character).Error; err != nil {
|
if err := database.DB.Session(&gorm.Session{FullSaveAssociations: true}).Save(&character).Error; err != nil {
|
||||||
|
|||||||
@@ -76,21 +76,23 @@ type Vermoegen struct {
|
|||||||
|
|
||||||
type Char struct {
|
type Char struct {
|
||||||
BamortBase
|
BamortBase
|
||||||
UserID uint `gorm:"index;not null;default:1" json:"user_id"`
|
GameSystem string `gorm:"column:game_system;index;default:midgard" json:"game_system"`
|
||||||
User user.User `gorm:"foreignKey:UserID;references:UserID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE" json:"user"`
|
GameSystemId uint `json:"game_system_id,omitempty"`
|
||||||
Rasse string `json:"rasse"`
|
UserID uint `gorm:"index;not null;default:1" json:"user_id"`
|
||||||
Typ string `json:"typ"`
|
User user.User `gorm:"foreignKey:UserID;references:UserID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE" json:"user"`
|
||||||
Alter int `json:"alter"`
|
Rasse string `json:"rasse"`
|
||||||
Anrede string `json:"anrede"`
|
Typ string `json:"typ"`
|
||||||
Grad int `json:"grad"`
|
Alter int `json:"alter"`
|
||||||
Gender string `json:"gender"`
|
Anrede string `json:"anrede"`
|
||||||
SocialClass string `json:"social_class"`
|
Grad int `json:"grad"`
|
||||||
Groesse int `json:"groesse"`
|
Gender string `json:"gender"`
|
||||||
Gewicht int `json:"gewicht"`
|
SocialClass string `json:"social_class"`
|
||||||
Herkunft string `json:"origin"`
|
Groesse int `json:"groesse"`
|
||||||
Glaube string `json:"glaube"`
|
Gewicht int `json:"gewicht"`
|
||||||
Hand string `json:"hand"`
|
Herkunft string `json:"origin"`
|
||||||
Public bool `json:"public"`
|
Glaube string `json:"glaube"`
|
||||||
|
Hand string `json:"hand"`
|
||||||
|
Public bool `json:"public"`
|
||||||
// Static derived values (can increase with grade)
|
// Static derived values (can increase with grade)
|
||||||
ResistenzKoerper int `json:"resistenz_koerper"`
|
ResistenzKoerper int `json:"resistenz_koerper"`
|
||||||
ResistenzGeist int `json:"resistenz_geist"`
|
ResistenzGeist int `json:"resistenz_geist"`
|
||||||
@@ -117,12 +119,14 @@ type Char struct {
|
|||||||
}
|
}
|
||||||
type CharList struct {
|
type CharList struct {
|
||||||
BamortBase
|
BamortBase
|
||||||
UserID uint `json:"user_id"`
|
GameSystem string `gorm:"column:game_system" json:"game_system"`
|
||||||
Rasse string `json:"rasse"`
|
GameSystemId uint `json:"game_system_id"`
|
||||||
Typ string `json:"typ"`
|
UserID uint `json:"user_id"`
|
||||||
Grad int `json:"grad"`
|
Rasse string `json:"rasse"`
|
||||||
Owner string `json:"owner"`
|
Typ string `json:"typ"`
|
||||||
Public bool `json:"public"`
|
Grad int `json:"grad"`
|
||||||
|
Owner string `json:"owner"`
|
||||||
|
Public bool `json:"public"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type FeChar struct {
|
type FeChar struct {
|
||||||
@@ -136,7 +140,32 @@ func (object *Char) TableName() string {
|
|||||||
return dbPrefix + "_" + "chars"
|
return dbPrefix + "_" + "chars"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (object *Char) ensureGameSystem() {
|
||||||
|
gs := GetGameSystem(object.GameSystemId, object.GameSystem)
|
||||||
|
if gs == nil {
|
||||||
|
gs = GetGameSystem(0, "midgard")
|
||||||
|
}
|
||||||
|
if gs != nil {
|
||||||
|
object.GameSystemId = gs.ID
|
||||||
|
object.GameSystem = gs.Name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (object *Char) BeforeCreate(tx *gorm.DB) error {
|
||||||
|
object.ensureGameSystem()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (object *Char) BeforeSave(tx *gorm.DB) error {
|
||||||
|
object.ensureGameSystem()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (object *Char) First(charName string) error {
|
func (object *Char) First(charName string) error {
|
||||||
|
gs := GetGameSystem(object.GameSystemId, object.GameSystem)
|
||||||
|
if gs == nil {
|
||||||
|
gs = GetGameSystem(0, "midgard")
|
||||||
|
}
|
||||||
err := database.DB.
|
err := database.DB.
|
||||||
Preload("User").
|
Preload("User").
|
||||||
Preload("Lp").
|
Preload("Lp").
|
||||||
@@ -154,7 +183,8 @@ func (object *Char) First(charName string) error {
|
|||||||
Preload("Behaeltnisse").
|
Preload("Behaeltnisse").
|
||||||
Preload("Transportmittel").
|
Preload("Transportmittel").
|
||||||
Preload("Ausruestung").
|
Preload("Ausruestung").
|
||||||
First(&object, " name = ?", charName).Error
|
Where("(game_system = ? OR game_system_id = ?) AND name = ?", gs.Name, gs.ID, charName).
|
||||||
|
First(&object).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// zauber found
|
// zauber found
|
||||||
return err
|
return err
|
||||||
@@ -163,6 +193,10 @@ func (object *Char) First(charName string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (object *Char) FirstID(charID string) error {
|
func (object *Char) FirstID(charID string) error {
|
||||||
|
gs := GetGameSystem(object.GameSystemId, object.GameSystem)
|
||||||
|
if gs == nil {
|
||||||
|
gs = GetGameSystem(0, "midgard")
|
||||||
|
}
|
||||||
err := database.DB.
|
err := database.DB.
|
||||||
Preload("User").
|
Preload("User").
|
||||||
Preload("Lp").
|
Preload("Lp").
|
||||||
@@ -180,7 +214,8 @@ func (object *Char) FirstID(charID string) error {
|
|||||||
Preload("Behaeltnisse").
|
Preload("Behaeltnisse").
|
||||||
Preload("Transportmittel").
|
Preload("Transportmittel").
|
||||||
Preload("Ausruestung").
|
Preload("Ausruestung").
|
||||||
First(&object, charID).Error
|
Where("(game_system = ? OR game_system_id = ?) AND char_chars.id = ?", gs.Name, gs.ID, charID).
|
||||||
|
First(&object).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// zauber found
|
// zauber found
|
||||||
return err
|
return err
|
||||||
@@ -191,6 +226,10 @@ func (object *Char) FirstID(charID string) error {
|
|||||||
// FindByUserID finds all characters belonging to a specific user
|
// FindByUserID finds all characters belonging to a specific user
|
||||||
func (object *Char) FindByUserID(userID uint) ([]Char, error) {
|
func (object *Char) FindByUserID(userID uint) ([]Char, error) {
|
||||||
var chars []Char
|
var chars []Char
|
||||||
|
gs := GetGameSystem(object.GameSystemId, object.GameSystem)
|
||||||
|
if gs == nil {
|
||||||
|
gs = GetGameSystem(0, "midgard")
|
||||||
|
}
|
||||||
err := database.DB.
|
err := database.DB.
|
||||||
Preload("User").
|
Preload("User").
|
||||||
Preload("Lp").
|
Preload("Lp").
|
||||||
@@ -208,7 +247,7 @@ func (object *Char) FindByUserID(userID uint) ([]Char, error) {
|
|||||||
Preload("Behaeltnisse").
|
Preload("Behaeltnisse").
|
||||||
Preload("Transportmittel").
|
Preload("Transportmittel").
|
||||||
Preload("Ausruestung").
|
Preload("Ausruestung").
|
||||||
Where("user_id = ?", userID).
|
Where("user_id = ? AND (game_system = ? OR game_system_id = ?)", userID, gs.Name, gs.ID).
|
||||||
Find(&chars).Error
|
Find(&chars).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -218,10 +257,11 @@ func (object *Char) FindByUserID(userID uint) ([]Char, error) {
|
|||||||
|
|
||||||
func FindPublicCharList() ([]CharList, error) {
|
func FindPublicCharList() ([]CharList, error) {
|
||||||
var chars []CharList
|
var chars []CharList
|
||||||
|
gs := GetGameSystem(0, "midgard")
|
||||||
err := database.DB.Table("char_chars").
|
err := database.DB.Table("char_chars").
|
||||||
Select("char_chars.id, char_chars.name, char_chars.user_id, char_chars.rasse, char_chars.typ, char_chars.grad, char_chars.public, users.username as owner").
|
Select("char_chars.id, char_chars.name, char_chars.user_id, char_chars.rasse, char_chars.typ, char_chars.grad, char_chars.public, char_chars.game_system, char_chars.game_system_id, users.username as owner").
|
||||||
Joins("LEFT JOIN users ON char_chars.user_id = users.user_id").
|
Joins("LEFT JOIN users ON char_chars.user_id = users.user_id").
|
||||||
Where("char_chars.public = ?", true).
|
Where("char_chars.public = ? AND (char_chars.game_system = ? OR char_chars.game_system_id = ?)", true, gs.Name, gs.ID).
|
||||||
Find(&chars).Error
|
Find(&chars).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -232,10 +272,11 @@ func FindPublicCharList() ([]CharList, error) {
|
|||||||
// FindCharListByUserID finds all characters belonging to a specific user for listing (minimal data)
|
// FindCharListByUserID finds all characters belonging to a specific user for listing (minimal data)
|
||||||
func FindCharListByUserID(userID uint) ([]CharList, error) {
|
func FindCharListByUserID(userID uint) ([]CharList, error) {
|
||||||
var chars []CharList
|
var chars []CharList
|
||||||
|
gs := GetGameSystem(0, "midgard")
|
||||||
err := database.DB.Table("char_chars").
|
err := database.DB.Table("char_chars").
|
||||||
Select("char_chars.id, char_chars.name, char_chars.user_id, char_chars.rasse, char_chars.typ, char_chars.grad, char_chars.public, users.username as owner").
|
Select("char_chars.id, char_chars.name, char_chars.user_id, char_chars.rasse, char_chars.typ, char_chars.grad, char_chars.public, char_chars.game_system, char_chars.game_system_id, users.username as owner").
|
||||||
Joins("LEFT JOIN users ON char_chars.user_id = users.user_id").
|
Joins("LEFT JOIN users ON char_chars.user_id = users.user_id").
|
||||||
Where("char_chars.user_id = ?", userID).
|
Where("char_chars.user_id = ? AND (char_chars.game_system = ? OR char_chars.game_system_id = ?)", userID, gs.Name, gs.ID).
|
||||||
Find(&chars).Error
|
Find(&chars).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -244,6 +285,7 @@ func FindCharListByUserID(userID uint) ([]CharList, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (object *Char) Create() error {
|
func (object *Char) Create() error {
|
||||||
|
object.ensureGameSystem()
|
||||||
err := database.DB.Transaction(func(tx *gorm.DB) error {
|
err := database.DB.Transaction(func(tx *gorm.DB) error {
|
||||||
// Save the main character record
|
// Save the main character record
|
||||||
if err := tx.Create(&object).Error; err != nil {
|
if err := tx.Create(&object).Error; err != nil {
|
||||||
|
|||||||
@@ -120,6 +120,23 @@ func TestChar_Create_Success(t *testing.T) {
|
|||||||
assert.Greater(t, testChar.ID, uint(0), "ID should be set after create")
|
assert.Greater(t, testChar.ID, uint(0), "ID should be set after create")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestChar_Create_SetsGameSystem(t *testing.T) {
|
||||||
|
setupCharacterTestDB(t)
|
||||||
|
|
||||||
|
gs := GetGameSystem(0, "midgard")
|
||||||
|
require.NotNil(t, gs)
|
||||||
|
|
||||||
|
testChar := createTestChar("Test Character GS")
|
||||||
|
testChar.GameSystem = ""
|
||||||
|
testChar.GameSystemId = 0
|
||||||
|
|
||||||
|
err := testChar.Create()
|
||||||
|
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, gs.Name, testChar.GameSystem)
|
||||||
|
assert.Equal(t, gs.ID, testChar.GameSystemId)
|
||||||
|
}
|
||||||
|
|
||||||
func TestChar_Create_WithRelations(t *testing.T) {
|
func TestChar_Create_WithRelations(t *testing.T) {
|
||||||
setupCharacterTestDB(t)
|
setupCharacterTestDB(t)
|
||||||
|
|
||||||
@@ -157,6 +174,33 @@ func TestChar_First_Success(t *testing.T) {
|
|||||||
assert.Equal(t, testChar.Rasse, foundChar.Rasse, "Found character should have same race")
|
assert.Equal(t, testChar.Rasse, foundChar.Rasse, "Found character should have same race")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestChar_First_UsesGameSystem(t *testing.T) {
|
||||||
|
setupCharacterTestDB(t)
|
||||||
|
|
||||||
|
defaultGS := GetGameSystem(0, "midgard")
|
||||||
|
require.NotNil(t, defaultGS)
|
||||||
|
|
||||||
|
altGS := &GameSystem{Code: "ALT", Name: "AltSystem", Description: "Test", IsActive: true}
|
||||||
|
require.NoError(t, database.DB.Create(altGS).Error)
|
||||||
|
|
||||||
|
charDefault := createTestChar("GS Default Char")
|
||||||
|
require.NoError(t, charDefault.Create())
|
||||||
|
|
||||||
|
charAlt := createTestChar("GS Alt Char")
|
||||||
|
charAlt.GameSystem = altGS.Code
|
||||||
|
charAlt.GameSystemId = altGS.ID
|
||||||
|
require.NoError(t, charAlt.Create())
|
||||||
|
|
||||||
|
finder := &Char{GameSystem: defaultGS.Name, GameSystemId: defaultGS.ID}
|
||||||
|
err := finder.First("GS Default Char")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, defaultGS.ID, finder.GameSystemId)
|
||||||
|
|
||||||
|
otherFinder := &Char{GameSystem: altGS.Code, GameSystemId: altGS.ID}
|
||||||
|
err = otherFinder.First("GS Default Char")
|
||||||
|
assert.Error(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
func TestChar_First_NotFound(t *testing.T) {
|
func TestChar_First_NotFound(t *testing.T) {
|
||||||
setupCharacterTestDB(t)
|
setupCharacterTestDB(t)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user