0
0
Fork 0
mirror of https://github.com/go-gitea/gitea synced 2024-12-23 04:04:14 +01:00

refactor(models/attachement): use getAttachmentsByUUIDs (#9317)

This commit is contained in:
Antoine GIRARD 2019-12-11 01:01:52 +01:00 committed by zeripath
parent 50da9f7dae
commit c3d31e5534
4 changed files with 23 additions and 20 deletions

View file

@ -133,6 +133,11 @@ func getAttachmentByUUID(e Engine, uuid string) (*Attachment, error) {
return attach, nil return attach, nil
} }
// GetAttachmentsByUUIDs returns attachment by given UUID list.
func GetAttachmentsByUUIDs(uuids []string) ([]*Attachment, error) {
return getAttachmentsByUUIDs(x, uuids)
}
func getAttachmentsByUUIDs(e Engine, uuids []string) ([]*Attachment, error) { func getAttachmentsByUUIDs(e Engine, uuids []string) ([]*Attachment, error) {
if len(uuids) == 0 { if len(uuids) == 0 {
return []*Attachment{}, nil return []*Attachment{}, nil

View file

@ -116,3 +116,15 @@ func TestUpdateAttachment(t *testing.T) {
AssertExistsAndLoadBean(t, &Attachment{Name: "new_name"}) AssertExistsAndLoadBean(t, &Attachment{Name: "new_name"})
} }
func TestGetAttachmentsByUUIDs(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
attachList, err := GetAttachmentsByUUIDs([]string{"a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a17", "not-existing-uuid"})
assert.NoError(t, err)
assert.Equal(t, 2, len(attachList))
assert.Equal(t, "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", attachList[0].UUID)
assert.Equal(t, "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a17", attachList[1].UUID)
assert.Equal(t, int64(1), attachList[0].IssueID)
assert.Equal(t, int64(5), attachList[1].IssueID)
}

View file

@ -567,16 +567,9 @@ func updateCommentInfos(e *xorm.Session, opts *CreateCommentOptions, comment *Co
} }
// Check attachments // Check attachments
attachments := make([]*Attachment, 0, len(opts.Attachments)) attachments, err := getAttachmentsByUUIDs(e, opts.Attachments)
for _, uuid := range opts.Attachments { if err != nil {
attach, err := getAttachmentByUUID(e, uuid) return fmt.Errorf("getAttachmentsByUUIDs [uuids: %v]: %v", opts.Attachments, err)
if err != nil {
if IsErrAttachmentNotExist(err) {
continue
}
return fmt.Errorf("getAttachmentByUUID [%s]: %v", uuid, err)
}
attachments = append(attachments, attach)
} }
for i := range attachments { for i := range attachments {

View file

@ -129,16 +129,9 @@ func UpdateRelease(rel *Release) error {
// AddReleaseAttachments adds a release attachments // AddReleaseAttachments adds a release attachments
func AddReleaseAttachments(releaseID int64, attachmentUUIDs []string) (err error) { func AddReleaseAttachments(releaseID int64, attachmentUUIDs []string) (err error) {
// Check attachments // Check attachments
var attachments = make([]*Attachment, 0) attachments, err := GetAttachmentsByUUIDs(attachmentUUIDs)
for _, uuid := range attachmentUUIDs { if err != nil {
attach, err := getAttachmentByUUID(x, uuid) return fmt.Errorf("GetAttachmentsByUUIDs [uuids: %v]: %v", attachmentUUIDs, err)
if err != nil {
if IsErrAttachmentNotExist(err) {
continue
}
return fmt.Errorf("getAttachmentByUUID [%s]: %v", uuid, err)
}
attachments = append(attachments, attach)
} }
for i := range attachments { for i := range attachments {