Update Endpoint

As your users look back at past memories, they might discover typos. You want them to be able to edit existing memories, so you add this PATCH endpoint:

service.patch('/memories/:id', (request, response) => {
// issue update statement...
});

The ID of the memory is included as a parameter in the URL. You expect the rest of the memory's fields to be sent along in a JSON payload.

The callback is similar to the one you used for inserting a brand new memory. It issues an UPDATE statement with a WHERE clause that singles out the memory. However, there's no need to send much back in the response beyond a confirmation that the update succeeded:

service.patch('/memories/:id', (request, response) => {
const parameters = [
request.body.year,
request.body.month,
request.body.day,
request.body.entry,
parseInt(request.params.id),
];

const query = 'UPDATE memory SET year = ?, month = ?, day = ?, entry = ? WHERE id = ?';
connection.query(query, parameters, (error, result) => {
if (error) {
response.status(404);
response.json({
ok: false,
results: error.message,
});
} else {
response.json({
ok: true,
});
}
});
});

Try adding this endpoint to your service and restarting it. Because this is a PATCH request, you can't test it with your browser. Instead, run a command like this in your terminal:

curl --header 'Content-Type: application/json' \
--request PATCH \
--data '{"year": 2013, "month": 2, "day": 3, "entry": "Got my first bike! Also, I broke my first arm."}' \
http://example.com:5001/memories/1

Adjust the ID in the URL as needed to point to an existing memory. Try querying for all the memories on this month and day. Does the updated record appear in the results?