Instructor: We're now going to demonstrate updating and deleting Domino data via the NodeJS server. To do that for our demonstration, we're going to need to add several routes. They're all fairly similar, so what I'll do is just highlight the differences.
The first that we're going to need to do is get hold of a list of records. We're going to use this list in our demo just to be able to use select a document, which will then open up the data. Then it allows you to update it or delete it.
Common to all these routes will be the need to have the Domino security token past. The options we need for the request to Domino are going to be, again, fairly similar across all our routes.
In the case of a view of all records, which is past that as the last argument in our API code. Then we'll use our standard method for dealing with the response back from Domino, which will be to check for any security issues. I'll send through the data. I'll also handle any errors.
That's our first route, which is to get all "Star Wars" records from a note's view. We're also going to need the following routes. We're going to need a route to get an actual individual document.
This one is going to be past the unit off the record. Exactly the same, we're looking for Domino security. The options are very similar, except the URL is slightly different where we have documents/unit. We pass it the unit, which has been passed to the express application.
When you have a route that looks like this with a colon you did in this case, that property becomes available to you through the Express framework using this syntax. Our request to Domino is exactly the same.
That's an individual record. We now have to deal with the update to an individual document. There are two methods available to you or HTTP methods, I should say. There's the PATCH and a PUT.
Now, according to the Domino REST API, if you use the PATCH method, you only need to pass the specific fields you want to update. Whereas the PUT method, you need to pass either all your fields, or if you don't, then the ones that you don't pass will be blanked.
Therefore, I see no real value in the PUT method. I would stick to PATCH. To keep it the same on our Node server, we'll also use the PATCH HTTP method. Again, we'll be passed the unit of the document we want to update.
Again, we'll be checking for Domino security. Now, our options for our request to Domino is slightly different. The key thing here is we have to set a special header for Domino called the XHTTP Method Override. It needs a PATCH as its value.
The actual HTTP request to Domino will be a standard post. As part of our PATCH, we will be passing the body from the request and send that straight through to Domino. This can be a subset of fields.
That will then update the Domino document, but it's critical. You must include this value as a HTTP header. The actual request to Domino itself is exactly the same as our other routes.
Next, we'll add the Delete route. Again, we'll be passed the unit of the document we want to delete.
We'll be using Domino security, so we need a token. Our options for our requested Domino are similar, except the method will be Delete and our actual request to Domino will be the same as the other routes.
We've got get all records, get an individual record, update a single record, and delete a record. That's our service inaudible JS file updated of all the new routes. We'll now look at the index to our HTML page, which we'll use to show how it all hangs together.
Here, we have our web page, which we're going to use to display our records and edit them and then delete them. Standard bootstrap layout -- we've got a top form, which we'll use to do the login, seen before on this one.
Then we've got a container, which is going to hold a list of our records. Then finally, we have a bottom form, which we'll be using to do the updating of the data. Down the bottom, we have our script functions.
Now, I should point out that this demo is using no libraries, whatsoever. There's no jQuery or no AngularJS, no React, no View. All this stuff would be much easier using a single-page application.
We have a function to load records. This will be calling our Get on our Node server, which is the SW records route. Once we get back the information, we're going to loop over it and update our list element with a list of links.
Clicking on a link we'll call the Load Record function, passing it the unit of the selected record. This is engulfed to our Node server. Using the SW record/unit route, it will get the data. It will then update the form for it to be editable.
We then have our update function. This one will do the update by sending a PATCH HTTP method to the Node server, passing it the unit. The last one is we got a Delete method.
This again is very similar where we're just calling our SW records with the unit and we're passing it a Delete HTTP method. One thing we're doing is to hold the unit value for the selected record as a hidden input field on the form.
Let's have a look and see what it looks like in the browser. I've started my server, refreshed my browser page, loaded up my new HTML page, which is to be records. The first thing I'm presented with is the login.
I'm going to log in to Domino via my Node server. Successfully logging in. The next thing that happens is the call to going to get to the note's view. You'll see here, we're actually getting back a response, which is Domino data which corresponds to this list element up here.
If I click on one of these, I then populate my form. Using the Update method here, I can change a value.
If I click update, it's going to go off and do a patch to Domino. The end result is actually updated automatically up here, because my view is being refreshed as well.
Finally, just to prove I'm using Domino data, I've turned off the ability to delete from my username. If I click delete, I get an error, coming back from Domino saying I'm not authorized to perform the operation because in Domino, I'm not authorized to delete documents.
If I click delete documents, I go back into the browser. Try again. The document is deleted. That's the code working.
As I say, click on the document. Load it up into the form. You can edit it, update it, and delete it.