Controllers

Generating Your Controller

Now that we have created our application, our next step is to create our controllers. Controllers define the business-logic of the Blueprint application, and are a composed from group of actions. The actions of a controller are responsible for servicing requests from clients.

If we look at the original super-rentals tutorial, the mirage component of the tutorial defines the single route GET /api/rentals. This means that we need an action to handle this HTTP request from the client.

First, let's define our controller. We are going to name the controller rental since it will be responsible for all business logic related to rentals.

blueprint generate controller rental

Make sure you run the Blueprint cli from the Blueprint application directory. In this tutorial, you must run the Blueprint cli from the ./super-rentals directory.

This command will generate an empty controller (i.e., a controller with no actions) named rental in the ./app/controllers directory.

app/controllers/rental.js
const {
  Controller,
  Action
} = require ('@onehilltech/blueprint');

/**
 * @class rental
 */
module.exports = Controller.extend ({
})

Implementing the Action

We can now add our single action to the rental controller that will get the rentals when requested by a client. The action we create will return the same data as the EmberJS super-rentals example.

app/controllers/rental.js
const {
  Controller,
  Action
} = require ('@onehilltech/blueprint');

/**
 * @class rental
 */
module.exports = Controller.extend ({
  get () {
    return Action.extend ({
      /**
       * The execute(res, res) method is an abstract method that must be 
       * implemented by all Action subclasses. The execute(req, res) method
       * is responsible for handling the request by sending a response to
       * the client.       
       */
      execute (req, res) {
        res.status (200).json ({
          data: [
            {
              type: 'rentals',
              id: 'grand-old-mansion',
              attributes: {
                title: 'Grand Old Mansion',
                owner: 'Veruca Salt',
                city: 'San Francisco',
                category: 'Estate',
                bedrooms: 15,
                image: 'https://upload.wikimedia.org/wikipedia/commons/c/cb/Crane_estate_(5).jpg'
              }
            },
            {
              type: 'rentals',
              id: 'urban-living',
              attributes: {
                title: 'Urban Living',
                owner: 'Mike Teavee',
                city: 'Seattle',
                category: 'Condo',
                bedrooms: 1,
                image: 'https://upload.wikimedia.org/wikipedia/commons/0/0e/Alfonso_13_Highrise_Tegucigalpa.jpg'
              }
            },
            {
              type: 'rentals',
              id: 'downtown-charm',
              attributes: {
                title: 'Downtown Charm',
                owner: 'Violet Beauregarde',
                city: 'Portland',
                category: 'Apartment',
                bedrooms: 3,
                image: 'https://upload.wikimedia.org/wikipedia/commons/f/f7/Wheeldon_Apartment_Building_-_Portland_Oregon.jpg'
              }
            }]
        });
      }
    })
  }
});

That was quite simple!

Next Steps

Now we have a controller that defines an action to respond to client requests for rentals. Our next step is to define a route that invokes this action.

Last updated