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

Summary

# Lecture Summary: Sales Blueprint API and RESTful Routes

In the previous lecture, we focused on creating a model and setting up its schema. This lecture will cover the **Sales Blueprint API** and how it generates **RESTful Routes** for our resource, specifically the **Company** resource.

## Overview of Company Resource
- The Company resource model includes:
  - Name
  - Founding date
  - Employee count

## Automatic Controller Generation
Using the command `SalesGenerateApi`, Sales automatically creates a controller for the Company resource. Initially, the controller file contains an empty module. Behind the scenes, several RESTful controller actions are created that map to each HTTP request type:

### Actions Mapped to HTTP Requests
- **GET /companies**: Retrieves all companies (`find` action)
- **GET /companies/:id**: Retrieves a specific company (`findOne` action)
- **POST /companies**: Creates a new company (`create` action)
- **PUT /companies/:id**: Updates an existing company (`update` action)
- **DELETE /companies/:id**: Deletes a company (`destroy` action)
- **Additional Actions**: There is a `populate` action; refer to documentation for details.

### Code Visibility
Although these actions are generated automatically, they are not explicitly defined in the code. The controller file for the company (located at `API/controllers/companyController`) appears empty.

## Visualization of API Functionality
To visualize and test the generated actions, we will make HTTP calls using a REST client. It is recommended to use **Postman**, a versatile tool for making API requests. 

### Getting Started with Postman
- Download and install Postman from [GetPostman.com](https://www.getpostman.com).
- After installation, we will explore the automatically generated API in our Sales app.

## Next Steps
In the upcoming lecture, we will perform operations such as:
- Creating new companies
- Listing all companies
- Retrieving specific companies
- Updating a company
- Deleting a company

Stay tuned for the practical application of these concepts!

For more information, check out the GitHub Gist by visiting: [gist.github.com/NVKhold](https://gist.github.com/NVKhold/salesblueprintrest.md)

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: