# 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.&#x20;

If we look at the original [super-rentals tutorial](https://guides.emberjs.com/release/tutorial/installing-addons/#toc_ember-cli-mirage), 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.

```bash
blueprint generate controller rental
```

{% hint style="info" %}
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.
{% endhint %}

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

{% code title="app/controllers/rental.js" %}

```javascript
const {
  Controller,
  Action
} = require ('@onehilltech/blueprint');

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

{% endcode %}

## 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](https://guides.emberjs.com/release/tutorial/installing-addons/#toc_ember-cli-mirage).

{% code title="app/controllers/rental.js" %}

```javascript
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'
              }
            }]
        });
      }
    })
  }
});
```

{% endcode %}

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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blueprint.onehilltech.com/quick-start/my-first-application/controllers.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
