A continuation of HomeBox the inventory and organization system built for the Home User
fix: ensure only one attachment is marked as primary (#287)
Michael Waynec2546f06d49e
diff --git a/backend/internal/data/repo/repo_item_attachments.go b/backend/internal/data/repo/repo_item_attachments.goindex 7be4d7fe..d4f8e568 100644--- a/backend/internal/data/repo/repo_item_attachments.go+++ b/backend/internal/data/repo/repo_item_attachments.go@@ -87,11 +87,11 @@ func (r *AttachmentRepo) Get(ctx context.Context, id uuid.UUID) (*ent.AttachmentOnly(ctx)}-func (r *AttachmentRepo) Update(ctx context.Context, itemID uuid.UUID, data *ItemAttachmentUpdate) (*ent.Attachment, error) {+func (r *AttachmentRepo) Update(ctx context.Context, id uuid.UUID, data *ItemAttachmentUpdate) (*ent.Attachment, error) {// TODO: execute within Txtyp := attachment.Type(data.Type)- bldr := r.db.Attachment.UpdateOneID(itemID).+ bldr := r.db.Attachment.UpdateOneID(id).SetType(typ)// Primary only applies to photos@@ -101,7 +101,12 @@ func (r *AttachmentRepo) Update(ctx context.Context, itemID uuid.UUID, data *Itebldr = bldr.SetPrimary(false)}- itm, err := bldr.Save(ctx)+ updatedAttachment, err := bldr.Save(ctx)+ if err != nil {+ return nil, err+ }++ attachmentItem, err := updatedAttachment.QueryItem().Only(ctx)if err != nil {return nil, err}@@ -109,8 +114,8 @@ func (r *AttachmentRepo) Update(ctx context.Context, itemID uuid.UUID, data *Ite// Ensure all other attachments are not primaryerr = r.db.Attachment.Update().Where(- attachment.HasItemWith(item.ID(itemID)),- attachment.IDNEQ(itm.ID),+ attachment.HasItemWith(item.ID(attachmentItem.ID)),+ attachment.IDNEQ(updatedAttachment.ID),).SetPrimary(false).Exec(ctx)@@ -118,7 +123,7 @@ func (r *AttachmentRepo) Update(ctx context.Context, itemID uuid.UUID, data *Itereturn nil, err}- return r.Get(ctx, itm.ID)+ return r.Get(ctx, updatedAttachment.ID)}func (r *AttachmentRepo) Delete(ctx context.Context, id uuid.UUID) error {diff --git a/backend/internal/data/repo/repo_item_attachments_test.go b/backend/internal/data/repo/repo_item_attachments_test.goindex 22a2256c..1dd922b9 100644--- a/backend/internal/data/repo/repo_item_attachments_test.go+++ b/backend/internal/data/repo/repo_item_attachments_test.go@@ -133,3 +133,25 @@ func TestAttachmentRepo_Delete(t *testing.T) {_, err = tRepos.Attachments.Get(context.Background(), entity.ID)require.Error(t, err)}++func TestAttachmentRepo_EnsureSinglePrimaryAttachment(t *testing.T) {+ ctx := context.Background()+ attachments := useAttachments(t, 2)++ setAndVerifyPrimary := func(primaryAttachmentID, nonPrimaryAttachmentID uuid.UUID) {+ primaryAttachment, err := tRepos.Attachments.Update(ctx, primaryAttachmentID, &ItemAttachmentUpdate{+ Type: attachment.TypePhoto.String(),+ Primary: true,+ })+ require.NoError(t, err)++ nonPrimaryAttachment, err := tRepos.Attachments.Get(ctx, nonPrimaryAttachmentID)+ require.NoError(t, err)++ assert.True(t, primaryAttachment.Primary)+ assert.False(t, nonPrimaryAttachment.Primary)+ }++ setAndVerifyPrimary(attachments[0].ID, attachments[1].ID)+ setAndVerifyPrimary(attachments[1].ID, attachments[0].ID)+}