// Copyright IBM Corp. and LoopBack contributors 2018,2020. All Rights Reserved. // Node module: @loopback/example-todo-list // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT import { Count, CountSchema, Filter, FilterExcludingWhere, repository, Where, } from '@loopback/repository'; import { del, get, getModelSchemaRef, param, patch, post, put, requestBody, } from '@loopback/rest'; import {Todo, TodoList} from '../models'; import {TodoRepository} from '../repositories'; export class TodoController { constructor( @repository(TodoRepository) public todoRepository: TodoRepository, ) {} @post('/todos', { responses: { '200': { description: 'Todo model instance', content: {'application/json': {schema: getModelSchemaRef(Todo)}}, }, }, }) async create( @requestBody({ content: { 'application/json': { schema: getModelSchemaRef(Todo, { title: 'NewTodo', exclude: ['id'], }), }, }, }) todo: Omit, ): Promise { return this.todoRepository.create(todo); } @get('/todos/{id}', { responses: { '200': { description: 'Todo model instance', content: { 'application/json': { schema: getModelSchemaRef(Todo, {includeRelations: true}), }, }, }, }, }) async findById( @param.path.number('id') id: number, @param.filter(Todo, {exclude: 'where'}) filter?: FilterExcludingWhere, ): Promise { return this.todoRepository.findById(id, filter); } @get('/todos', { responses: { '200': { description: 'Array of Todo model instances', content: { 'application/json': { schema: { type: 'array', items: getModelSchemaRef(Todo, {includeRelations: true}), }, }, }, }, }, }) async find(@param.filter(Todo) filter?: Filter): Promise { return this.todoRepository.find(filter); } @put('/todos/{id}', { responses: { '204': { description: 'Todo PUT success', }, }, }) async replaceById( @param.path.number('id') id: number, @requestBody() todo: Todo, ): Promise { await this.todoRepository.replaceById(id, todo); } @patch('/todos/{id}', { responses: { '204': { description: 'Todo PATCH success', }, }, }) async updateById( @param.path.number('id') id: number, @requestBody({ content: { 'application/json': { schema: getModelSchemaRef(Todo, {partial: true}), }, }, }) todo: Todo, ): Promise { await this.todoRepository.updateById(id, todo); } @del('/todos/{id}', { responses: { '204': { description: 'Todo DELETE success', }, }, }) async deleteById(@param.path.number('id') id: number): Promise { await this.todoRepository.deleteById(id); } @get('/todos/{id}/todo-list', { responses: { '200': { description: 'TodoList belonging to Todo', content: { 'application/json': { schema: {type: 'array', items: getModelSchemaRef(TodoList)}, }, }, }, }, }) async getTodoList( @param.path.number('id') id: typeof Todo.prototype.id, ): Promise { return this.todoRepository.todoList(id); } @get('/todos/count', { responses: { '200': { description: 'Todo model count', content: {'application/json': {schema: CountSchema}}, }, }, }) async count(@param.where(Todo) where?: Where): Promise { return this.todoRepository.count(where); } @patch('/todos', { responses: { '200': { description: 'Todo PATCH success count', content: {'application/json': {schema: CountSchema}}, }, }, }) async updateAll( @requestBody({ content: { 'application/json': { schema: getModelSchemaRef(Todo, {partial: true}), }, }, }) todo: Todo, @param.where(Todo) where?: Where, ): Promise { return this.todoRepository.updateAll(todo, where); } }