Skip to content

Commit

Permalink
docs(lb4): scaffolding a CRUD controller (#583)
Browse files Browse the repository at this point in the history
* docs(lb4): scaffolding a REST Controller with CRUD

* docs(lb4): Update empty controller example
  • Loading branch information
Kevin Delisle authored Jan 15, 2018
1 parent 692bf6c commit 425065b
Showing 1 changed file with 93 additions and 3 deletions.
96 changes: 93 additions & 3 deletions pages/en/lb4/Controller-generator.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,98 @@ If provided, the tool will use that as the default when it prompts for the name.

The tool will prompt you for:

- Name of the controller. If the name had been supplied from the command line, the prompt is skipped and the controller is built with the name from the command-line argument.
- Name of the controller. If the name had been supplied from the command line,
the prompt is skipped and the controller is built with the name from the
command-line argument.
- Type of the controller. You can select from the following types:
* **Empty Controller** - An empty controller definition
* **Basic CRUD Controller** - A controller wired up to a model and repository
definition, with pre-defined CRUD methods.

### Output
#### Empty Controller
If you select the Empty Controller, it will generate a nearly-empty template
based on the given name:

The tool will create a new file as <code>/src/controllers/<i>controller-name</i>.controller.ts</code>. The file will contain an empty constructor and common package imports that can be uncommented.
```ts
// Uncomment these imports to begin using these cool features!

// import {inject} from '@loopback/context';

export class FooController {
constructor() {}
}
```

#### REST Controller with CRUD Methods
If you select the REST Controller with CRUD Methods type, you will then be asked
to select:
- The model to use for the CRUD function definitions
- The repository for this model that provides datasource connectivity

{% include warning.html content=
"
If you do not have a model and repository to select, then you will
receive an error!
" lang=page.lang %}

Here's an example of what the template will produce given a `Todo` model and
a `TodoRepository`:
```ts
import {Filter, Where} from '@loopback/repository';
import {post, param, get, put, patch, del} from '@loopback/openapi-v2';
import {inject} from '@loopback/context';
import {Todo} from '../models';
import {TodoRepository} from '../repositories';

export class TodoController {

constructor(
@inject('repositories.TodoRepository')
public todoRepository : TodoRepository,
) {}

@post('/todo')
async create(@param.body('obj') obj: Todo)
: Promise<Todo> {
return await this.todoRepository.create(obj);
}

@get('/todo/count')
async count(@param.query.string('where') where: Where) : Promise<number> {
return await this.todoRepository.count(where);
}

@get('/todo')
async find(@param.query.string('filter') filter: Filter)
: Promise<Todo[]> {
return await this.todoRepository.find(filter);
}

@patch('/todo')
async updateAll(@param.query.string('where') where: Where,
@param.body('obj') obj: Todo) : Promise<number> {
return await this.todoRepository.updateAll(where, obj);
}

@del('/todo')
async deleteAll(@param.query.string('where') where: Where) : Promise<number> {
return await this.todoRepository.deleteAll(where);
}

@get('/todo/{id}')
async findById(@param.path.number('id') id: number) : Promise<Todo> {
return await this.todoRepository.findById(id);
}

@patch('/todo/{id}')
async updateById(@param.path.number('id') id: number, @param.body('obj')
obj: Todo) : Promise<boolean> {
return await this.todoRepository.updateById(id, obj);
}

@del('/todo/{id}')
async deleteById(@param.path.number('id') id: number) : Promise<boolean> {
return await this.todoRepository.deleteById(id);
}
}
```

0 comments on commit 425065b

Please sign in to comment.