...

/

Completing and Deleting Items

Completing and Deleting Items

Learn how to add two missing features: completion and deletion.

Let’s complete the application functionality by adding the two missing features: the complete command to mark an item as done and the del command to delete an item from the list.

According to the to-do REST API requirements, to complete an item we must send an HTTP PATCH request to the endpoint /todo/id?complete, where id is an integer number representing the item in the list. To delete an item from the list, we send an HTTP DELETE request to the endpoint /todo/id, where id is again a number that represents the item.

To send those requests, we’ll reuse the function sendRequest(). We open and edit the file cmd/client.go. We define a new function completeItem() that takes two parameters, the apiRoot as string and id as int. It returns an error. This function uses those two parameters to compose the final URL for the request and then uses the sendRequest() function to send the request to the server:

func completeItem(apiRoot string, id int) error {
u := fmt.Sprintf("%s/todo/%d?complete", apiRoot, id)
return sendRequest(u, http.MethodPatch, "", http.StatusNoContent, nil)
}
The completeItem() function

In this function, we use the constant http.MethodPatch as the ...