Lesson 10
Create, Update, and Delete Using Sails Blueprint API
Summary
Summary of the Transcript
In this session, we explored the functionalities of a company management API using HTTP requests. Here's a breakdown of the key actions performed:
1. Retrieve All Companies
- Action: Changed the request from
POST
toGET
using the endpoint/company
. - Outcome: Received a JSON array containing the records of all companies, confirming the route is working correctly.
2. Retrieve a Specific Company
- Action: Used the endpoint
/company/1
to fetch information about the company with ID 1. - Outcome: Successfully received the specific company object, confirming the
find one
controller action is functioning.
3. Create a New Company
- Action: Sent a
POST
request to/companies
with JSON body to create a new company named "googly", founded in June 1997 with 700 employees. - Outcome: The company record was created, and the response included the new ID (2). This mapping corresponds to the
create
controller action.
4. Retrieve All Companies Again
- Action: Executed another
GET
request to/companies
. - Outcome: Confirmed that there are now two company records in the system.
5. Update a Company
- Action: Changed the request type to
PUT
and updated the name from "Google" to "Google U", and modified the employee count to 707. - Outcome: Successfully updated the record, returning the modified object.
6. Attempt to Use PATCH Method
- Action: Tested
PATCH
to update just the founding date of "Google U". - Outcome: It did not work as expected because the API does not seem to support
PATCH
, resulting in a 404 error. Reverting toPUT
worked correctly for updates.
7. Delete a Company
- Action: Sent a
DELETE
request to/company/2
to remove "Google U". - Outcome: The company was successfully deleted with a 200 OK response. A subsequent
GET
request confirmed that the record no longer exists, showing only the remaining companies.
Conclusion
We successfully demonstrated various HTTP methods (GET, POST, PUT, DELETE) to manage company records through the API. For more information about the limitations and functionalities, referring to the API documentation is recommended.
Video Transcript
So now that we have a company, let's try and get a list of all the companies like we did before.
Before we got an empty list, right?
So we expect now to have one record inside of this.
So let's go back here and switch from a post request to a get request, keeping the same URL, slash company.
So we expect to get a list of all the companies in the system right now.
So let's click send and see what we get.
So you can see the response right here.
It's an array of objects, JSON response that has that company that we created.
So indeed this route is working.
So this is the route that maps to the controller action called find.
All right, that finds all the records for this specific resource.
Now, if I wanted to get just information about this company here, you can say company slash the company ID.
So the company ID here is one.
So if I just want to get information about the company, whose ID is one, I can do it like so.
So we do a get slash company slash one, which is the ID.
So let's see what we get.
So as you can see, we get an object.
So this case gives you the specific object, the specific company information.
Okay, so this maps to the controller action find one.
Okay, we're doing a get slash resource slash resource ID, which maps to the controller action find one.
Okay, so how about we create another company?
Let's do a post request.
Let's change the URL back to slash companies slash company only.
And let's say let's go to the body of the post request.
If you can see here body, go back there and raw JSON again.
Let's create another company.
Let's say googly, something like that.
Let's say founded in some 1997, the month, let's say June 23 employee count, let's say has 700.
And let's create this record, let's click send.
And we're going to get scroll down to see the response.
So we create a new record.
So this post request post slash resource maps to the controller action create.
Okay, controller action is create.
And as we saw before, it's going to create the record with the data we provided, and it's going to add those time stamps, created at and updated at automatically for you.
You don't have to specify this in the schema.
And also obviously the ID, which now is number two.
So with that in mind, let's again get a listing of all the companies.
Let's go and switch to get request again.
And let's click send.
Now if you take a look at the response now we have another company there.
Now we have a list of two companies because we created another one.
As you can see now two records.
Okay.
So we learned how to create a record.
We learned how to fetch all the records and how to fetch a specific record.
Like if you want to get Google you can say slash two and you're going to get it.
Now let's learn how to update a record.
So we're going to switch from a get to a put.
Now we're going to go here and update this Google I don't like the name I'm going to change to something else.
So let's go into the body of this put request instead of Google.
Let's put Google you whatever.
If you want to change your play count to 707.
So let's see what we get.
Remember that before I had the name Google I with employee count of 700.
So let's see if we can update this resource and change its name and the employee count.
Let's click send.
Let's see the response scroll down.
Now you can see the response gives you back the object the record with the updated properties already there.
The name has been changed to Google you and the employee count to OK.
So that's how you update other record.
Now note that patch is also going to work.
So if you try to patch something for example let's remove from the body to name and the employee count.
I just want to change the founding date for company number two which is Google you.
Let's change the founding date to 2003 and let's keep the same month and day.
So it's going to work the same way.
So we can click send and let's see what we get.
Scroll down.
We expect 404 now it found what happened.
Seems like the patch actually does not work.
So the blueprint API does not provide a patch it seems.
Well that's too bad.
So we're going to stuck with the put.
So let's click send.
Let's see what we get.
So we got the Google you with the founding date 2003 and employee count as before.
So seems like that sales for the blueprint API also parts of the put and it's doing the same job as patch because we only passed one property to be updated instead of the whole original object.
All right.
So there's that detail for sales blueprint API.
If you want to learn more about that take a look at the blueprint API documentation.
All right.
Now let's keep on going.
See that's the 2003 that we changed and learn the final.
The last verb we're going to do is delete.
So say I don't like Google or anymore and I want to delete it.
You can do so delete slash company slash the ID of the company you want to delete.
So we're going to delete company to goodbye Google.
So let's do a delete and click send.
So we got 200.
Okay.
So this is the record.
Did they really get updated.
Let's take a look.
Let's go and switch to a get.
And let's click send.
So this is what we get.
No record found with the specified ID.
So it really got deleted from the list from the database.
So let's take a look at all the companies.
I changed the URL removed the ID.
Click send.
And now only Apple blah blah is there.
So indeed it deleted it.
So that's the sales blueprint API.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: