mirror of
https://github.com/wgh136/nysoure.git
synced 2025-12-16 07:51:14 +00:00
fix empty character image
This commit is contained in:
@@ -24,6 +24,10 @@ func CreateResource(r model.Resource) (model.Resource, error) {
|
||||
}
|
||||
for _, c := range characters {
|
||||
c.ResourceID = r.ID
|
||||
// If ImageID is 0, set it to nil to avoid foreign key constraint error
|
||||
if c.ImageID != nil && *c.ImageID == 0 {
|
||||
c.ImageID = nil
|
||||
}
|
||||
if err := tx.Create(&c).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -155,6 +159,10 @@ func UpdateResource(r model.Resource) error {
|
||||
if shouldAdd {
|
||||
c.ID = 0
|
||||
c.ResourceID = r.ID
|
||||
// If ImageID is 0, set it to nil to avoid foreign key constraint error
|
||||
if c.ImageID != nil && *c.ImageID == 0 {
|
||||
c.ImageID = nil
|
||||
}
|
||||
if err := tx.Create(&c).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -520,7 +528,14 @@ func CountResources() (int64, error) {
|
||||
|
||||
// UpdateCharacterImage 更新角色的图片ID
|
||||
func UpdateCharacterImage(characterID, imageID uint) error {
|
||||
result := db.Model(&model.Character{}).Where("id = ?", characterID).Update("image_id", imageID)
|
||||
var updateValue interface{}
|
||||
if imageID == 0 {
|
||||
updateValue = nil
|
||||
} else {
|
||||
updateValue = imageID
|
||||
}
|
||||
|
||||
result := db.Model(&model.Character{}).Where("id = ?", characterID).Update("image_id", updateValue)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ type Character struct {
|
||||
Alias []string `gorm:"serializer:json"`
|
||||
CV string `gorm:"type:varchar(100)"`
|
||||
Role string `gorm:"type:varchar(20);default:primary"`
|
||||
ImageID uint
|
||||
ImageID *uint
|
||||
ResourceID uint
|
||||
Image *Image `gorm:"foreignKey:ImageID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
|
||||
}
|
||||
@@ -21,18 +21,29 @@ type CharacterView struct {
|
||||
}
|
||||
|
||||
func (c *Character) ToView() *CharacterView {
|
||||
var imageID uint
|
||||
if c.ImageID != nil {
|
||||
imageID = *c.ImageID
|
||||
}
|
||||
return &CharacterView{
|
||||
Id: c.ID,
|
||||
Name: c.Name,
|
||||
Alias: c.Alias,
|
||||
CV: c.CV,
|
||||
Role: c.Role,
|
||||
Image: c.ImageID,
|
||||
Image: imageID,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Character) Equal(other *Character) bool {
|
||||
if c.Name != other.Name || c.CV != other.CV || c.Role != other.Role || c.ImageID != other.ImageID {
|
||||
if c.Name != other.Name || c.CV != other.CV || c.Role != other.Role {
|
||||
return false
|
||||
}
|
||||
// Compare ImageID pointers
|
||||
if (c.ImageID == nil) != (other.ImageID == nil) {
|
||||
return false
|
||||
}
|
||||
if c.ImageID != nil && other.ImageID != nil && *c.ImageID != *other.ImageID {
|
||||
return false
|
||||
}
|
||||
if len(c.Alias) != len(other.Alias) {
|
||||
|
||||
@@ -89,12 +89,16 @@ func CreateResource(uid uint, params *ResourceParams) (uint, error) {
|
||||
if role == "" {
|
||||
role = "primary"
|
||||
}
|
||||
var imageID *uint
|
||||
if c.Image != 0 {
|
||||
imageID = &c.Image
|
||||
}
|
||||
characters[i] = model.Character{
|
||||
Name: c.Name,
|
||||
Alias: c.Alias,
|
||||
CV: c.CV,
|
||||
Role: role,
|
||||
ImageID: c.Image,
|
||||
ImageID: imageID,
|
||||
}
|
||||
}
|
||||
r := model.Resource{
|
||||
@@ -512,12 +516,16 @@ func UpdateResource(uid, rid uint, params *ResourceParams) error {
|
||||
if role == "" {
|
||||
role = "primary"
|
||||
}
|
||||
var imageID *uint
|
||||
if c.Image != 0 {
|
||||
imageID = &c.Image
|
||||
}
|
||||
characters[i] = model.Character{
|
||||
Name: c.Name,
|
||||
Alias: c.Alias,
|
||||
CV: c.CV,
|
||||
Role: role,
|
||||
ImageID: c.Image,
|
||||
ImageID: imageID,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user