Skip to content

Commit 2775a3b

Browse files
committed
improvements
1 parent 8dfd601 commit 2775a3b

File tree

4 files changed

+16
-19
lines changed

4 files changed

+16
-19
lines changed

models/repo/attachment.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,19 @@ func UpdateAttachment(ctx context.Context, atta *Attachment) error {
185185
return err
186186
}
187187

188+
func DeleteAttachments(ctx context.Context, attachments []*Attachment) (int64, error) {
189+
if len(attachments) == 0 {
190+
return 0, nil
191+
}
192+
193+
ids := make([]int64, 0, len(attachments))
194+
for _, a := range attachments {
195+
ids = append(ids, a.ID)
196+
}
197+
198+
return db.GetEngine(ctx).In("id", ids).NoAutoCondition().Delete(attachments[0])
199+
}
200+
188201
// DeleteAttachmentsByRelease deletes all attachments associated with the given release.
189202
func DeleteAttachmentsByRelease(ctx context.Context, releaseID int64) error {
190203
_, err := db.GetEngine(ctx).Where("release_id = ?", releaseID).Delete(&Attachment{})

services/attachment/attachment.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,7 @@ func DeleteAttachment(ctx context.Context, a *repo_model.Attachment) error {
7171

7272
// DeleteAttachments deletes the given attachments and optionally the associated files.
7373
func DeleteAttachments(ctx context.Context, attachments []*repo_model.Attachment) (int, error) {
74-
if len(attachments) == 0 {
75-
return 0, nil
76-
}
77-
78-
ids := make([]int64, 0, len(attachments))
79-
for _, a := range attachments {
80-
ids = append(ids, a.ID)
81-
}
82-
83-
cnt, err := db.GetEngine(ctx).In("id", ids).NoAutoCondition().Delete(attachments[0])
74+
cnt, err := repo_model.DeleteAttachments(ctx, attachments)
8475
if err != nil {
8576
return 0, err
8677
}

services/issue/comments.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,7 @@ func deleteComment(ctx context.Context, comment *issues_model.Comment, removeAtt
152152

153153
if removeAttachments {
154154
// delete comment attachments
155-
_, err := db.GetEngine(ctx).Where("comment_id = ?", comment.ID).NoAutoCondition().Delete(&repo_model.Attachment{})
156-
if err != nil {
155+
if _, err := repo_model.DeleteAttachments(ctx, comment.Attachments); err != nil {
157156
return nil, err
158157
}
159158

services/user/delete.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,7 @@ func deleteUser(ctx context.Context, u *user_model.User, purge bool) (cleanup ut
131131
return nil, err
132132
}
133133

134-
ids := make([]int64, 0, len(comment.Attachments))
135-
for _, a := range comment.Attachments {
136-
ids = append(ids, a.ID)
137-
}
138-
139-
_, err := db.GetEngine(ctx).In("id", ids).NoAutoCondition().Delete(&repo_model.Attachment{})
140-
if err != nil {
134+
if _, err := repo_model.DeleteAttachments(ctx, comment.Attachments); err != nil {
141135
return nil, err
142136
}
143137

0 commit comments

Comments
 (0)