Blueprint
  • Blueprint Developer Guide
  • Quick Start
    • Getting Started
    • My First Application
      • Creating Your Application
      • Controllers
      • Routers & Routes
      • Services
      • Resources & Resource Controllers
      • Validating & Sanitizing Input
      • Unit Testing Your Application
      • Policies
  • Developer Guide
    • The Object Model
      • Introduction
      • Classes and Instances
      • Computed Properties
      • Aggregated Properties
      • Mixins
    • Routers and Controllers
      • Introduction
      • Routers
      • Controllers
      • Resources
    • Models
    • The Server
    • Policy Framework
    • Services
    • Messaging Framework
    • Configuration Management
    • Application and Resources
      • Lookup Operation
      • Views
      • Assets
    • Blueprint Modules
    • Blueprint Cluster
      • What is a Blueprint Cluster?
      • Running a Blueprint Cluster
      • Technical Details
    • Testing Framework
    • Command-line Interface (Coming Soon)
Powered by GitBook
On this page
  • Generating Your Controller
  • Implementing the Action
  • Next Steps
  1. Quick Start
  2. My First Application

Controllers

PreviousCreating Your ApplicationNextRouters & Routes

Last updated 7 years ago

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 , 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

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.

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 .

super-rentals tutorial
same data as the EmberJS super-rentals example