-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(example-getting-started): restore original code for migration
- Loading branch information
Kevin Delisle
committed
Jan 19, 2018
1 parent
dbbfb2c
commit 99bb50e
Showing
5 changed files
with
93 additions
and
71 deletions.
There are no files selected for viewing
77 changes: 35 additions & 42 deletions
77
packages/example-getting-started/src/controllers/todo.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,56 @@ | ||
// Copyright IBM Corp. 2017,2018. All Rights Reserved. | ||
// Node module: @loopback/example-getting-started | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
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'; | ||
import {HttpErrors} from '@loopback/rest'; | ||
import {TodoSchema, Todo} from '../models'; | ||
import {repository} from '@loopback/repository'; | ||
import {TodoRepository} from '../repositories/index'; | ||
|
||
export class TodoController { | ||
// TODO(bajtos) Fix documentation (and argument names?) of @repository() | ||
// to allow the usage below. | ||
// See https://github.com/strongloop/loopback-next/issues/744 | ||
constructor( | ||
@inject('repositories.TodoRepository') | ||
public todoRepository: TodoRepository, | ||
@repository(TodoRepository.name) protected todoRepo: TodoRepository, | ||
) {} | ||
|
||
@post('/todo') | ||
async create(@param.body('obj') obj: Todo): Promise<Todo> { | ||
return await this.todoRepository.create(obj); | ||
@param.body('todo', TodoSchema) | ||
async createTodo(todo: Todo) { | ||
// TODO(bajtos) This should be handled by the framework | ||
// See https://github.com/strongloop/loopback-next/issues/118 | ||
if (!todo.title) { | ||
return Promise.reject(new HttpErrors.BadRequest('title is required')); | ||
} | ||
return await this.todoRepo.create(todo); | ||
} | ||
|
||
@get('/todo/count') | ||
async count(@param.query.string('where') where: Where): Promise<number> { | ||
return await this.todoRepository.count(where); | ||
@get('/todo/{id}') | ||
@param.path.number('id') | ||
@param.query.boolean('items') | ||
async findTodoById(id: number, items?: boolean): Promise<Todo> { | ||
return await this.todoRepo.findById(id); | ||
} | ||
|
||
@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); | ||
async findTodos(): Promise<Todo[]> { | ||
return await this.todoRepo.find(); | ||
} | ||
|
||
@get('/todo/{id}') | ||
async findById(@param.path.number('id') id: number): Promise<Todo> { | ||
return await this.todoRepository.findById(id); | ||
@put('/todo/{id}') | ||
@param.path.number('id') | ||
@param.body('todo', TodoSchema) | ||
async replaceTodo(id: number, todo: Todo): Promise<boolean> { | ||
return await this.todoRepo.replaceById(id, todo); | ||
} | ||
|
||
@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); | ||
@param.path.number('id') | ||
@param.body('todo', TodoSchema) | ||
async updateTodo(id: number, todo: Todo): Promise<boolean> { | ||
return await this.todoRepo.updateById(id, todo); | ||
} | ||
|
||
@del('/todo/{id}') | ||
async deleteById(@param.path.number('id') id: number): Promise<boolean> { | ||
return await this.todoRepository.deleteById(id); | ||
@param.path.number('id') | ||
async deleteTodo(id: number): Promise<boolean> { | ||
return await this.todoRepo.deleteById(id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters