Loading
Lesson 07
Courses / Quick Start Introduction to Sails
Sails RESTful Controller Actions

Video Transcript

In the last lecture, we learned how to create a model and how to set up its schema. This lecture is going to be about the Sales Blueprint API and how it automatically generates RESTful Routes for our resource. So the resource in question is Company. So we created a model for a company resource that has a name, founding date, and employee count. Now Sales automatically creates a controller for us, as we saw using the command SalesGenerateApi. Now the control file didn't have anything in it, just a module that exports with an empty object. Now Sales behind the scenes actually creates several controller actions for us, RESTful Controller Actions that already map to each HTTP request type. For example, here's a list that I have that I made a GitHub Gist with this list. If you want to access by the way, you can go to NVKhold slash salesblueprintrest.md. It's available at gist.github.com slash NVKhold, my username, if you want to take a look. Anyway, let's keep on going. So for example, if you want to get all the companies, that would be a RESTfulRout like get slash the resource. So Sales is going to automatically create the resource controller dot find action that already has that logic to retrieve that resource and give back a list of all the resources. Similarly, if you want a specific resource, a get slash resource slash its ID, that is going to be under the find one controller action that's already implemented by the blueprint API from Sales and so on. You can do post to create a new resource and that maps to the create action of the controller. To update, you can do put and that maps to the update action. For deletion, you can, it maps the destroy action. And there's also another one called populate that if you want to know more about, check the documentation. So the thing is, you don't really see these guys being defined in the code. If you go back to the code, this is company controller under API slash controllers slash company controller. And there's nothing here. So that's one thing about sales is that's things for you, but you can't really see it here. So in order for us to be able to visualize what's going on here and to see that it's really working behind the scenes, we are going to make all these HTTP HTTP calls and see the resources to create the resources and so on. For that, we're going to need a REST client. Okay. And sales has something that allows you to create and update and change records all using the same using a get request. I'm not going to use that because I don't think that's proper RESTful. It's not using the proper way of calling an API. So I'm going to use a REST client and make the appropriate calls with the right verbs and so on. So for that, we're going to need a REST client. I recommend you use postman. Okay. So postman now is an app by itself used to be a Chrome. It still is, but it's going to become obsolete because Chrome apps are going to be obsolete. But you can get the app in a getpostman.com, www.getpostman.com. And you can download for your platform. And after installing it, we're going to start looking at that API that's automatically generated in our sales app by the blueprint API. We're going to play around and we're going to create some companies. We're going to list all the companies, list the specific companies, update a company, delete a company, all that stuff coming up in the next lecture.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: