Skip to content

Commit

Permalink
fix: fixed problem on patch when trying to modify an element
Browse files Browse the repository at this point in the history
  • Loading branch information
Molaryy committed Nov 18, 2023
1 parent e0fc9a5 commit dc9d7e1
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions backend/models/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ func (h Handler) GetTodo(c *gin.Context) {
}
}

func updateTodoFields(storedTodo *Todo, updatedTodo Todo) {
storedValue := reflect.ValueOf(storedTodo).Elem()
updatedValue := reflect.ValueOf(updatedTodo)

for i := 0; i < storedValue.NumField(); i++ {
field := storedValue.Type().Field(i)
fieldName := field.Name
storedFieldValue := storedValue.FieldByName(fieldName)
updatedFieldValue := updatedValue.FieldByName(fieldName)

if !reflect.DeepEqual(updatedFieldValue.Interface(), reflect.Zero(field.Type).Interface()) {
storedFieldValue.Set(updatedFieldValue)
}
}
}

func (h Handler) UpdateTodo(c *gin.Context) {
var updatedTodo Todo
var storedTodo Todo
Expand All @@ -92,28 +108,18 @@ func (h Handler) UpdateTodo(c *gin.Context) {
statusCode = http.StatusNotFound
}

if statusCode == http.StatusAccepted {
updatedTodoValue := reflect.ValueOf(updatedTodo)
updatedTodoType := updatedTodoValue.Type()
storedTodoValue := reflect.ValueOf(storedTodo)
storedTodoType := storedTodoValue.Type()
for i := 0; i < updatedTodoType.NumField(); i++ {
if updatedTodoType.Field(i).Name == storedTodoType.Field(i).Name {
fieldValue := updatedTodoValue.Field(i).Interface()
if str, ok := fieldValue.(string); ok && str != "" {
storedField := storedTodoValue.FieldByName(updatedTodoType.Field(i).Name)
storedField.Set(reflect.ValueOf(fieldValue))
}
}
}
fmt.Println(storedTodo)
c.JSON(statusCode, gin.H{
"todo": storedTodo,
})
// h.DB.Save(&storedTodo)
} else {
fmt.Println(updatedTodo)
fmt.Println(storedTodo)
if statusCode != http.StatusAccepted {
c.JSON(statusCode, gin.H{
"message": fmt.Sprintf("Todo with id %v could't be found\n", todoId),
})
return
}
updateTodoFields(&storedTodo, updatedTodo)
// h.DB.Save(&storedTodo)
fmt.Println(storedTodo)
c.JSON(statusCode, gin.H{
"todo": storedTodo,
})
}

0 comments on commit dc9d7e1

Please sign in to comment.