Skip to content

Commit

Permalink
Remove Duplicate functions
Browse files Browse the repository at this point in the history
  • Loading branch information
matherg committed Oct 15, 2023
1 parent 739bf18 commit ea436b3
Showing 1 changed file with 7 additions and 47 deletions.
54 changes: 7 additions & 47 deletions api/src/model/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ 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,
updates := map[string]interface{}{
"Name": inputGift.Name,
"Price": inputGift.Price,
"Link": inputGift.Link,
"Description": inputGift.Description,
"Demographic": inputGift.Demographic,
"GiftCollections": inputGift.GiftCollections,
}

Expand All @@ -58,7 +58,7 @@ func UpdateGiftToDb(db *gorm.DB, id int64, inputGift Gift) (Gift, error) {
}

// DeleteGiftFromDb Delete Gift
func DeleteGiftFromDb(db *gorm.DB, id int64) (error) {
func DeleteGiftFromDb(db *gorm.DB, id int64) error {
var deletedGift Gift
if err := db.Where("id = ?", id).First(&deletedGift).Error; err != nil {
return err
Expand Down Expand Up @@ -97,50 +97,10 @@ func GetAllGiftsFromDB(db *gorm.DB) ([]Gift, error) {
return gifts, nil
}

// UpdateGiftToDb updates the Gift and returns it
func UpdateGiftToDb(db *gorm.DB, id int64, inputGift Gift) (Gift, error) {
// Fetch Gift Record
var updatedGift Gift
if err := db.Where("id = ?", id).First(&updatedGift).Error; err != nil {
return Gift{}, err
}

// 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,
}

if err := db.Model(&updatedGift).Updates(updates).Error; err != nil {
return Gift{}, err
}

// Return Updated Gift Record
return updatedGift, nil
}

// WriteGiftToDb saves the Gift and returns it
func WriteGiftToDb(db *gorm.DB, inputGift Gift) (Gift, error) {
if err := db.Create(&inputGift).Error; err != nil {
return Gift{}, err
}
return inputGift, nil
}

// Delete Gift
func DeleteGiftFromDb(db *gorm.DB, id int64) error {
var deletedGift Gift
if err := db.Where("id = ?", id).First(&deletedGift).Error; err != nil {
return err
}

if err := db.Delete(&deletedGift).Error; err != nil {
return err
}

return nil
}

0 comments on commit ea436b3

Please sign in to comment.