diff --git a/api/src/model/transactions.go b/api/src/model/transactions.go index 36474ba..0176b30 100644 --- a/api/src/model/transactions.go +++ b/api/src/model/transactions.go @@ -40,13 +40,26 @@ func UpdateGiftToDb(db *gorm.DB, id int64, inputGift Gift) (Gift, error) { } // Update Gift Record - updates := map[string]interface{}{ - "Name": inputGift.Name, - "Price": inputGift.Price, - "Link": inputGift.Link, - "Description": inputGift.Description, - "Demographic": inputGift.Demographic, - "GiftCollections": inputGift.GiftCollections, + updates := make(map[string]interface{}) + + // Check each field in inputGift and add it to the updates map if it is non-zero + if inputGift.Name != "" { + updates["Name"] = inputGift.Name + } + if inputGift.Price != 0 { + updates["Price"] = inputGift.Price + } + if inputGift.Link != "" { + updates["Link"] = inputGift.Link + } + if inputGift.Description != "" { + updates["Description"] = inputGift.Description + } + if inputGift.Demographic != "" { + updates["Demographic"] = inputGift.Demographic + } + if inputGift.GiftCollections != nil && len(inputGift.GiftCollections) > 0 { + updates["GiftCollections"] = inputGift.GiftCollections } if err := db.Model(&updatedGift).Updates(updates).Error; err != nil { diff --git a/api/tests/api_test.go b/api/tests/api_test.go index cf79876..0d70c1f 100644 --- a/api/tests/api_test.go +++ b/api/tests/api_test.go @@ -601,7 +601,6 @@ func TestUpdateGift(t *testing.T) { updatedTestGift := model.Gift{ Name: "updatedgift1", Price: 100, - Link: "updatedlink1", Description: "updateddescription1", Demographic: "updateddemogrpahic1", GiftCollections: nil, @@ -624,15 +623,17 @@ func TestUpdateGift(t *testing.T) { if e := json.Unmarshal(w.Body.Bytes(), &updatedGiftRetrieved); e != nil { t.Fatalf("Error unmarshaling JSON: %v", e) } + fmt.Print(updatedGiftRetrieved.ID) var fetchedUpdatedGift model.Gift err = tx.First(&fetchedUpdatedGift, updatedGiftRetrieved.ID).Error + assert.NoError(t, err) err = tx.First(&updatedGiftRetrieved, fetchedUpdatedGift.ID).Error assert.NoError(t, err) assert.Equal(t, fetchedUpdatedGift.ID, updatedGiftRetrieved.ID) assert.Equal(t, fetchedUpdatedGift.Name, updatedGiftRetrieved.Name) assert.Equal(t, fetchedUpdatedGift.Price, updatedGiftRetrieved.Price) - assert.Equal(t, fetchedUpdatedGift.Link, updatedGiftRetrieved.Link) + assert.Equal(t, "link1", updatedGiftRetrieved.Link) assert.Equal(t, fetchedUpdatedGift.Description, updatedGiftRetrieved.Description) assert.Equal(t, fetchedUpdatedGift.Demographic, updatedGiftRetrieved.Demographic) assert.Equal(t, fetchedUpdatedGift.Link, updatedGiftRetrieved.Link) @@ -641,7 +642,7 @@ func TestUpdateGift(t *testing.T) { // Check that there's only 1 Gift var count int64 - db.Model(&model.Gift{}).Where("id = ?", updatedGiftRetrieved.ID).Count(&count) + tx.Model(&model.Gift{}).Where("id = ?", fetchedUpdatedGift.ID).Count(&count) assert.Equal(t, int64(1), count) } @@ -823,7 +824,7 @@ func TestGetAllGiftCollection(t *testing.T) { CollectionName: "sample name", Gifts: []*model.Gift{}, } - + uintValue = uint(6) collection_two := model.GiftCollection{ CustomerID: &uintValue, CollectionName: "sample name 2",