Last active
June 3, 2019 22:40
-
-
Save Duncanian/c22acbfd2281705434c5c9d4d7e0afd8 to your computer and use it in GitHub Desktop.
Handling the endpoint for updating an event in go-rest-api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func updateEvent(w http.ResponseWriter, r *http.Request) { | |
eventID := mux.Vars(r)["id"] | |
var updatedEvent event | |
reqBody, err := ioutil.ReadAll(r.Body) | |
if err != nil { | |
fmt.Fprintf(w, "Kindly enter data with the event title and description only in order to update") | |
} | |
json.Unmarshal(reqBody, &updatedEvent) | |
for i, singleEvent := range events { | |
if singleEvent.ID == eventID { | |
singleEvent.Title = updatedEvent.Title | |
singleEvent.Description = updatedEvent.Description | |
events = append(events[:i], singleEvent) | |
json.NewEncoder(w).Encode(singleEvent) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment