Casedase deleting attachment

This commit is contained in:
LittleSheep 2024-12-28 20:08:32 +08:00
parent a4e81cabec
commit edbe412f97

View File

@ -197,10 +197,15 @@ func UpdateAttachment(item models.Attachment) (models.Attachment, error) {
return item, nil return item, nil
} }
func DeleteAttachment(item models.Attachment) error { func DeleteAttachment(item models.Attachment, txs ...*gorm.DB) error {
dat := item dat := item
tx := database.C.Begin() var tx *gorm.DB
if len(txs) == 0 {
tx = database.C.Begin()
} else {
tx = txs[0]
}
if item.RefID != nil { if item.RefID != nil {
var refTarget models.Attachment var refTarget models.Attachment
@ -212,6 +217,16 @@ func DeleteAttachment(item models.Attachment) error {
} }
} }
} }
if item.Thumbnail != nil {
if err := DeleteAttachment(*item.Thumbnail, tx); err != nil {
return err
}
}
if item.Compressed != nil {
if err := DeleteAttachment(*item.Compressed, tx); err != nil {
return err
}
}
if err := database.C.Delete(&item).Error; err != nil { if err := database.C.Delete(&item).Error; err != nil {
tx.Rollback() tx.Rollback()
return err return err