mirror of
https://github.com/wgh136/nysoure.git
synced 2025-12-16 15: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 {
|
for _, c := range characters {
|
||||||
c.ResourceID = r.ID
|
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 {
|
if err := tx.Create(&c).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -155,6 +159,10 @@ func UpdateResource(r model.Resource) error {
|
|||||||
if shouldAdd {
|
if shouldAdd {
|
||||||
c.ID = 0
|
c.ID = 0
|
||||||
c.ResourceID = r.ID
|
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 {
|
if err := tx.Create(&c).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -520,7 +528,14 @@ func CountResources() (int64, error) {
|
|||||||
|
|
||||||
// UpdateCharacterImage 更新角色的图片ID
|
// UpdateCharacterImage 更新角色的图片ID
|
||||||
func UpdateCharacterImage(characterID, imageID uint) error {
|
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 {
|
if result.Error != nil {
|
||||||
return result.Error
|
return result.Error
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ type Character struct {
|
|||||||
Alias []string `gorm:"serializer:json"`
|
Alias []string `gorm:"serializer:json"`
|
||||||
CV string `gorm:"type:varchar(100)"`
|
CV string `gorm:"type:varchar(100)"`
|
||||||
Role string `gorm:"type:varchar(20);default:primary"`
|
Role string `gorm:"type:varchar(20);default:primary"`
|
||||||
ImageID uint
|
ImageID *uint
|
||||||
ResourceID uint
|
ResourceID uint
|
||||||
Image *Image `gorm:"foreignKey:ImageID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
|
Image *Image `gorm:"foreignKey:ImageID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
|
||||||
}
|
}
|
||||||
@@ -21,18 +21,29 @@ type CharacterView struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Character) ToView() *CharacterView {
|
func (c *Character) ToView() *CharacterView {
|
||||||
|
var imageID uint
|
||||||
|
if c.ImageID != nil {
|
||||||
|
imageID = *c.ImageID
|
||||||
|
}
|
||||||
return &CharacterView{
|
return &CharacterView{
|
||||||
Id: c.ID,
|
Id: c.ID,
|
||||||
Name: c.Name,
|
Name: c.Name,
|
||||||
Alias: c.Alias,
|
Alias: c.Alias,
|
||||||
CV: c.CV,
|
CV: c.CV,
|
||||||
Role: c.Role,
|
Role: c.Role,
|
||||||
Image: c.ImageID,
|
Image: imageID,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Character) Equal(other *Character) bool {
|
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
|
return false
|
||||||
}
|
}
|
||||||
if len(c.Alias) != len(other.Alias) {
|
if len(c.Alias) != len(other.Alias) {
|
||||||
|
|||||||
@@ -89,12 +89,16 @@ func CreateResource(uid uint, params *ResourceParams) (uint, error) {
|
|||||||
if role == "" {
|
if role == "" {
|
||||||
role = "primary"
|
role = "primary"
|
||||||
}
|
}
|
||||||
|
var imageID *uint
|
||||||
|
if c.Image != 0 {
|
||||||
|
imageID = &c.Image
|
||||||
|
}
|
||||||
characters[i] = model.Character{
|
characters[i] = model.Character{
|
||||||
Name: c.Name,
|
Name: c.Name,
|
||||||
Alias: c.Alias,
|
Alias: c.Alias,
|
||||||
CV: c.CV,
|
CV: c.CV,
|
||||||
Role: role,
|
Role: role,
|
||||||
ImageID: c.Image,
|
ImageID: imageID,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
r := model.Resource{
|
r := model.Resource{
|
||||||
@@ -512,12 +516,16 @@ func UpdateResource(uid, rid uint, params *ResourceParams) error {
|
|||||||
if role == "" {
|
if role == "" {
|
||||||
role = "primary"
|
role = "primary"
|
||||||
}
|
}
|
||||||
|
var imageID *uint
|
||||||
|
if c.Image != 0 {
|
||||||
|
imageID = &c.Image
|
||||||
|
}
|
||||||
characters[i] = model.Character{
|
characters[i] = model.Character{
|
||||||
Name: c.Name,
|
Name: c.Name,
|
||||||
Alias: c.Alias,
|
Alias: c.Alias,
|
||||||
CV: c.CV,
|
CV: c.CV,
|
||||||
Role: role,
|
Role: role,
|
||||||
ImageID: c.Image,
|
ImageID: imageID,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user