Skip to content

Commit

Permalink
refactor: improve note update method with robust error handling
Browse files Browse the repository at this point in the history
- Added input validation for note update method
- Implemented checks for empty ID and empty updates
- Enhanced error handling with specific error messages
- Ensured proper error reporting for update failures and non-existent notes
  • Loading branch information
tphakala committed Jan 26, 2025
1 parent e8acb66 commit 545b92b
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions internal/datastore/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,23 @@ func getHourRange(hour string, duration int) (startTime, endTime string) {
return startTime, endTime
}

// UpdateNote updates specific fields of a note
// UpdateNote updates specific fields of a note. It validates the input parameters
// and returns appropriate errors if the note doesn't exist or if the update fails.
func (ds *DataStore) UpdateNote(id string, updates map[string]interface{}) error {
return ds.DB.Model(&Note{}).Where("id = ?", id).Updates(updates).Error
if id == "" {
return fmt.Errorf("invalid id: must not be empty")
}
if len(updates) == 0 {
return fmt.Errorf("no updates provided")
}

result := ds.DB.Model(&Note{}).Where("id = ?", id).Updates(updates)
if result.Error != nil {
return fmt.Errorf("failed to update note: %w", result.Error)
}
if result.RowsAffected == 0 {
return fmt.Errorf("note with id %s not found", id)
}

return nil
}

0 comments on commit 545b92b

Please sign in to comment.