Skip to content

Commit

Permalink
Update gift now only updates changed fields
Browse files Browse the repository at this point in the history
  • Loading branch information
matherg committed Oct 18, 2023
1 parent bc9a42c commit 0bfa451
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
27 changes: 20 additions & 7 deletions api/src/model/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 5 additions & 4 deletions api/tests/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,6 @@ func TestUpdateGift(t *testing.T) {
updatedTestGift := model.Gift{
Name: "updatedgift1",
Price: 100,
Link: "updatedlink1",
Description: "updateddescription1",
Demographic: "updateddemogrpahic1",
GiftCollections: nil,
Expand All @@ -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)
Expand All @@ -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)
}

Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 0bfa451

Please sign in to comment.