improvements

This commit is contained in:
Lunny Xiao 2025-07-16 19:45:30 -07:00
parent 3abb7571a8
commit 8f97a2d0b1
4 changed files with 16 additions and 19 deletions

View File

@ -185,6 +185,19 @@ func UpdateAttachment(ctx context.Context, atta *Attachment) error {
return err
}
func DeleteAttachments(ctx context.Context, attachments []*Attachment) (int64, error) {
if len(attachments) == 0 {
return 0, nil
}
ids := make([]int64, 0, len(attachments))
for _, a := range attachments {
ids = append(ids, a.ID)
}
return db.GetEngine(ctx).In("id", ids).NoAutoCondition().Delete(attachments[0])
}
// DeleteAttachmentsByRelease deletes all attachments associated with the given release.
func DeleteAttachmentsByRelease(ctx context.Context, releaseID int64) error {
_, err := db.GetEngine(ctx).Where("release_id = ?", releaseID).Delete(&Attachment{})

View File

@ -71,16 +71,7 @@ func DeleteAttachment(ctx context.Context, a *repo_model.Attachment) error {
// DeleteAttachments deletes the given attachments and optionally the associated files.
func DeleteAttachments(ctx context.Context, attachments []*repo_model.Attachment) (int, error) {
if len(attachments) == 0 {
return 0, nil
}
ids := make([]int64, 0, len(attachments))
for _, a := range attachments {
ids = append(ids, a.ID)
}
cnt, err := db.GetEngine(ctx).In("id", ids).NoAutoCondition().Delete(attachments[0])
cnt, err := repo_model.DeleteAttachments(ctx, attachments)
if err != nil {
return 0, err
}

View File

@ -152,8 +152,7 @@ func deleteComment(ctx context.Context, comment *issues_model.Comment, removeAtt
if removeAttachments {
// delete comment attachments
_, err := db.GetEngine(ctx).Where("comment_id = ?", comment.ID).NoAutoCondition().Delete(&repo_model.Attachment{})
if err != nil {
if _, err := repo_model.DeleteAttachments(ctx, comment.Attachments); err != nil {
return nil, err
}

View File

@ -131,13 +131,7 @@ func deleteUser(ctx context.Context, u *user_model.User, purge bool) (cleanup ut
return nil, err
}
ids := make([]int64, 0, len(comment.Attachments))
for _, a := range comment.Attachments {
ids = append(ids, a.ID)
}
_, err := db.GetEngine(ctx).In("id", ids).NoAutoCondition().Delete(&repo_model.Attachment{})
if err != nil {
if _, err := repo_model.DeleteAttachments(ctx, comment.Attachments); err != nil {
return nil, err
}