// 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, repository, Where, } from '@loopback/repository'; import { del, get, getModelSchemaRef, getWhereSchemaFor, param, patch, post, requestBody, } from '@loopback/rest'; import {Todo, TodoList} from '../models'; import {TodoListRepository} from '../repositories'; export class TodoListTodoController { constructor( @repository(TodoListRepository) protected todoListRepository: TodoListRepository, ) {} @post('/todo-lists/{id}/todos', { responses: { '200': { description: 'TodoList model instance', content: {'application/json': {schema: getModelSchemaRef(Todo)}}, }, }, }) async create( @param.path.number('id') id: typeof TodoList.prototype.id, @requestBody({ content: { 'application/json': { schema: getModelSchemaRef(Todo, { title: 'NewTodoInTodoList', exclude: ['id'], optional: ['todoListId'], }), }, }, }) todo: Omit, ): Promise { return this.todoListRepository.todos(id).create(todo); } @get('/todo-lists/{id}/todos', { responses: { '200': { description: 'Array of TodoList has many Todo', content: { 'application/json': { schema: {type: 'array', items: getModelSchemaRef(Todo)}, }, }, }, }, }) async find( @param.path.number('id') id: number, @param.query.object('filter') filter?: Filter, ): Promise { return this.todoListRepository.todos(id).find(filter); } @patch('/todo-lists/{id}/todos', { responses: { '200': { description: 'TodoList.Todo PATCH success count', content: {'application/json': {schema: CountSchema}}, }, }, }) async patch( @param.path.number('id') id: number, @requestBody({ content: { 'application/json': { schema: getModelSchemaRef(Todo, {partial: true}), }, }, }) todo: Partial, @param.query.object('where', getWhereSchemaFor(Todo)) where?: Where, ): Promise { return this.todoListRepository.todos(id).patch(todo, where); } @del('/todo-lists/{id}/todos', { responses: { '200': { description: 'TodoList.Todo DELETE success count', content: {'application/json': {schema: CountSchema}}, }, }, }) async delete( @param.path.number('id') id: number, @param.query.object('where', getWhereSchemaFor(Todo)) where?: Where, ): Promise { return this.todoListRepository.todos(id).delete(where); } }