@@ -10,8 +10,8 @@ logic will live_!
1010
1111### Create your controller
1212
13- So, let's create a controller to handle our Todo routes. Inside the
14- ` src/controllers ` directory create the following two files:
13+ So, let's create a controller to handle our Todo routes. Create the
14+ ` src/controllers ` directory and two files inside :
1515- ` index.ts ` (export helper)
1616- ` todo.controller.ts `
1717
@@ -25,7 +25,7 @@ repository, which we'll use to perform our operations against the datasource.
2525#### src/controllers/todo.controller.ts
2626``` ts
2727import {repository } from ' @loopback/repository' ;
28- import {TodoRepository } from ' ../repositories' ;
28+ import {TodoRepository } from ' ../repositories/index ' ;
2929
3030export class TodoController {
3131 constructor (
@@ -51,10 +51,11 @@ Now that we have the repository wireup, let's create our first handler function.
5151
5252#### src/controllers/todo.controller.ts
5353``` ts
54- import {repository } from ' @loopback/repository ' ;
55- import {TodoRepository } from ' ../repositories ' ;
54+ import {post , param } from ' @loopback/openapi-v3 ' ;
55+ import {HttpErrors } from ' @loopback/rest ' ;
5656import {Todo } from ' ../models' ;
57- import {HttpErrors , post , param , requestBody } from ' @loopback/rest' ;
57+ import {repository } from ' @loopback/repository' ;
58+ import {TodoRepository } from ' ../repositories/index' ;
5859
5960export class TodoController {
6061 constructor (
@@ -93,36 +94,29 @@ verbs:
9394
9495#### src/controllers/todo.controller.ts
9596``` ts
96- import {repository } from ' @loopback/repository ' ;
97- import {TodoRepository } from ' ../repositories ' ;
97+ import {post , param , requestBody , get , put , patch , del } from ' @loopback/openapi-v3 ' ;
98+ import {HttpErrors } from ' @loopback/rest ' ;
9899import {Todo } from ' ../models' ;
99- import {
100- HttpErrors ,
101- post ,
102- param ,
103- requestBody ,
104- get ,
105- put ,
106- patch ,
107- del ,
108- } from ' @loopback/rest' ;
100+ import {repository } from ' @loopback/repository' ;
101+ import {TodoRepository } from ' ../repositories/index' ;
109102
110103export class TodoController {
111104 constructor (
112105 @repository (TodoRepository .name ) protected todoRepo : TodoRepository ,
113106 ) {}
114107
115108 @post (' /todo' )
116- async createTodo(@requestBody () todo : Todo ) {
109+ async createTodo(@requestBody ()todo : Todo ) {
117110 if (! todo .title ) {
118111 return Promise .reject (new HttpErrors .BadRequest (' title is required' ));
119112 }
120113 return await this .todoRepo .create (todo );
121114 }
122115
123116 @get (' /todo/{id}' )
124- async findTodoById(@param .path .number (' id' ) id : number ): Promise <Todo > {
125- id = + id ;
117+ async findTodoById(
118+ @param .path .number (' id' ) id : number ,
119+ @param .query .boolean (' items' ) items ? : boolean ): Promise <Todo > {
126120 return await this .todoRepo .findById (id );
127121 }
128122
@@ -134,18 +128,14 @@ export class TodoController {
134128 @put (' /todo/{id}' )
135129 async replaceTodo(
136130 @param .path .number (' id' ) id : number ,
137- @requestBody () todo : Todo ,
138- ): Promise <boolean > {
139- id = + id ;
131+ @requestBody () todo : Todo ): Promise <boolean > {
140132 return await this .todoRepo .replaceById (id , todo );
141133 }
142134
143135 @patch (' /todo/{id}' )
144136 async updateTodo(
145137 @param .path .number (' id' ) id : number ,
146- @requestBody () todo : Todo ,
147- ): Promise <boolean > {
148- id = + id ;
138+ @requestBody () todo : Todo ): Promise <boolean > {
149139 return await this .todoRepo .updateById (id , todo );
150140 }
151141
@@ -160,7 +150,7 @@ Some additional things to note about this example:
160150- Routes like ` @get('/todo/{id}') ` can be paired with the ` @param.path `
161151decorators to inject those values at request time into the handler function.
162152- LoopBack's ` @param ` decorator also contains a namespace full of other
163- "subdecorators" like ` @param.path ` , ` @param.query ` , and ` @param.header ` that
153+ "subdecorators" like ` @param.path ` , ` @param.query ` , and ` @param.body ` that
164154allow specification of metadata for those parts of a REST request.
165155- LoopBack's ` @param.path ` and ` @param.query ` also provide subdecorators for
166156specifying the type of certain value primitives, such as
0 commit comments