Skip to content

Commit

Permalink
fix(update): correct query when finding by site_name
Browse files Browse the repository at this point in the history
  • Loading branch information
musaubrian committed May 28, 2024
1 parent 5b5865d commit a6a4ead
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
8 changes: 6 additions & 2 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ import (

// updateCmd represents the update command
var updateCmd = &cobra.Command{
Use: "update",
Short: "Update a single site records",
Use: "update",
Short: "Update a single site records",
Long: `
Update a single site records either by the user_name or site_name
NOTE:
When there are records with the same user_name, only the first one gets updated`,
Aliases: []string{"u"},
}

Expand Down
26 changes: 21 additions & 5 deletions internal/model/model_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package model
import (
"errors"
"fmt"
"reflect"
"strings"

"github.com/charmbracelet/huh"
Expand Down Expand Up @@ -54,22 +55,22 @@ func UpdateRecord(recordType RecordType) {

if recordType == SiteName {
sitename := GetInput("Site record to Update")
err := db.Where("site = ?", sitename).First(&site)
err := db.Where("name = ?", sitename).First(&site)
if err.Error != nil {
del.Printf("Record not found")
return
}
s := UpdateRec(site)
success.Printf("\nUpdated {%s} to {%s}\n\n", sitename, s.Name)
updatedSite := UpdateRec(site)
printChanges(site, updatedSite)
} else if recordType == Username {
user := GetInput("User record to Update")
err := db.Where("user_name = ?", user).First(&site)
if err.Error != nil {
del.Printf("Record not found")
return
}
s := UpdateRec(site)
success.Printf("\nUpdated {%s} to {%s}\n\n", user, s.UserName)
updatesSite := UpdateRec(site)
printChanges(site, updatesSite)
}

}
Expand Down Expand Up @@ -143,3 +144,18 @@ func UpdateRec(site Site) Site {
db.Save(&site)
return site
}

func printChanges(prev, updated Site) {
originalValue := reflect.ValueOf(prev)
updatedValue := reflect.ValueOf(updated)
for i := 0; i < originalValue.NumField(); i++ {
origField := originalValue.Field(i)
updtField := updatedValue.Field(i)
if !reflect.DeepEqual(origField.Interface(), updtField.Interface()) {
field := bold.Sprintf("%v", originalValue.Type().Field(i).Name)
prevVal := bold.Sprintf("%v", origField.Interface())
newVal := bold.Sprintf("%v", updtField.Interface())
success.Printf("%s changed from %s to %s\n", field, prevVal, newVal)
}
}
}

0 comments on commit a6a4ead

Please sign in to comment.